sql - Subtract two columns -
i have following ms access table:
itemid qty flag 1 2 0 1 1 1 2 5 0 2 4 1
i want write query balance (qty-qty
) , group flag
.
as example:
(sum of qty flag =0) - (sum of qty flag =1)
my final output should be:
1=1 2=1
use conditional aggregation:
select itemid, nz(sum(iif(flag = 0, qty, 0)), 0) - nz(sum(iif(flag = 1, qty, 0)), 0) difference yourtable group itemid
Comments
Post a Comment