I have the query below:
- SELECT sum(c.ConvertedNetPremium + c.ConvertedVAT + c.ConvertedRegistryFee) as Due
- from dbo.CashierSalesDetails c
- select sum(a.AmountPaid) as payment
- from dbo.tblAgentPaymentMonitoring a
My requirement is that, I should be able to deduct the "payment" from the "Due". below is the actual data of both tables
I tried this code but the answer is wrong:
- SELECT a.TaxInvoiceNumber, sum(a.AmountPaid) as payment, sum(c.ConvertedNetPremium + c.ConvertedVAT + c.ConvertedRegistryFee) as Due,
- sum(a.AmountPaid) - sum(c.ConvertedNetPremium + c.ConvertedVAT + c.ConvertedRegistryFee) as BAlance
- FROM dbo.tblAgentPaymentMonitoring a INNER JOIN
- dbo.CashierSalesDetails c ON a.TaxInvoiceNumber = c.TaxInvoiceNumber
- GROUP BY a.TaxInvoiceNumber



Nitin SontakkeRank #264 · 9.6K reputationPosted Mar 21, 2017, 10:02 PM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Mar 21, 2017, 3:08 AM EDT
Jes SieRank #1,285 · 1.3K reputationPosted Mar 21, 2017, 2:29 AM EDT
Nitin Sontakke the joining column is TaxInvoiceNumber. Below is the script:
Nitin SontakkeRank #264 · 9.6K reputationPosted Mar 21, 2017, 2:25 AM EDT
Jes SieRank #1,285 · 1.3K reputationPosted Mar 21, 2017, 9:31 PM EDT
Jes SieRank #1,285 · 1.3K reputationPosted Mar 22, 2017, 8:32 PM EDT
Nitin Sontakke Thank you so much! Now I understand. May I ask why in the where clause you use 1 = 1?
Nitin SontakkeRank #264 · 9.6K reputationPosted Mar 22, 2017, 8:41 PM EDT