Groups | Blog | Home
all groups > sql server programming > april 2005 >

sql server programming : How do you produce multiple columns based on multiple rows?


sydney.luu NO[at]SPAM gmail.com
4/14/2005 6:53:30 PM
Hi,

If I have a table with the following structure/data:

CommentID Comment
---------------------
1 YELLOW
2 BLACK
3 RED
---------------------

I want to produce resultset that looks like this in a SELECT
statement (3 header columns and 1 row of data).

Col1 Col2 Col3
------------------
YELLOW BLACK RED


Thanks in advance!
Sydney Luu
oj
4/14/2005 7:18:39 PM
you can search the archive for 'crosstab' if you have some fancy report.
for this particular one, this should do:

select col1=max(case commentid when 1 then comment end),
col2=max(case commentid when 2 then comment end),
col3=max(case commentid when 2 then comment end)
from tb
--group by pkid

--
-oj


[quoted text, click to view]

Simic Marko
4/14/2005 7:20:02 PM
This article may give an idea how to do that
http://www.sqlteam.com/item.asp?ItemID=2955


[quoted text, click to view]
Adam Machanic
4/14/2005 10:13:11 PM
http://www.aspfaq.com/show.asp?id=2462

--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--


[quoted text, click to view]

AddThis Social Bookmark Button