Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL PIVOT Functions Expand / Collapse
Author
Message
Posted Monday, August 18, 2014 4:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 17, 2014 3:53 AM
Points: 177, Visits: 419
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.
Post #1604337
Posted Tuesday, August 19, 2014 5:57 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 6:40 PM
Points: 421, Visits: 1,000
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





Post #1605250
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse