0

我查看了一些类似的问题,并尝试运用自己的知识使查询正常工作,但仍然无法获得我想要的结果。我有 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

CASE WHEN COUNT(UniqueID) = '0' THEN 1 ELSE COUNT(UniqueID)

我尝试在数据库中创建另一个表,其中仅包含这些行和一个要加入的名称,而不是使用“Case When”,以防万一这是问题但那不起作用。

有人有什么建议吗?谢谢你。

4

1 回答 1

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 来检查,但是这个一般的想法应该可行。

于 2017-12-06T12:39:09.230 回答