Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a questionI have a table that contains promotions that someone receivedID Promo-- -----1 a1 c1 d2 b2 c2 dand i would like an output table to come as ID a b c d-- - - - -1 1 0 1 12 0 1 1 1any help would be greatly appreciated i'd like to find a very efficient way to do this without writing a ton of update statements to do sowhile (I != Understand){KickMe.Hard;}
yosiasz
Master Smack Fu Yak Hacker
1635 Posts
Posted - 2009-12-16 : 17:05:05
have you looked into the PIVOT command<><><><><><><><><><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion
jmpierce30
Starting Member
7 Posts
Posted - 2009-12-16 : 17:09:43
I know that it exists, but I have not used it before. Could anyone provide a simple example that works with this dataset so that I can learn?Thanks in advance for your help :-)while (I != Understand){KickMe.Hard;}
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2009-12-16 : 17:31:54
This should be a start..
declare @t table (ID int, promo varchar(1))insert @tselect 1, 'a'union all select 1,'c'union all select 1, 'd'union all select 2, 'b'union all select 2, 'c'union all select 2, 'd'
select ID,[a],[b],[c],[d] from(select ID,promo from @t) pPIVOT(COUNT(promo) FOR promo IN ([a],[b],[c],[d])) as pvt
yosiasz
Master Smack Fu Yak Hacker
1635 Posts
Posted - 2009-12-16 : 18:05:46
vijayisonly is on fire today folks, I don't know what he ate or what he drank..I will have what he is having or whatever he had.and if you want to get fancy you can use one of those dynamic PIVOT functions like so (this is helpful for when the values of proo are dynamic)exec usp_pivot 'dbo', 'PivotTest', 'ID', 'promo','COUNT', 'promo'<><><><><><><><><><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion