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 |
|
lec7grs
Starting Member
2 Posts |
Posted - 2012-07-27 : 06:14:24
|
| Hi there,Here is a simple example of something I am trying to figure out in MS SQL.I have a table with the following data:QID RESP1 a1 b1 c1 d2 e2 f2 g2 h3 i3 j3 k3 lAnd I would like it to look like this:1 2 3a e ib f jc g kd h l With the distinct values in the QID column in the first table becoming the column headers in the new table.I've tried various things with PIVOT but cannot get all the data retained in the output view.Any help would be appreciated!Thankslec7grs |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-07-27 : 06:20:39
|
[code]select [1], [2], [3]from ( select QID, RESP, rn = row_number() over (partition by QID order by RESP) from yourtable ) t pivot ( max(RESP) for QID in ([1], [2], [3]) ) p[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lec7grs
Starting Member
2 Posts |
Posted - 2012-07-27 : 07:02:06
|
| Many thanks for your response - that works perfectly.Now, assuming QID has a finite but unknown distinct list of values - say ~1500 is there any way of making the selection dynamic?ThanksGraham |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-07-28 : 13:50:00
|
the whole bandwidth + computation power balance has shifted since mainframe days. This simply isn't a task you should be asking a database server to do now....Simply order the output list by the keys and then pivot them in your application layer. it's got *much* more room to scale.PIVOT is my least favourite operator.Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
 |
|
|
|
|
|
|
|