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


Pivot table for Microsoft SQL Server


Pivot table for Microsoft SQL Server

Author
Message
SwePeso
SwePeso
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16645 Visits: 3433

You're welcome!

Now I have to look up "arduous" in my dictionary...




N 56°04'39.16"
E 12°55'05.25"
R. M. Joseph
R. M. Joseph
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 44

Thanks Peter. It is very good procedure and I shall use it.

R. M. Joseph


silvio costa
silvio costa
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 117
You got my Excellent! I've seen quite a few procedures, but I really appreciate your methodology: you paved a complete road from the ground up with concise and adequate explanations. I shall use it as a solid base for my pivot table reports. Thanks a lot.
Silvio E. Costa

SwePeso
SwePeso
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16645 Visits: 3433

You are welcome!

I am satisfied you liked the article. I really struggled to keep it simple and consise. And as you write, my purpose was to give away the base of what pivot tables really are.




N 56°04'39.16"
E 12°55'05.25"
Simeon Herbert
Simeon Herbert
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 7

Fantastic Article,

I now need to somehow work out to add ordering based on these "unknown" columns but this is a great start to stuff I really did not understand.

A Huge help - thanks.


Sharon Matyk
Sharon Matyk
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 47
Hi - based on the dates of the other comments, I'm coming to the party a little late. But thanks for a very clearly-explained (and with two examples) piece of code. I too have wondered - I've been on SQL since v6.x - how to do a crosstab when the column number is variable. I'll be interested to see PIVOT and UNPIVOT on 2005.
SwePeso
SwePeso
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16645 Visits: 3433

The article was re-released today, so don't take it personal

About PIVOT and UNPIVOT in SQL Server 2005; I have more use for UNPIVOT than PIVOT...




N 56°04'39.16"
E 12°55'05.25"
Marc Brooks
Marc Brooks
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 19

You could update the table creation to get the datatype of temporary table directly from the source instead of the hard-coded types/sizes. This would be easily done with:

SELECT RowText, ColumnText, Value FROM Source WHERE 1=0 INTO #Aggregates

Which works because the 1=0 clause insures no rows are matched, but the datatypes are set according to the soruce...





SwePeso
SwePeso
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16645 Visits: 3433

1) Why would I? Then you have no control over the datatype for the aggregated column VALUE (sum).
2) The syntax is wrong. It should be SELECT .. INTO...FROM ... WHERE ...




N 56°04'39.16"
E 12°55'05.25"
rukmang rege
rukmang rege
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 14

Thanks for the post.

Very simple,perfect.Would like to see similar post from you


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