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.
Author |
Topic |
sijeffs
Starting Member
1 Post |
Posted - 2008-08-06 : 07:15:44
|
Hi All,First topic so hopefully its understandable!I need to transpose a set of values similar to belowRRH_KEY RRS_KEY363044 92105363044 92106363044 92107363044 92108363044 92109363044 92110363045 92111363045 92112363045 92113363045 92115363045 92116To give me a table that looks like belowRRH_KEY RRS_KEY_1 RRS_KEY_2 RRS_KEY_3 RRS_KEY_4 RRS_KEY_5 RRS_KEY_6363044 92105 92106 92107 92108 92109 92110363045 92111 92112 92113 92114 92115 92116I've tried multiple settings with no joy. Do I need to rank the RRS_KEYS in order to get the pivot to work?Going round in circles.Any assistance greatly appreciated.Sijeffs  |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-06 : 13:55:19
|
[code]SELECT t.RRH_KEY,SUM(CASE WHEN Seq=1 THEN RRS_KEY ELSE 0 END) AS RRS_KEY_1,SUM(CASE WHEN Seq=2 THEN RRS_KEY ELSE 0 END) AS RRS_KEY_2,SUM(CASE WHEN Seq=3 THEN RRS_KEY ELSE 0 END) AS RRS_KEY_3,SUM(CASE WHEN Seq=4 THEN RRS_KEY ELSE 0 END) AS RRS_KEY_4,SUM(CASE WHEN Seq=5 THEN RRS_KEY ELSE 0 END) AS RRS_KEY_5,SUM(CASE WHEN Seq=6 THEN RRS_KEY ELSE 0 END) AS RRS_KEY_6FROM(SELECT ROW_NUMBER() OVER (PARTITION BY RRH_KEY ORDER BY RRS_KEY) AS Seq,*FROM YourTable) tGROUP BY t.RRH_KEY [/code] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-06 : 13:56:12
|
And if your number of RRS_KEY is not certain for each RRH_KEY and you want all of them then you need to use dynamic sql. |
 |
|
|
|
|