Sunday, 15 June 2014

sql server - sql for subtracting two queries -



sql server - sql for subtracting two queries -

im having issues making query work. can help. have 2 set of queries want merge together.

the first 1 is

select 'total calls hitting office' "report" ,call_date "date" ,sum([calls) "total" ,'calls' "units" phone call grouping call_date

and query generated using 2 tables.

select 'total calls coming through phone call centre' "report" ,interval_start_date "date" ,sum(mycs.incoming + myca.abandoned)/4 "total" , 'calls' " units" call_callsummary mycs left bring together call_abandonedcalls myca on mycs.interval_start_date = myca.callabandoned_date mycs.category <> 'application

my 3rd query meant subtract sec query total first query total...

i have tried using query below merging 3 tables getting results 10 times more desired result should be.

select 'total calls dealt within office' "report" ,call_date "date" ,sum(myc.[calls]) - sum(mycs.incoming + myca.abandoned)/4 "total" ,'calls' " units" phone call myc inner bring together call_callsummary mycs on mycs.interval_start_date = myc.call_date inner bring together call_abandonedcalls myca on mycs.interval_start_date = myca.callabandoned_date mycs.category <> 'application' grouping call_date

with totalcallshittingtheoffice ( select call_date "date" ,sum(calls) "total" phone call grouping call_date ) ,totalcallscomingthroughthecallcentre ( select interval_start_date "date" sum(mycs.incoming + myca.abandoned)/4 "total" call_callsummary mycs left bring together call_abandonedcalls myca on call_abandonedcalls.callabandoned_date = call_callsummary.interval_start_date = call_callsummary.category <> 'application' grouping interval_start_date ) select 'total calls dealt within office' "report" ,totalcallshittingtheoffice."date" "date" ,sum(totalcallshittingtheoffice."total" - coalesce(totalcallscomingthroughthecallcentre."total", 0)) ,'calls' " units" totalcallshittingtheoffice left bring together totalcallscomingthroughthecallcentre on totalcallshittingtheoffice."date" = totalcallscomingthroughthecallcentre."date" grouping totalcallshittingtheoffice."date"

sql sql-server

No comments:

Post a Comment