计算日期范围,而不是计算特定的一天

时间:2019-01-11 21:47:40

标签: sql date count

我正尝试检索在一个财政月而不是一天之内生成的警报计数。我当前的代码让我收到一天的总数,但是我希望它检索一个财政月的总数。

我已经尝试了所有方法,该怎么做才能更改下面的代码?

SELECT COUNT(DISTINCT
    CASE
        WHEN substr(from_utc_timestamp(act.created, 'America/New_York'), 0, 10) = '[End date]'
            THEN act.activity_uid
        ELSE NULL
    END) AS Total_Generated_on_SLA_Date
FROM contactcentre.s_evt_act AS act
WHERE 
    (
        substr(from_utc_timestamp(act.created, 'America/New_York'), 0, 10) = '[End date]'
        OR substr(from_utc_timestamp(act.todo_actl_end_dt, 'America/New_York'), 0, 10) = '[End date]'
    )
    AND x_low_assgn_queue = 'Cust Care Exceptions'
    AND NOT todo_cd = 'Call';

1 个答案:

答案 0 :(得分:0)

一种快速而肮脏的解决方案,只需对现有查询进行最少的更改即可。

您已经有一个聚合查询,该查询将时间戳act.created截断到当天。只需扩展逻辑并截断3个其他字符即可获得月份。因此:

SELECT COUNT(DISTINCT
    CASE
        WHEN substr(from_utc_timestamp(act.created, 'America/New_York'), 0, 7) = '[End month]'
            THEN act.activity_uid
        ELSE NULL
    END) AS Total_Generated_on_SLA_Date
FROM contactcentre.s_evt_act AS act
WHERE 
    (
        substr(from_utc_timestamp(act.created, 'America/New_York'), 0, 7) = '[End date]'
        OR substr(from_utc_timestamp(act.todo_actl_end_dt, 'America/New_York'), 0, 7) = '[End month]'
    )
    AND x_low_assgn_queue = 'Cust Care Exceptions'
    AND NOT todo_cd = 'Call';

[End month]的值应为2019-01