多个选择相同的结果

时间:2016-07-19 21:16:28

标签: sql

在SQL Server中,如何将多个SELECT语句的结果引入单个返回的结果中?我正在运行具有各种标准的COUNT,并希望能够在一个结果中显示它们。这可能还是我需要SSRS?

以下是我的SQL查询:

--Total calls in the queue
SELECT 
ItemOwner as 'Team'
, Count (*) as 'Total calls in the queue'
FROM CG_IncidentRequest
WHERE
Status not in( 'Closed', 'Pending Close', 'Pending Resolution')
AND 
SLAStage is not NULL
AND 
Priority not in( '7')
AND
ItemOwner in('482', '1041', '483', '486', '489', '701', '1714')
GROUP by ItemOwner

--Total new calls for the week
SELECT 
ItemOwner as 'Team'
, Count (*) as 'Total calls in the queue'
FROM CG_IncidentRequest
WHERE 
SLAStage is not NULL
AND 
Priority not in( '7')
AND
ItemOwner in('482', '1041', '483', '486', '489', '701', '1714')
AND
CreatedDateTime >= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(dd,-7, GetDate())), 0) 
GROUP by ItemOwner

--Calls fixed within SLA for the week
SELECT 
ItemOwner as 'Team'
, Count (*) as 'Calls fixed within SLA for the week'
FROM CG_IncidentRequest
WHERE 
SLAStage is not NULL
AND 
Priority not in( '7')
AND
ItemOwner in('482', '1041', '483', '486', '489', '701', '1714')
AND
CreatedDateTime >= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(dd,-7, GetDate())), 0) 
AND
Status = 'Closed'
AND SLAStage = 'Meets'
GROUP by ItemOwner

--Calls fixed outside SLA for the week
SELECT 
ItemOwner as 'Team'
, Count (*) as 'Calls fixed outside SLA for the week'
FROM CG_IncidentRequest
WHERE 
SLAStage is not NULL
AND 
Priority not in( '7')
AND
ItemOwner in('482', '1041', '483', '486', '489', '701', '1714')
AND
CreatedDateTime >= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(dd,-7, GetDate())), 0) 
AND
Status = 'Closed'
AND SLAStage = 'Escalation 2'
GROUP by ItemOwner

--Calls older than 7 days
SELECT 
ItemOwner as 'Team'
, Count (*) as 'Calls older than 7 days'
FROM CG_IncidentRequest
WHERE
Status not in( 'Closed', 'Pending Close', 'Pending Resolution')
AND 
SLAStage is not NULL
AND 
Priority not in( '7')
AND
ItemOwner in('482', '1041', '483', '486', '489', '701', '1714')
AND 
CreatedDateTime <= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(dd,-7, GetDate())), 0) 
GROUP by ItemOwner

--Calls older than 14 days
SELECT 
ItemOwner as 'Team'
, Count (*) as 'Calls older than 14 days'
FROM CG_IncidentRequest
WHERE
Status not in( 'Closed', 'Pending Close', 'Pending Resolution')
AND 
SLAStage is not NULL
AND 
Priority not in( '7')
AND
ItemOwner in('482', '1041', '483', '486', '489', '701', '1714')
AND 
CreatedDateTime <= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(dd,-14, GetDate())), 0) 
GROUP by ItemOwner

最终我希望结果看起来像这样:

Team    Active  Opened in last 7 days   Resolved inside SLA in last 7 days  Resolved outside SLA in last 7 days Older than 7 days   Older than 14 days
 482         83      83     197     2      34      23
1041         22      22      58     0      12       8
 483         68      68      50     0      46      35
 486         13      13      25     0       6       2
 489         38      38      2      3      31      30
 701         12      12      3      0       9       9
1714        270     270     31      0     251     239

2 个答案:

答案 0 :(得分:1)

我认为你只想要条件聚合。例如,前两个查询变成了这一个查询:

SELECT ItemOwner as Team,
       SUM(CASE WHEN Status not in ('Closed', 'Pending Close', 'Pending Resolution') AND
                     ItemOwner in ('482', '1041', '483', '486', '489', '701', '1714')
                THEN 1 ELSE 0
           END) as total_calls_in_queue,
       SUM(CASE WHEN ItemOwner in ('482', '1041', '483', '486', '489', '701', '1714')
AND
                     CreatedDateTime >= CAST(DATEADD(day -7, GetDate()) as DATE)
                THEN 1 ELSE 0
           END) as total_calls_in_queue
FROM CG_IncidentRequest
WHERE SLAStage is not NULL AND 
      Priority not in ( '7')
GROUP by ItemOwner;

其余查询遵循相同的模式。

注释和建议:

  • 不要使用单引号来定义列别名。最后,当您使用该列时,您将开始使用单引号,这将是一个错误。可以为列命名,使其不需要转义,也可以使用双引号或方括号。
  • 写出日期函数的日期部分。 &#34; DD&#34;对于&#34; day&#34;来说可能看起来很明显,但是&#34; mm&#34;? &#34; Y&#34 ;? &#34; DW&#34 ;?很好,并使代码尽可能透明。
  • 您可以直接转换为删除SQL Server 2008及更高版本中的时间组件。
  • 如果ItemOwner和/或Priority是数字,请不要使用单引号作为常量。这会使查询的人类读者感到困惑,并且可能会使优化程序混淆。

答案 1 :(得分:0)

如果我理解你的问题,你需要的是UNION声明。 UNION获取多个SELECTS的结果并将它们作为一个结果集返回。但请注意,每个SELECTS必须返回相同数量的列,并且我相信,每个SELECT中返回的列名必须具有相同的名称。