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
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5333 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
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 44

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

R. M. Joseph


silvio costa
silvio costa
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 110
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
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5333 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
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 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-Enthusiastic
SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)

Group: General Forum Members
Points: 142 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
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5333 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 (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 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
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5333 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
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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