I'm trying to create a query to summarize data by month and year and have stumbled upon PIVOT and have been trying that (I have SQL Server 2012). I have data that looks like this:
Supplier Date Sales
(mm-yyyy)
-------- ------- ------
A 01-2012 157.54
A 02-2012 215.43
A 03-2012 993.88
B 04-2014 85.50
B 04-2014 41.50
C 01-2012 112.22
C 05-2015 84.55
C 06-2015 1188.42
C 07-2015 445.58
Basically hundreds of suppliers with a date and a sales amount. I would like to create a summary of this data by month and year in a format like this:
Supplier Month 2012 2013 2014 2015
-------- ----- ---- ---- ---- ----
A 01 100.51 155.96 15.10 699.66
A 02 54.89 155.55 0 144.52
A 03 11.53 488.99 419.98 155.21
A 04
A 05
A 06
A 07
A 08
A 09
A 10
A 11
A 12
B 01
B 02
Each supplier will have 12 rows, one for each month, and then the years are summarized in columns.
Can this be done with a PIVOT or is there a better way? I could do this another way that is much more painful, but based on seeing a number of PIVOT questions here, like this one: Convert Rows to columns using 'Pivot' in SQL Server, I thought it might be done easily.