我已经看了几个类似的问题并试图运用我自己的知识来使查询正常工作,但仍然无法得到我想要的结果。 我需要从我们的数据库报告5条呼叫线路。问题是他们中的一些人可以在没有电话的情况下整整一天。而不是报告0,它只是没有拉线。不幸的是,出于报告目的,我需要将其放入0.
SELECT CASE [Campaign]
WHEN '1' THEN 'Line A'
WHEN '2' THEN 'Line B'
WHEN '3' THEN 'Line C'
WHEN '4' THEN 'Line D'
WHEN '5' THEN 'Line E'
ELSE 'Outbound Dialling' END AS [Line_name],
COUNT(UniqueID) AS [Total Calls]
FROM Telephone_Table
WHERE [CallTime] BETWEEN '2017-12-01 00:00:00' AND '2017-12-01 23:59:59'
AND [Campaign] IN ('402','403','404','405','406')
结果:
Line_name | Campaign | Total Calls
Line A | 402 | 13
Line B | 403 | 11
Line C | 405 | 26
Line E | 406 | 178
D线经常没有接到电话。而不是将其列为0,它将完全错过它。
我尝试了以下但没有一个有效: COUNT(UniqueID)+1
COUNT(UniqueID)='0'时的情况,然后1个ELSE COUNT(UniqueID)
我尝试在数据库中创建另一个表,只有这些行和一个名称在那里加入而不是使用“Case When”,如果这是问题但是没有用。
有人有什么建议吗?谢谢。
答案 0 :(得分:0)
这里有一个建议:为你想要显示的每个组添加一个假人,然后从计数中减去1:
SELECT CASE [Campaign]
WHEN '402' THEN 'Line A'
WHEN '403' THEN 'Line B'
WHEN '404' THEN 'Line C'
WHEN '405' THEN 'Line D'
WHEN '406' THEN 'Line E'
ELSE 'Outbound Dialling' END AS [Line_name]
,[Campaign]
,COUNT(UniqueID)-1 AS [Total Calls]
FROM
(SELECT [Campaign], [UniqueID] FROM Telephone_Table
WHERE [CallTime] BETWEEN '2017-12-01 00:00:00' AND '2017-12-01 23:59:59'
AND [Campaign] IN ('402','403','404','405','406')
UNION SELECT '402' AS [Campaign], 'DUMMY1' AS UniqueID
UNION SELECT '403' AS [Campaign], 'DUMMY2' AS UniqueID
UNION SELECT '404' AS [Campaign], 'DUMMY3' AS UniqueID
UNION SELECT '405' AS [Campaign], 'DUMMY4' AS UniqueID
UNION SELECT '406' AS [Campaign], 'DUMMY5' AS UniqueID) AS a
GROUP BY [Campaign]
我可能混淆了我的语法,我现在还没有方便的sql-server检查,但是这个一般的想法应该有效。