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

Pivot And Unpivot Table Expand / Collapse
Author
Message
Posted Friday, August 3, 2012 5:07 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, September 11, 2014 9:23 PM
Points: 262, Visits: 647
Hi,
I am a newbie in sql server.I have came across various article on internet which shows how to implement pivot and un-pivot table. Since there are various gurus of sql server in this forum, i would like to know from them wheather they had used this feature in any of there project. And even though if you had used the above feature what would have being the alternate solution to the problem if you had being not using this feature.
Post #1339734
Posted Friday, August 3, 2012 5:22 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, June 27, 2014 4:17 AM
Points: 75, Visits: 443
When you asking about alternate solution you, do mean funtctional equivalent? you may use case+group by instead of pivot, and cross apply instead of unpivot. You may also check out these article about unpivot.


I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
Post #1339740
Posted Friday, August 3, 2012 6:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, September 11, 2014 9:23 PM
Points: 262, Visits: 647
yeah i was expecting functional requriment. But again the new question that comes to mind is if we do have an alternate solution then why unpivot and pivot.

Since i am new to SQL Server just curios to know it
Post #1339778
Posted Friday, August 3, 2012 6:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 1:10 PM
Points: 4, Visits: 118
In the past I have used PIVOT/ UNPIVOT along side dynamic SQL mainly in ETL scripts.

For example I was importing data that was stored in columns and needed it in a single row in the target DB.
Post #1339784
Posted Saturday, August 4, 2012 4:13 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 11:44 AM
Points: 1,127, Visits: 1,592
Using Pivot/unpivot could be more than just a matter of choice. You can definitely avoid the Pivot with Case+Group but then you come across situations where you can't use Case+Group. This could be because the columns are not Static and you need to get the Columns Dynamically.

In these situations you can't avoid the Pivot and have to go with a Dynamic CrossTab.

For other situations it is a matter of choice and can be avoided.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1340177
Posted Monday, August 6, 2012 7:04 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: Today @ 8:47 PM
Points: 3,648, Visits: 5,318
SomewhereSomehow (8/3/2012)
When you asking about alternate solution you, do mean funtctional equivalent? you may use case+group by instead of pivot, and cross apply instead of unpivot. You may also check out these article about unpivot.


Wow! You quoted me!

Thanks for taking the time to read my article. I was wondering who that was.



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 #1340963
Posted Monday, August 6, 2012 10:21 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 11:44 AM
Points: 1,127, Visits: 1,592
dwain.c (8/6/2012)
SomewhereSomehow (8/3/2012)
When you asking about alternate solution you, do mean funtctional equivalent? you may use case+group by instead of pivot, and cross apply instead of unpivot. You may also check out these article about unpivot.


Wow! You quoted me!

Thanks for taking the time to read my article. I was wondering who that was.


Wow!!....Don't forget me Dwain now that you have become all famous after writing that article.....lol


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1340988
Posted Monday, August 6, 2012 10:25 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: Today @ 8:47 PM
Points: 3,648, Visits: 5,318
vinu512 (8/6/2012)
dwain.c (8/6/2012)
SomewhereSomehow (8/3/2012)
When you asking about alternate solution you, do mean funtctional equivalent? you may use case+group by instead of pivot, and cross apply instead of unpivot. You may also check out these article about unpivot.


Wow! You quoted me!

Thanks for taking the time to read my article. I was wondering who that was.


Wow!!....Don't forget me Dwain now that you have become all famous after writing that article.....lol


How could I forget you Vinu, despite the fame and glory of being published! Now, all that awaits is the fortune coming my way and I'll be set.



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 #1340989
Posted Monday, August 6, 2012 10:35 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 11:44 AM
Points: 1,127, Visits: 1,592
dwain.c (8/6/2012)
vinu512 (8/6/2012)
dwain.c (8/6/2012)
SomewhereSomehow (8/3/2012)
When you asking about alternate solution you, do mean funtctional equivalent? you may use case+group by instead of pivot, and cross apply instead of unpivot. You may also check out these article about unpivot.


Wow! You quoted me!

Thanks for taking the time to read my article. I was wondering who that was.


Wow!!....Don't forget me Dwain now that you have become all famous after writing that article.....lol


How could I forget you Vinu, despite the fame and glory of being published! Now, all that awaits is the fortune coming my way and I'll be set.


All my wishes are with you Dwain....keep learning and keep writing stuff like that so that Learners like me can learn from your Experiences....All the Best!!


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1340992
Posted Monday, August 6, 2012 10:38 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: Today @ 8:47 PM
Points: 3,648, Visits: 5,318
What? You mean you read it too, Vinu?

An interesting use case just appeared here: http://www.sqlservercentral.com/Forums/Topic1339633-392-3.aspx#bm1340977

The discussion thread also sheds further light on its performance, just in case you didn't go through it. There's some more research to do, but it looks pretty good overall on performance.



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 #1340994
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse