在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
答案 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;
其余查询遵循相同的模式。
注释和建议:
ItemOwner
和/或Priority
是数字,请不要使用单引号作为常量。这会使查询的人类读者感到困惑,并且可能会使优化程序混淆。答案 1 :(得分:0)
如果我理解你的问题,你需要的是UNION声明。 UNION获取多个SELECTS的结果并将它们作为一个结果集返回。但请注意,每个SELECTS必须返回相同数量的列,并且我相信,每个SELECT中返回的列名必须具有相同的名称。