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
 creating column names from rows

Author  Topic 

insanepaul
Posting Yak Master

178 Posts

Posted - 2010-05-28 : 08:11:13
Hi,

I just need to know how to convert row names into columns eg.Does this make sense?

Data
customer...id
James.........1
paul..........1
Paul..........2
James.........3

output:
id james paul

1..........1..........1
2..........0..........1
3..........1..........0

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-05-28 : 08:23:37
select id,case when customer = 'james' then 1 else 0 end as james
,case when customer = 'paul' then 1 else 0 end as paul
from tablename
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2010-05-28 : 09:57:32
quote:
Originally posted by bklr

select id,case when customer = 'james' then 1 else 0 end as james
,case when customer = 'paul' then 1 else 0 end as paul
from tablename



Excellent...just what I wanted thanks!!!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-28 : 10:49:01
For unknown number of customers, you may need to use dynamic cross-tab
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -