将两个查询合并为一个

时间:2014-09-01 22:28:58

标签: sql

我有两个工作查询。查询1对大型表执行过滤,并准确返回我需要的数据,如下所示:

/****** QUERY #1 - This query will filter the data  ******/
  SELECT [WacnId],
   [StartDT]
  ,[EndDT]
  ,[Group]
  ,[ID_Agency]
  ,[TargetUnit_Agency],


  case [Group]
  when 1 then 'in'
  when 0 then 'out'
  end as traffic

FROM [GW_20140315].[dbo].[ARC_Calls_ReportView]

WHERE [GroupDisplayID] = 'T802149' OR [ID_Agency] = 'Dispatch' or [TargetUnit_Agency] =   'Dispatch'
order by StartDT

查询#2对来自查询1的过滤数据起作用,并生成1/2小时的报告。查询2如下所示:

/******Query #2-  This query will take the filtered data and process it as needed  ******/
SELECT dateadd(mi, (datediff(mi, 0, StartDT) / 30) * 30, 0) as HalfHour
   , sum(DATEDIFF ( s , [StartDT] , [EndDT] )) as [Total Time (Seconds)],
   SUM(CASE WHEN [TargetUnit_Agency] = 'Dispatch' then 1 ELSE 0 END ) AS InCount,
SUM(CASE WHEN [ID_Agency] = 'Dispatch' then 1 ELSE 0 END ) AS OutCount
FROM  [Radio].[dbo].[Filter_Data]--This is how I did it before, but now I want to combine the two  queries
GROUP BY dateadd(mi, (datediff(mi, 0, StartDT) / 30) * 30, 0) 
ORDER BY 1

我如何将这两个查询合并为一个?

2 个答案:

答案 0 :(得分:1)

您可以使用CTE描述过滤后的数据(第一次查询),然后使用CTE作为主表进行查询(第二次查询):

;WITH FilteredCTE AS
(
    SELECT [WacnId],
     [StartDT]
    ,[EndDT]
    ,[Group]
    ,[ID_Agency]
    ,[TargetUnit_Agency],
    case [Group]
        when 1 then 'in'
        when 0 then 'out'
    end as traffic
    FROM [GW_20140315].[dbo].[ARC_Calls_ReportView]
    WHERE [GroupDisplayID] = 'T802149' 
        OR [ID_Agency] = 'Dispatch' 
        or [TargetUnit_Agency] =   'Dispatch'
)
SELECT dateadd(mi, (datediff(mi, 0, StartDT) / 30) * 30, 0) as HalfHour,
sum(DATEDIFF ( s , [StartDT] , [EndDT] )) as [Total Time (Seconds)],
SUM(CASE WHEN [TargetUnit_Agency] = 'Dispatch' then 1 ELSE 0 END ) AS InCount,
SUM(CASE WHEN [ID_Agency] = 'Dispatch' then 1 ELSE 0 END ) AS OutCount
FROM  FilteredCTE
GROUP BY dateadd(mi, (datediff(mi, 0, StartDT) / 30) * 30, 0) 
ORDER BY StartDT

答案 1 :(得分:0)

只需选择Query1 FROM Query2:

SELECT Dateadd(mi, ( Datediff(mi, 0, startdt) / 30 ) * 30, 0) AS HalfHour, 
       Sum(Datediff (s, [startdt], [enddt]))                  AS 
       [Total Time (Seconds)], 
       Sum(CASE 
             WHEN [targetunit_agency] = 'Dispatch' THEN 1 
             ELSE 0 
           end)                                               AS InCount, 
       Sum(CASE 
             WHEN [id_agency] = 'Dispatch' THEN 1 
             ELSE 0 
           end)                                               AS OutCount 
FROM   (SELECT [wacnid], 
               [startdt], 
               [enddt], 
               [group], 
               [id_agency], 
               [targetunit_agency], 
               CASE [group] 
                 WHEN 1 THEN 'in' 
                 WHEN 0 THEN 'out' 
               end AS traffic 
        FROM   [GW_20140315].[dbo].[arc_calls_reportview] 
        WHERE  [groupdisplayid] = 'T802149' 
                OR [id_agency] = 'Dispatch' 
                OR [targetunit_agency] = 'Dispatch' 
        ORDER  BY startdt) 
GROUP  BY Dateadd(mi, ( Datediff(mi, 0, startdt) / 30 ) * 30, 0) 
ORDER  BY 1