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.
Hi allI am trying to combine multiple records in to one record ideally through a view if its possible? The fields ConsumerId and CampaignId would be used to group by then all the PinCode associated to the consumer and campaign would be in the same row.So I have...ConsumerId | CampaignId | PinCode1 | 10 | 12341 | 10 | 23452 | 15 | 3452 | 15 | 5462 | 56 | 2345and would like to output ConsumerId | CampaignId | Pin1 | Pin2 | Pin3 | Pin41 | 10 | 1234 | 2345 | | 2 | 15 | 345 | 546 | |2 | 56 | 2345 | | |They'll only ever be 4 pins linked to a consumer and campaignI'm on sql 2008 - I've tried looking in to pivot tables but haven't been able to get it to do what I'm looking forthanks,Ray
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2010-05-26 : 11:26:57
Try this
select ConsumerId, CampaignId, max(case when seq = 1 then PinCode else null end) as Pin1, max(case when seq = 2 then PinCode else null end) as Pin2, max(case when seq = 3 then PinCode else null end) as Pin3, max(case when seq = 4 then PinCode else null end) as Pin4from(select row_number() over(partition by ConsumerId,CampaignId order by PinCode) as seq, * from ConsumerTable ) tgroup by ConsumerId,CampaignId