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


SQL PIVOT Functions


SQL PIVOT Functions

Author
Message
SteveEClarke
SteveEClarke
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1233 Visits: 457
I have a financial department requirement, where they require various data grouped by Year/Month.

I have done this by using PIVOT - but the goal posts have now changed and they only want to see the previous 3 months along with lasts years data as a comparison.

I have the following script ;

========================
SELECT
TrnYear,
[01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12]

FROM

(
SELECT
ROW_NUMBER() OVER ( ORDER BY TrnYear DESC, right('00'+ rtrim(TrnMonth), 2) DESC ) as Row,
TrnYear,
(right('00'+ rtrim(TrnMonth), 2) ) as TrnMonth,
Count( Invoice ) as InvoiceCount

FROM ApInvoicePay

Where
TrnYear >= Year( GetDate()) -2

Group by TrnYear, TrnMonth

) p


PIVOT
(
Sum(InvoiceCount)
FOR TrnMonth IN
( [01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12])
) AS Pvt


where Row in ( 1,2,3,12,13,14,15 )

================================

from the above I get the the following output ;

================================
TrnYear 01 02 03 04 05 06 07 08 09 10 11 12
2013 NULL NULL NULL NULL NULL NULL NULL NULL 652 NULL NULL NULL
2013 NULL NULL NULL NULL NULL NULL NULL 556 NULL NULL NULL NULL
2013 NULL NULL NULL NULL NULL NULL 679 NULL NULL NULL NULL NULL
2013 NULL NULL NULL NULL NULL 485 NULL NULL NULL NULL NULL NULL
2014 NULL NULL NULL NULL NULL NULL NULL 331 NULL NULL NULL NULL
2014 NULL NULL NULL NULL NULL NULL 488 NULL NULL NULL NULL NULL
2014 NULL NULL NULL NULL NULL 560 NULL NULL NULL NULL NULL NULL

================================

what I would like to see is the TrnYear only ONCE ..... am I missing one simple piece of grouping code ?

Thanks

________________________________________________________________________________________________
Regards
Steve
SQL 2008 DBA/DBD - MCTS/MCITP

Please don't trust me, test the solutions I give you before using them.
MMartin1
MMartin1
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6813 Visits: 2033
Hi,
Since I don't have sample data to work with I took the AdventurWorks database as my foundation. I am not totally clear how your data looks like but below is what I was able to devise.. maybe it can help you with your code .



select * from
(
select YEAR(ORderDATE) as Year_, MONTH(OrderDate) AS Month_pivot,SalesOrderID
FROM Sales.SalesOrderHeader


) as t
pivot
(
Count(salesOrderID) for month_pivot in ([01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12])
) as p







----------------------------------------------------
How to post forum questions to get the best help
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