用于减去两个查询的sql

时间:2014-06-26 12:15:37

标签: sql sql-server

我在使查询工作时遇到问题。谁能帮忙。 我有两组想要合并的查询。

第一个是

SELECT 'Total Calls Hitting the Office' as "Report"
    ,Call_date as "Date"
    ,SUM([Calls) as "Total"
    ,'Calls' as "Units"
FROM Call
GROUP BY Call_date

并使用2个表生成此查询。

 select 'Total Calls Coming through the Call Centre' as "Report"
  ,Interval_Start_date as "Date"
  ,sum(MyCS.Incoming + MyCA.Abandoned)/4 as "Total"
  , 'Calls' as " Units"
    From call_callsummary as MyCS
   left join Call_abandonedcalls as MyCA
   on MyCS.Interval_Start_date = MyCA.CallAbandoned_date
   where MyCS.Category <> 'Application

我的第三个查询是从第一个QUERY TOTAL中减去第二个查询TOTAL ......

我尝试使用下面的查询将3个表合并在一起,但我得到的结果比期望的结果要多10倍。

select 'Total Calls Dealt Within the office' as "Report"
  ,Call_date as "Date"
  ,sum(MyC.[Calls]) - Sum(MyCS.Incoming + MyCA.Abandoned)/4 as "Total"
  ,'Calls' as " Units"
    FROM Call as MyC inner join call_callsummary as MyCS
    on MyCS.Interval_Start_date = MyC.Call_date
   inner join Call_abandonedcalls as MyCA
   on MyCS.Interval_Start_date = MyCA.CallAbandoned_date  
   where MyCS.Category <> 'Application'
   group by Call_date

3 个答案:

答案 0 :(得分:1)

大多数数据库都有minusexcept来减去两个查询。此示例适用于Oracle(使用minus):

SELECT Call_date as "Date"
,      SUM([Calls) as "Total"
,      'Calls' as "Units"
FROM   Call
GROUP
BY     Call_date
except
select Interval_Start_date as "Date"
,      sum(MyCS.Incoming + MyCA.Abandoned)/4 as "Total"
,      'Calls' as " Units"
From   call_callsummary as MyCS
left
join   Call_abandonedcalls as MyCA
on     MyCS.Interval_Start_date = MyCA.CallAbandoned_date
where  MyCS.Category <> 'Application

如果要减去而不是行,请使用下面的查询。它连接date上的行并减去它们的值:

select x.date
,      x.total - y.total
,      x.units - y.units
from   ( SELECT Call_date as "Date"
         ,      SUM([Calls) as "Total"
         ,      'Calls' as "Units"
         FROM   Call
         GROUP
         BY     Call_date
       ) x
  join ( select Interval_Start_date as "Date"
         ,      sum(MyCS.Incoming + MyCA.Abandoned)/4 as "Total"
         ,      'Calls' as " Units"
         From   call_callsummary as MyCS
         left
         join   Call_abandonedcalls as MyCA
         on     MyCS.Interval_Start_date = MyCA.CallAbandoned_date
         where  MyCS.Category <> 'Application
       ) y
  on   x.date = y.date

答案 1 :(得分:0)

WITH TotalCallsHittingTheOffice AS (
    SELECT Call_date as "Date"
          ,SUM(Calls) as "Total"
    FROM Call
    GROUP BY Call_date
)
,TotalCallsComingThroughTheCallCentre AS (
    SELECT Interval_Start_date as "Date"
           SUM(MyCS.Incoming + MyCA.Abandoned)/4 as "Total"
    FROM   call_callsummary as MyCS
           LEFT JOIN Call_abandonedcalls as MyCA
               ON Call_abandonedcalls.CallAbandoned_date = call_callsummary.Interval_Start_date = 
    WHERE  call_callsummary.Category <> 'Application'
    GROUP BY Interval_Start_date
)
SELECT 'Total Calls Dealt Within the office' AS "Report"
      ,TotalCallsHittingTheOffice."Date" AS "Date"
      ,SUM(TotalCallsHittingTheOffice."Total" - COALESCE(TotalCallsComingThroughTheCallCentre."Total", 0))
      ,'Calls' AS " Units"
FROM TotalCallsHittingTheOffice
     LEFT JOIN TotalCallsComingThroughTheCallCentre
        ON TotalCallsHittingTheOffice."Date" = TotalCallsComingThroughTheCallCentre."Date"
GROUP BY TotalCallsHittingTheOffice."Date"

答案 2 :(得分:0)

试试这个:

With TotalCallsFromCallCentre 
As
(
 select 'Total Calls Coming through the Call Centre' as "Report"
  ,Interval_Start_date as "Date"
  ,sum(MyCS.Incoming + MyCA.Abandoned)/4 as "Total"
  , 'Calls' as " Units"
    From call_callsummary as MyCS
   left join Call_abandonedcalls as MyCA
   on MyCS.Interval_Start_date = MyCA.CallAbandoned_date
   where MyCS.Category <> 'Application'
)
select 'Total Calls Dealt Within the office' as "Report"
  ,Call_date as "Date"
  ,Sum(Total - Call.[Calls])
  ,'Calls' as " Units"
  From Call inner join TotalCallsFromCallCentre on Call.Call_Date = TotalCallsFromCallCentre.Date
Group by Call_Date

虽然我无法在SQL Server中尝试这个,但它应该可以工作。