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 ««12345»»»

Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Expand / Collapse
Author
Message
Posted Wednesday, December 3, 2008 8:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 1:43 AM
Points: 6,890, Visits: 14,254
Top work, Jeff. You have a rare talent for mixing informal narrative with a technically challenging subject - and with ruthless precision

Cheers

ChrisM


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #612909
Posted Wednesday, December 3, 2008 8:53 AM


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: Tuesday, September 30, 2014 7:42 AM
Points: 3,688, Visits: 72,435
That was just an amazing read... learned some neat tricks that I didn't even think were possible (but in retrospect should have known)... very informative...





--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
Post #612921
Posted Wednesday, December 3, 2008 9:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:24 PM
Points: 1, Visits: 11
Jeff,

Whoa, using SQL to do cross-tabs really is "old school" (circa mid-90's or before.) It just wasn't designed for this.

Do yourself a BIG favor and try Analysis Services. The MDX language is everything the SQL "select-groupby" ever wanted to be when it grew up. And it's far more expressive in terms of business reporting than SQL will ever be.

Mark Landry
Tampa, FL
Post #612951
Posted Wednesday, December 3, 2008 9:41 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
mlandry (12/3/2008)
Jeff,

Whoa, using SQL to do cross-tabs really is "old school" (circa mid-90's or before.) It just wasn't designed for this.

Do yourself a BIG favor and try Analysis Services. The MDX language is everything the SQL "select-groupby" ever wanted to be when it grew up. And it's far more expressive in terms of business reporting than SQL will ever be.

Mark Landry
Tampa, FL


Thanks, Mark... yep... I absolutely agree with everything you said. But, it is a bit more difficult to setup Analysis Services and learn the MDX language than it is to learn how to do a simple cross-tab. Lot's of folks/shops just won't go through it. Not saying that's right or wrong, but simply a fact based on the number of requests for help on cross-tabs on these forums in the last 12 months or so. I figured that if they're going to write a cross tab, they might as well learn how to do it without a cursor or While loop. :)

It just wasn't designed for this.


Heh... T-SQL wasn't designed for most of what I do with it... :P


--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 #612980
Posted Wednesday, December 3, 2008 10:04 AM


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: Tuesday, September 30, 2014 7:42 AM
Points: 3,688, Visits: 72,435
My favorite way to do cross-tabs is to cheat..

Put the data into a cross tab friend format (measures and values to break it out by)

and feed it into crystal reports.




--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
Post #612999
Posted Wednesday, December 3, 2008 10:08 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, August 10, 2012 6:08 PM
Points: 1,156, Visits: 801
Thank you for your efforts here Jeff. An article of this level takes hours to prepare, and the effort shows. Although I am an Analysis Server and Report Server convert, I still think this kind of TSQL workup is useful for instruction on many levels, and is the type of project that demonstrates to the less informed that server-side TSQL is far more than just CRUD (A.K.A. READ or SUID). I love dynamic code projects. I dislike static hard-coded unyeiding inflexible and dead-when-deployed code.

You are very spot-on when you say TSQL allows you to do many things not originally designed (but powerful and useful just the same).
Post #613004
Posted Wednesday, December 3, 2008 10:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:58 AM
Points: 5,370, Visits: 9,010
Jeff Moden (12/3/2008)
Heh... T-SQL wasn't designed for most of what I do with it... :P

Isn't that the truth.
Fantastic article! Thanks!
Wayne


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #613007
Posted Wednesday, December 3, 2008 10:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 6, 2014 10:36 AM
Points: 104, Visits: 98
I've had better luck writing code that writes my SQL. I wrote a set of code that you point at any table, it accepts any column within the table as the top or left portion of the crosstab and any numeric column as the data portion. Another option is to calculate as percent of the whole or sum or count.

Doing it that way, you have one set of reusable code for all crosstabs...works pretty well. I usually just create a simple view, point the object at the view and Voila have instant reconfigurable, groupable by anything crosstab. I even added an option to subgroup on the left side.

Same concept, more code up front, but infinitely reusable.



Post #613023
Posted Wednesday, December 3, 2008 11:00 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, December 4, 2014 7:52 AM
Points: 9,294, Visits: 9,495
Great article, Jeff. Like The Return of the King, it was well worth the wait!
:)


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #613035
Posted Wednesday, December 3, 2008 11:04 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, December 4, 2014 7:52 AM
Points: 9,294, Visits: 9,495
mlandry (12/3/2008)
Whoa, using SQL to do cross-tabs really is "old school" (circa mid-90's or before.) It just wasn't designed for this.

Do yourself a BIG favor and try Analysis Services. The MDX language is everything the SQL "select-groupby" ever wanted to be when it grew up. And it's far more expressive in terms of business reporting than SQL will ever be.


I am sure that a similar article on how to use Analysis Services/MDX to do this for those of us who are T-SQL wonks only, would be very well received.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #613038
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse