Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query for SQL Server

Author  Topic 

Indrajit
Starting Member

11 Posts

Posted - 2012-08-01 : 06:44:28
Hi All
I have SQL table like below

Rack Cell Qty
A1 X 20
A2 Y 30
A3 Z 40
B1 X 59
B2 Z 60
C1 X 70

Now my question is how I can display the table like below

X Y Z
A1
A2
A3
B1
B2
C1

In original table X, Y, Z is more then one time but new table it shows only one time each

Thanks to All

IndrajitDasgupta

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-01 : 06:52:58
select Rack
from tbl
where cell in ('X','Y','Z')
group by Rack

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Indrajit
Starting Member

11 Posts

Posted - 2012-08-01 : 07:49:38
quote:
Originally posted by Indrajit

Hi All
I have SQL table like below

Rack Cell Qty
A1 X 20
A2 Y 30
A3 Z 40
B1 X 59
B2 Z 60
C1 X 70

Now my question is how I can display the table like below

X Y Z
A1
A2
A3
B1
B2
C1

In original table X, Y, Z is more then one time but new table it shows only one time each

Thanks to All

IndrajitDasgupta



IndrajitDasgupta
Go to Top of Page

Indrajit
Starting Member

11 Posts

Posted - 2012-08-01 : 07:52:43
Thanks for but it is not coming as I required
I required like below
X Y Z
A1
A2
A3
B1
B2
C1
table should show like that

IndrajitDasgupta
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-01 : 08:08:14
quote:
Originally posted by Indrajit

Thanks for but it is not coming as I required
I required like below
X Y Z
A1
A2
A3
B1
B2
C1
table should show like that

IndrajitDasgupta



what is X Y Z ? Is it column ?
A1, A2, A3, B1, B2, C1 is all under column X ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-01 : 09:03:55
Sorry

select X=Rack, Y='', Z=''
from tbl
where cell in ('X','Y','Z')
group by Rack

or maybe

select [X Y Z] = Rack
from tbl
where cell in ('X','Y','Z')
group by Rack


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-01 : 09:08:23
i have a feeling OP want a pivoted result


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-08-01 : 14:34:17
Try this:

[CODE]
SELECT * FROM
(SELECT [Cell], [Rack] FROM YourTableName) AS SourceTable
PIVOT (max([Rack]) FOR [Cell] IN ([x], [y], [z])) AS P1;
[/CODE]
Go to Top of Page
   

- Advertisement -