如何比较SQL(2014)中两个select语句的结果?
我的两个问题:
SELECT CallDisposition, count(CallDisposition) as Count
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
where (DateTime between dateadd(minute,@timespan,convert(datetime2,(GETDATE())+ @count)) AND convert(datetime2,(GETDATE())+ @count))
Group by CallDisposition
SELECT CallDisposition, count(CallDisposition) as Count
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
where DateTime >= dateadd(minute,@timespan,convert(datetime2,(GETDATE())))
Group by CallDisposition
查询1的结果:
+-----------------+-------+
| CallDisposition | Count |
+-----------------+-------+
| 2 | 2 |
| 3 | 8 |
| 4 | 8 |
| 7 | 21 |
| 10 | 16 |
| 13 | 738 |
| 14 | 45 |
| 15 | 14 |
| 19 | 8 |
| 28 | 41 |
| 29 | 12 |
| 52 | 76 |
| 55 | 1 |
+-----------------+-------+
查询2的结果:
+-----------------+-------+
| CallDisposition | Count |
+-----------------+-------+
| 2 | 4 |
| 3 | 7 |
| 4 | 6 |
| 6 | 2 |
| 7 | 22 |
| 10 | 6 |
| 13 | 703 |
| 14 | 67 |
| 15 | 11 |
| 19 | 4 |
| 26 | 1 |
| 28 | 62 |
| 29 | 10 |
| 52 | 79 |
+-----------------+-------+
背后的主要问题是,两个查询的结果可能不同。基于上面的示例:查询1中缺少CallDisposition 6和26.在查询2中缺少CallDispoition 55注意:可以预期CallDispoition值0-100,这可能有帮助吗?!
预期结果应如下所示:
+-----------------+-------+
| CallDisposition | Count |
+-----------------+-------+
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
| 6 | 2 |
| 7 | 1 |
| 10 | 10 |
| .. | |
| .. | |
| .. | |
| 52 | 3 |
| 55 | 1 |
+-----------------+-------+
WITH S1 AS (SELECT CallDisposition, count(CallDisposition) as Count
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
where (DateTime between dateadd(minute,@timespan,convert(datetime2,(GETDATE())+ @count)) AND convert(datetime2,(GETDATE())+ @count))
Group by CallDisposition
)
,S2 AS (
SELECT CallDisposition, count(CallDisposition) as Count
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
where DateTime >= dateadd(minute,@timespan,convert(datetime2,(GETDATE())))
Group by CallDisposition
)
Select ISNULL(S1.CallDisposition,S2.CallDisposition) AS CallDisposition
,ABS(ISNULL(S1.COUNT,0)-ISNULL(S2.Count,0)) Count
FROM S1 FULL JOIN S2
ON S1.CallDisposition=s2.CallDisposition
老实说,我对你的预期结果感到困惑,并将结果进行了比较。所以我的解决方案不会产生这样的结果。但也许它指向你正确的方向有帮助。
你可以在calldisposition
上完全加入结果。这样共存的calldisposition
将彼此相邻。如果在一个结果中对于calldisposition
,在另一个结果中没有人存在另一个的列将是所有NULL
。
SELECT *
FROM (SELECT calldisposition,
count(CallDisposition) count
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
WHERE (datetime BETWEEN dateadd(minute, @timespan, convert(datetime2, (getdate()) + @count))
AND convert(datetime2, (getdate()) + @count))
GROUP BY calldisposition) x
FULL JOIN (SELECT calldisposition,
count(calldisposition) count
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
WHERE datetime >= dateadd(minute, @timespan, convert(datetime2, (getdate())))
GROUP BY calldisposition) y
ON y.calldisposition = x.calldisposition;
我可能会将结果插入临时表,然后查询。使用表变量:
DECLARE @Results TABLE ([Source] VARCHAR(10), CallDisposition INT, [CallCount] INT)
(使用CallCount,因为那不是保留字)
INSERT INTO @Results(CallDisposition , CallCount, [Source])
SELECT CallDisposition, count(CallDisposition) as Count, [Source] = 'Query1',
FROM [bs_Reporting].[dbo].[Termination_Call_Detail]
where (DateTime between dateadd(minute,@timespan,convert(datetime2,(GETDATE())+ @count)) AND convert(datetime2,(GETDATE())+ @count))
Group by CallDisposition
重复第二个查询,设置Source ='Query2'(或在上面的插入中与它结合)。
现在你可以检查你的@Results了:
SELECT r1.* FROM @Results r1
WHERE [Source] = 'Query1'
AND NOT EXISTS (SELECT 'X' FROM @Results r2 WHERE [Source] = 'Query2' AND r2.CallDisposition = r1.CallDisposition)
(仅限示例;可能不是最有洞察力的)
请原谅任何轻微的语法错误;我面前没有SQL Server。