x

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, 2009 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, 2009 at 05:06 AM

Madhivanan gravatar image

Madhivanan
1.1k 1 3 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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1948
x473
x371

asked: Nov 04, 2009 at 03:02 AM

Seen: 1280 times

Last Updated: Nov 11, 2009 at 06:00 AM