Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pivot

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2013-09-16 : 12:36:19
Is there a Pivot script I can run that will List Each month across as my columns and my rows be Category, Customer, and Year?

Under each column for the month will be total sales for that month.

Lets say this is all found in the SALES table with the following Columns.

SALES.InvoiceDate
SALES.Category
SALES.Customer
SALES.TotalSales

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2013-09-17 : 08:10:35
Don't know if this will help you as an example pivot to show dates as columns and count in cells, you can alter it to suit:

select Column A
, [01/01/2012] as '01/01/2012'
, [02/01/2012] as '02/01/2012'
, [03/01/2012] as '03/01/2012'
, [04/01/2012] as '04/01/2012'
, [05/01/2012] as '05/01/2012'
, [06/01/2012] as '06/01/2012'
, [07/01/2012] as '07/01/2012'
, [08/01/2012] as '08/01/2012'
, [09/01/2012] as '09/01/2012'
, [10/01/2012] as '10/01/2012'
, [11/01/2012] as '11/01/2012'
, [12/01/2012] as '12/01/2012'
FROM (select column A
, column B
, column C
FROM Table) ps
PIVOT
(
COUNT(Unique_Instance)
FOR Instance_Date IN ([01/01/2012]
,[02/01/2012]
,[03/01/2012]
,[04/01/2012]
,[05/01/2012]
,[06/01/2012]
,[07/01/2012]
,[08/01/2012]
,[09/01/2012]
,[10/01/2012]
,[11/01/2012]
,[12/01/2012])
) AS PVT


Go to Top of Page
   

- Advertisement -