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

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -