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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 8:01 PM
Points: 3,605, Visits: 5,198
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.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
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: Today @ 8:46 AM
Points: 36,611, Visits: 31,047
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
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