SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Not sure how to transpose a table like this...


Not sure how to transpose a table like this...

Author
Message
xusword
xusword
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 5
Let's assume I have a table like this

SELECT * FROM Points

PointID Axis Value
P1 X 10
P1 Y 20
P2 X 30
P2 Y 40


I was wondering what query can I write that can return the following

PointID X Y
P1 10 20
P2 30 40

Any suggestions?
thanks
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8753 Visits: 7660
The keywords you want to use to search for are either PIVOT or CROSSTAB. Either method will work for you.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
xusword
xusword
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 5
Thanks
The query would be

select PointID, X, Y
from Points
pivot
(
max(Value)
for Axis in (X, Y)
)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88948 Visits: 41136
In many cases, people who have these types of Pivots also eventually want a "Total" column. The ancient "Black Art" of CROSSTABs easily allows for such a thing.


SELECT PointID
,X = SUM(CASE WHEN Axis = 'X' THEN Value ELSE '' END)
,Y = SUM(CASE WHEN Axis = 'Y' THEN Value ELSE '' END)
,Total = SUM(Value)
FROM cteTestData
GROUP BY PointID
;



Result Set:

PointID X Y Total
------- ----------- ----------- -----------
P1 10 20 30
P2 30 40 70



CROSSTABs also make it pretty easy to do other things. For example...

SELECT PointID = CASE WHEN GROUPING(PointID) = 0 THEN PointID ELSE 'Total' END
,X = SUM(CASE WHEN Axis = 'X' THEN Value ELSE '' END)
,Y = SUM(CASE WHEN Axis = 'Y' THEN Value ELSE '' END)
,Total = SUM(Value)
FROM cteTestData
GROUP BY PointID WITH ROLLUP
;




Result Set:

PointID X Y Total
------- ----------- ----------- -----------
P1 10 20 30
P2 30 40 70
Total 40 60 100




The following article also explains how to do multiple column types in Pivots and CROSSTABs, explains the performance advantages of "pre-aggregation", along with a demonstration that pre-aggregatedCROSSTABs are frequently almost twice as fast as pre-aggregated Pivots. It can really make a difference in being able to meet and beat reporting SLAs. The second link demonstrates the simple power of dynamic CROSSTABs for things like perpetual sliding window reports with no code change based on GETDATE() alone.
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/

To be honest, I don't ever use Pivot. I always use CROSSTABs, instead, for all the reasons previously stated and for their ease in readability.

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search