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 ««12

Transpose/Pivot Help Expand / Collapse
Author
Message
Posted Friday, February 22, 2013 1:51 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, February 24, 2013 11:33 PM
Points: 7, Visits: 18
Jeff Moden (2/22/2013)
ajames 36824 (2/22/2013)
You ROCK!


Do you understand how and why it works?


Well it looks like it says that for each question in the QuestionID column, return the maximum numerical value related for the associated Response and making the rest blank. In order to do this, Response must be converted to characters. The Group By statement pivots all the other variables not in the statement and groups them based on those variables. Is that right?
Post #1423252
Posted Friday, February 22, 2013 7:18 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 2,341, Visits: 3,175
ajames 36824 (2/22/2013)
Jeff Moden (2/22/2013)
ajames 36824 (2/22/2013)
You ROCK!


Do you understand how and why it works?


Well it looks like it says that for each question in the QuestionID column, return the maximum numerical value related for the associated Response and making the rest blank. In order to do this, Response must be converted to characters. The Group By statement pivots all the other variables not in the statement and groups them based on those variables. Is that right?


Not sure I'd say it quite like that.

The GROUP BY collapses all rows where the value of the grouped columns is the same, thus the rest of the columns are free to be pivoted. Which is done by one MAX/CASE per column to be pivoted.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1423310
Posted Friday, February 22, 2013 8:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906, Visits: 26,790
ajames 36824 (2/22/2013)
Jeff Moden (2/22/2013)
ajames 36824 (2/22/2013)
You ROCK!


Do you understand how and why it works?


Well it looks like it says that for each question in the QuestionID column, return the maximum numerical value related for the associated Response and making the rest blank. In order to do this, Response must be converted to characters. The Group By statement pivots all the other variables not in the statement and groups them based on those variables. Is that right?


Mostly correct. I say "mostly" because you don't actually need to convert the respose to VARCHAR. Dwaine did that just so he could show a blank instead of a NULL (which also suppresses the NULL aggregate warning messages).

If you'd like to do a deeper dive on CrossTabs and Pivots, here are a couple of articles that start you on the ground floor and move you all the way through dynamic CrossTabs.
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1423313
Posted Sunday, February 24, 2013 11:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, February 24, 2013 11:33 PM
Points: 7, Visits: 18
Thanks for the feedback & the articles.
Post #1423436
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse