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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Column Values into rows, not Columns in to rows

Author  Topic 

kumar1248
Starting Member

20 Posts

Posted - 2010-01-21 : 13:51:41
Hi,

I have a result set like
PRCd	Ctg	Cost
=======================
AB 4578 500
CD 4578 252
EF 4578 824
XM 4578 898
AB 9579 5000
CD 9579 2520
EF 9579 8240
XM 9579 8980
.
.
.
.

and i am looking for T-sql that produce the result as



PRCd Ctg Cost AB CD EF XM
====================================================
Cost 4578 500 500 252 824 898
Cost 9579 500 5000 2520 8240 8980
.
.
.
.


Please help.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-21 : 14:05:43
This?
select 'Cost' as [PRCd]
,Ctg
,max(case when PRCd = 'AB' then Cost else null end) as [AB]
,max(case when PRCd = 'CD' then Cost else null end) as [CD]
,max(case when PRCd = 'EF' then Cost else null end) as [EF]
,max(case when PRCd = 'XM' then Cost else null end) as [XM]
from table1
group by Ctg

If [PRCd] values are dynamic , search for 'dynamic cross tabs' here.
Go to Top of Page
   

- Advertisement -