Saturday, January 31, 2015

Join Tables to Create the Summary View

You need to UNION in the below way. Please note the number of Select columns in each query.



SELECT [SupplierCode]
,[Prop Code]
,'Rent Received' AS [FEE TYPE]
,[TotalPayment] AS [AMOUNT]
,[Year]
,[Month]
FROM [SQL2012EELearning].[dbo].[CustomerPayment]

UNION ALL

SELECT (SELECT TOP 1 [SupplierCode] FROM [SQL2012EELearning].[dbo].[CustomerPayment] A WHERE A.[PROP CODE]=B.[PROP_CODE]) AS [SupplierCode]
,[PROP_CODE] as [Prop Code]
,'Repair Cost' AS [FEE TYPE]
,[AMOUNT]
,[Year]
,[Month]
FROM [SQL2012EELearning].[dbo].[RepairCost] B

UNION ALL

SELECT [SupplierCode]
,[PROP_CODE] as [Prop Code]
,'Supplier Payment' as [FEE TYPE]
,[TotalPayment]
,[Year]
,[Month]
FROM [SQL2012EELearning].[dbo].[SupplierPayment]





Regards, RSingh


No comments:

Post a Comment