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

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

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

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -