I want to find the total outstanding amount for a particular item amount excluding the refund amount.
There will be two columns for each item based on number of times it was sold, final_amount and 2nd refund_amount for each items and I want to subtract total refund_amount from final_amount for each item.
PFB the code/query
SELECT item_id,
SUM(final_amount) as total_amount,
SUM(ISNULL(refund_amount, 0)) AS total_refund
SUM(final_amount) - SUM(ISNULL(refund_amount, 0)) AS outstanding_amount
FROM tabel1
WHERE item_id in ('119688521',
'109536343',
'99459466',
'97126817',
'138148320',
'107816131')
GROUP BY 1
I am getting a syntax error for "SUM" near
SUM(final_amount)-SUM(ISNULL(refund_amount, 0)) AS outstanding_amount
I tried different code:
SUM(total_amount - total_refund) AS npv
And I got the same error.
First off, there's a few errors in syntax. You're missing a comma between all the select-elements (between your
total_refundandoutstanding_amount). You should also check if theSUM()was null, not if the column was null. You can also useCOALESCE()if you prefer that. Finally, you need toGROUP BYsomething useful, like theitem_id.