Home > OS >  Deduct result of one query from another SQL
Deduct result of one query from another SQL

Time:12-15

I have result of an SQL query as following:

Date | Pt_Cnt | Wardname | Division 
12/11| 23     | wd01     | Med 
12/11| 30     | wd02     | Surg 
12/11| 35     | wd08     | Surg

and I have a result from another SQL query for patients on leave as:

Date | Pt_Cnt | Wardname | Division 
12/11| 2      | wd01     | Med 
12/11| 1      | wd02     | Surg 

I want the number of pt_cnt from the second query deducted from pt_cnt of 1st query. How can this be done in SQL?

CodePudding user response:

SQL Server:

select a.date, a.pt_cnt-isnull(b.pt_cnt,0), a.ward, a.division
from (first query) as a
left join (second query) as b on a.date = b.date and a.ward = b.ward and a.division = b.divisionn

MySql:

select a.date, a.pt_cnt-ifnull(b.pt_cnt,0), a.ward, a.division
from (first query) as a
left join (second query) as b on a.date = b.date and a.ward = b.ward and a.division = b.divisionn
  • Related