August 25, 2014 at 7:23 am
Steve Jones - SSC Editor (8/24/2014)
Added "functions" to the QotD, though I think you're being a bit pedantic, Tom.
So do I π
Tom
August 25, 2014 at 8:31 am
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
Lots of people come to the forums asking for help with pivot functions. I usually suggest them to use the crosstab approach. π
August 25, 2014 at 8:36 am
I was making this a lot harder than it needed to be, until I remembered this part of PIVOT:
..
FOR
[<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
... [last pivoted column])
...
Since PIVOT uses data from the row to generate the column names, that left me with three options:
1. PIVOT is a simple function with a 1:1 mapping (one column for every distinct value, meaning one value and therefore one aggregate function :Whistling: )
2. PIVOT allows for the creation of large numbers of columns with duplicate names, or
3. PIVOT uses a complex algorithm to generate column headers for each aggregated column to ensure uniqueness.
Now, "Microsoft" and "complex" often go hand in hand, so #3 was tempting, but I have used PIVOT before, so I knew that wasn't the case.
Duplicating column names is possible, but leads to a mess where the output essentially can't be used in any further queries, so #2 was out.
That left #1, or 1:1 or one.
August 25, 2014 at 10:02 am
This was removed by the editor as SPAM
August 25, 2014 at 1:31 pm
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I like PIVOT because you can use it to do a dynamic number of columns on the back end.
August 25, 2014 at 1:54 pm
jshahan (8/25/2014)
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I like PIVOT because you can use it to do a dynamic number of columns on the back end.
You can do that with cross tabs as well. π
August 25, 2014 at 2:52 pm
Luis Cazares (8/25/2014)
jshahan (8/25/2014)
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I like PIVOT because you can use it to do a dynamic number of columns on the back end.
You can do that with cross tabs as well. π
Hi Luis. It's been a long time since I wrote a cross tab (sql 2005, I think) and it seems like there were significant restrictions on the types of aggregates you could use. Like only being able to use SUM or something. I just tried it on 2008 R2 and it seems like everything but COUNT DISTINCT is working. Maybe I should revisit cross tabs.
August 25, 2014 at 3:05 pm
Can't believe I got that wrong.
Thanks for the question, though.
---------------
Mel. π
August 25, 2014 at 4:08 pm
jshahan (8/25/2014)
Luis Cazares (8/25/2014)
jshahan (8/25/2014)
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I like PIVOT because you can use it to do a dynamic number of columns on the back end.
You can do that with cross tabs as well. π
Hi Luis. It's been a long time since I wrote a cross tab (sql 2005, I think) and it seems like there were significant restrictions on the types of aggregates you could use. Like only being able to use SUM or something. I just tried it on 2008 R2 and it seems like everything but COUNT DISTINCT is working. Maybe I should revisit cross tabs.
I've never found any problems with the different aggregates on cross tabs. I just tried COUNT DISTINCT and it works fine.
I've also tested and when you need more than a single aggregate, either multiple columns or multiple functions, the cross tabs will outperform the PIVOT function.
August 25, 2014 at 9:11 pm
Jeff Moden does a good job of showing performance comparisons
August 26, 2014 at 1:20 am
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I used it for the first time the other day. It was the best way of turning columns of day of the week bit flags in to rows. I didn't use an aggregate though. I'm so pleased with something else I did with it I want to share it with the world! π
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
August 26, 2014 at 2:59 am
jshahan (8/25/2014)
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I like PIVOT because you can use it to do a dynamic number of columns on the back end.
I had tended to use the cross tab approach, recently (well, last 5 or 6 years) switched back to pivots. Mainly I guess because the DBA at our office showed me a decent way to accomplish what I needed and I have followed that ever since. I would generally use this in circumstances where I do have a dynamic column set (meaning use of ADO for programmatic retrieval, dev types) and while both approaches perform similarly in my experience since I properly grokked the PIVOT way it's just seemed slightly more elegant.
August 26, 2014 at 4:25 am
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I use both, but the company I worked for has used it frequently before I came on board.
Personally, I use PIVOT statements more when pivoting data horizontally. It's easier to write for me and the way I format a PIVOT statement is easier to read versus a CASE statement.
But, when it comes to 100 million rows, I stick to CASE for performance.
August 26, 2014 at 4:28 am
Luis Cazares (8/25/2014)
jshahan (8/25/2014)
sestell1 (8/25/2014)
Does anyone actually use the PIVOT operator?I find it so limited that I almost always roll my own using GROUP BY and CASE.
I like PIVOT because you can use it to do a dynamic number of columns on the back end.
You can do that with cross tabs as well. π
Any good example with Cross tab query vs PIVOT?
Thanks
August 26, 2014 at 4:48 am
Eirikur Eiriksson (8/24/2014)
Since you wrote such a long post Tom, I guess you really got it wrong π
π
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
Viewing 15 posts - 16 through 30 (of 32 total)
You must be logged in to reply to this topic. Login to reply