Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help on Pivot Expand / Collapse
Author
Message
Posted Wednesday, June 06, 2012 1:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 10, 2013 2:57 AM
Points: 6, Visits: 64
Hi All,

I have a scenario where in i have to convert rows to columns dynamically, im trying but not able to get the exact result

The data is as follows

cusid name email
111 abc abc@xyz.com
222 aaa aaa@xyz.com
222 aaa aaa1@xyz.com
222 aaa aaa2@xyz.com
222 aaa aaa3@xyz.com
333 bbb bbb@xyz.com
333 bbb bbb1@xyz.com

I need to get the output as follows

cusid name email1 email2 email3 email4
111 abc abc@xyz.com null null null
222 aaa aaa@xyz.com aaa1@xyz.com aaa2@xyz.com aaa3@xyz.com
333 bbb bbb@xyz.com bbb1@xyz.com null null

Thanks
Post #1311626
Posted Wednesday, June 06, 2012 1:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Take a look in my signature for the Cross Tabs links by Jeff, the second part is in relation to dynamic cross tabs.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1311628
Posted Wednesday, June 06, 2012 3:36 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
Here are a few versions of doing it:

--Creating Table

Create Table Ex
(cusid int,
name varchar(3),
email varchar(20) )


--Inserting Sample Data

Insert Into Ex
Select 111, 'abc', 'abc@xyz.com'
union ALL
Select 222, 'aaa', 'aaa@xyz.com'
union ALL
Select 222, 'aaa', 'aaa1@xyz.com'
union ALL
Select 222, 'aaa', 'aaa2@xyz.com'
union ALL
Select 222, 'aaa', 'aaa3@xyz.com'
union ALL
Select 333, 'bbb', 'bbb@xyz.com'
union ALL
Select 333, 'bbb', 'bbb1@xyz.com'


--Query Using Case

Select cusid, MAX(Name),
Max(Case When rn = 1 Then email Else '' End) As Email1,
Max(Case When rn = 2 Then email Else '' End) As Email2,
Max(Case When rn = 3 Then email Else '' End) As Email3,
Max(Case When rn = 4 Then email Else '' End) As Email4
From
(Select *, ROW_NUMBER() Over (Partition By cusid Order By (Select NULL)) As rn From Ex) As a
Group By cusid


--Static Pivot

Select cusid, name, [Email1], [Email2] , [Email3] , [Email4]
From
(Select *, 'Email' + Cast(ROW_NUMBER() Over (Partition By cusid Order By (Select NULL)) As varchar) As rn From Ex) As a
Pivot
(max(Email) For rn IN ([Email1],[Email2],[Email3],[Email4])) As pvt
Order By cusid


--Dynamic Pivot

Declare @cols varchar(max), @sql varchar(max)
Declare @temp Table(Cols varchar(max))
Insert Into @temp
Select Distinct 'Email' + Cast(ROW_NUMBER() Over (Partition By cusid Order By (Select NULL)) As varchar) As rn From Ex
Select @cols = Coalesce(@cols + ', ', '') + QUOTENAME(cols) From @temp
Set @sql = 'Select cusid, name, '+@cols+'
From
(Select *, ''Email'' + Cast(ROW_NUMBER() Over (Partition By cusid Order By (Select NULL)) As varchar) As rn From Ex) As a
Pivot
(max(Email) For rn IN ('+@cols+')) As pvt
Order By cusid'
Execute (@sql)



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1311712
Posted Thursday, June 07, 2012 12:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 10, 2013 2:57 AM
Points: 6, Visits: 64
Hi,

Had done the same way but did not use "partition by" so was not able to get the exact output

Thanks for the reply... It helped me and solved it :)



Post #1312314
Posted Thursday, June 07, 2012 5:16 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
manzilkolur23 (6/7/2012)
Hi,

Had done the same way but did not use "partition by" so was not able to get the exact output

Thanks for the reply... It helped me and solved it :)





You're welcome. I am glad it was helpful for you.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1312393
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse