如何比较TSQL(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 |
+-----------------+-------+
答案 0 :(得分:0)
老实说,我对您的预期结果感到困惑,并且比较了结果。所以我的解决方案不会产生这样的结果。但这也许可以为您指出正确的方向。
您可以完全加入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;
答案 1 :(得分:0)
我可能会将结果插入临时表中,然后进行查询。使用表变量:
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。
答案 2 :(得分:0)
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