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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -