Saturday, January 31, 2015

Join Tables to Create the Summary View

Try PIVOT or Dynamic PIVOT and UNION the 3 result set. Below is an example of using PIVOT to display summary of payment to supplier.



SELECT * FROM (
SELECT [Year]
,[Month]
,[PROP_CODE]
,[SupplierCode]
,isnull(TotalPayment,0) as [TotalPayment]
,[ControlGroup]
,'Supplier Payment' as [Supplier Payment]
FROM [SQL2012EELearning].[dbo].[SupplierPayment]
) X
PIVOT
( SUM(TotalPayment) FOR [Month] IN (January,February,March,April,May,June,July,August,September,November,December)) pvt

You can append year and month to form a column. i.e FOR [Month] IN (January_2014,February_2014.....)




Regards, RSingh




No comments:

Post a Comment