August 18, 2008 at 3:30 pm
Hi:
I'd like to pivot table data represented "horizontally" into data sorted "vertically," like so. Consider the following table:
[font="Courier New"]Key Quantity1 Quantity2 Quantity3
-------------------------------------------
1 10 5 25
2 30 15 20
3 40 35 5[/font]
I'd like to return the data as a two-column result set sorted by Quantity ASC. Here's what the output looks like:
[font="Courier New"]Key Quantity
----------------
1 5
3 5
1 10
2 15
2 20
1 25
2 30
3 35
3 40[/font]
Can anyone provide the T-SQL syntax that would produce this result? Thanks.
August 18, 2008 at 4:34 pm
Try this:
Select Key, Quantity1 as [Quantity] From tbl
Union All Select Key, Quantity2 as [Quantity] From tbl
Union All Select Key, Quantity3 as [Quantity] From tbl
Order by Quantity
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
August 18, 2008 at 4:47 pm
Crude, but effective. Thanks!
August 18, 2008 at 5:04 pm
Glad I could help.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply