question

Raju avatar image
Raju asked

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

sql-server-2005sql-server-2000query
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Madhivanan avatar image
Madhivanan answered

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            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.