The PIVOT is one of options for you. You can try with below before applying the dynamic PIVOT
WITH Months([Month] ,MonthOrder) As( SELECT '2015-07',1 UNION SELECT '2015-08',2 UNION SELECT '2015-09',3 UNION SELECT '2015-10',4 UNION SELECT '2015-11',5 UNION SELECT '2015-12',6 UNION SELECT '2016-01',7 UNION SELECT '2016-02',8 UNION SELECT '2016-03',9 UNION SELECT '2016-04',10 UNION SELECT '2016-05',11 UNION SELECT '2016-06',12 UNION SELECT '2016-07',13),Sales(CustomerID,[Month],SalesAmount) AS( SELECT '0001','2015-07',120.11 UNION SELECT '0001','2015-09',190.34 UNION SELECT '0001','2015-10',100.10 UNION SELECT '0001','2016-02',140.05 UNION SELECT '0002','2015-09',150.05 ),Report(CustomerID,[Month],SalesAmount) AS( SELECT tmp.CustomerID,tmp.Month,ISNULL(s.SalesAmount,0.00) As SalesAmount FROM ( SELECT s.CustomerID,m.Month,0.00 SalesAMount FROM (SELECT DISTINCT CustomerID FROM Sales) s CROSS JOIN Months m ) tmp LEFT JOIN Sales s ON tmp.CustomerID = s.CustomerID AND tmp.Month = s.Month)SELECT [CustomerID], [2015-07],[2015-08],[2015-09],[2015-10],[2015-11],[2015-12],[2016-01], [2016-02],[2016-03],[2016-04],[2016-05],[2016-06],[2016-07]FROM Report r PIVOT ( SUM(r.SalesAmount) FOR r.Month IN ([2015-07],[2015-08],[2015-09],[2015-10],[2015-11],[2015-12],[2016-01], [2016-02],[2016-03],[2016-04],[2016-05],[2016-06],[2016-07]) ) AS pv;
Open in new window