x
login about faq Site discussion (meta-askssc)

Select query---Show table rows in columns

Hi all,

Table name is Emp

Field : EName

And only 1 field and is indexed.

"select * from Emp" will give result as

                    
Item                     
------                     
A                    
B                    
C                    
D                     
E                     
F                     
G                     
H                     

Need an output like ?i.e sequential

                    
A   B   C                     
D   E   F                     
G   H                     

and there is only 1 field and an index on it.

Can any one pls tell me how to get the above result with SQL query??

Regards,

Raju

more ▼

asked Nov 04 '09 at 03:02 AM in Default

Raju gravatar image

Raju
1 1 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Why do you need this type of output?

One of the methods is

declare @t table(data char(1))            
insert into @t            
select char(number) as data from master..spt_values             
where type='p' and number between 97 and 104            
            
            
select [1],[2],[3] from            
(            
select row_number() over (partition by number order by data) as sno, * from             
    (            
    select data,ntile(3)over(order by data) as number from @t            
    ) as t            
) as y            
pivot            
(            
    max(data) for sno in ([1],[2],[3])            
) d            
more ▼

answered Nov 04 '09 at 05:06 AM

Madhivanan gravatar image

Madhivanan
1.1k 1 2 6

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1843
x455
x322

asked: Nov 04 '09 at 03:02 AM

Seen: 892 times

Last Updated: Nov 11 '09 at 06:00 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.