Sql 将多个选择转换为相同的结果

Sql 将多个选择转换为相同的结果,sql,Sql,在SQL server中,如何将多个SELECT语句的结果引入到一个返回的结果中?我正在使用各种条件运行计数,并希望能够在一个结果中显示所有条件。这是可能的还是我需要SSR 以下是我的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',

在SQL server中,如何将多个SELECT语句的结果引入到一个返回的结果中?我正在使用各种条件运行计数,并希望能够在一个结果中显示所有条件。这是可能的还是我需要SSR

以下是我的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

如果我理解你的问题,你需要的是一份工会声明。UNION接受多个选择的结果并将其作为一个结果集返回。但是,请注意,每个SELECT必须返回相同数量的列,而且我相信,每个SELECT中返回的列名必须具有相同的名称。

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

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;
其余的查询遵循相同的模式

注意事项和建议:

  • 不要使用单引号定义列别名。最后,当您使用列时,您将开始使用单引号,这将是一个bug。为列命名,使其不需要转义,或者使用双引号或方括号
  • 写出日期函数的日期部分。“dd”对“day”来说似乎很明显,但是“mm”呢?“y”?“dw”?友好一点,让代码尽可能透明
  • 在SQLServer2008及更高版本中,您只需强制转换到日期即可删除时间组件
  • 如果
    ItemOwner
    和/或
    Priority
    是数字,则不要对常量使用单引号。这会混淆查询的人类读者,并会混淆优化器

请提供样本数据和所需结果。我已在原始询问中添加了更多细节。我希望这会有所帮助?我相信如果不符合标准,某些结果可能不会返回任何数据。既然我已经添加了我正在使用的SQL,你能再看看我的提问吗?@Ben实际上,在大多数数据库中,只有联合中的第一个select用于结果中的名称。对于另一个select,不需要别名。我看不出工会如何将这些聚合数字放入OP所要求的列中。我想,你可以从那里开始,但你仍然必须改变它。我认为Gordon的答案或将每个计数放入相关子查询是创建此查询最直接的方法。@TimLehner我认为你是对的,Tim。最初的问题没有给出示例数据或期望的结果,只是问如何将多个SELECT语句的结果相互追加。
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;