0

我有两列,ticket_created_date 和 ticket_resolved_date,我想通过 SQL 查询以某种方式旋转(如果我可以称之为)它们并得到如下图所示的内容,

  1. 第一列是天的桶,即如果2个日期的差异小于1天,则为0,如果差异> 1,则为1-2

  2. 下一列是票的数量,在这种情况下只是行条目,每个桶都有。

我是一个完整的 sql 新手,并且在我的一个寻找数据可视化的人中需要这个。

在此处输入图像描述

4

1 回答 1

0

可能有很多方法可以解决这个问题,我会使用 GROUP BY。

首先,您必须了解这两个日期之间的差异。由于您没有指定您使用的 DBMS(ORACLE、SQL SERVER、My SQL 等),我将给出 oracle 和 SQL SERVER 的示例。

甲骨文:

SELECT (ticket_resolved_date - ticket_created_date) AS DAYS FROM TABLE_NAME;

SQL 服务器:

SELECT DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) AS DAYS FROM TABLE_NAME;

如果您只是按 DAYS 和 count(*) 分组,那么您已经拥有每天的计数,但您想创建一个类别('1-2'、'3-4' 等)。为此,您可以使用 CASE。

甲骨文:

SELECT CASE 
        WHEN (ticket_resolved_date - ticket_created_date) = 0 THEN 'A - 0'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 1 AND 2 THEN 'B - 1-2'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 3 AND 4 THEN 'C - 3-4'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 5 AND 6 THEN 'D - 5-6'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 7 AND 8 THEN 'E - 7-8'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 9 AND 10 THEN 'F - 9-10'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 11 AND 12 THEN 'G - 11-12'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 13 AND 14 THEN 'H - 13-14'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 15 AND 16 THEN 'I - 15-16'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 17 AND 18 THEN 'J - 17-18'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 19 AND 20 THEN 'K - 19-20'
        ELSE 'L - 20+'
    END AS DAYS_CATEGORY
    ,COUNT(*) AS "#TICKETS"
FROM DATES
GROUP BY CASE 
        WHEN (ticket_resolved_date - ticket_created_date) = 0 THEN 'A - 0'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 1 AND 2 THEN 'B - 1-2'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 3 AND 4 THEN 'C - 3-4'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 5 AND 6 THEN 'D - 5-6'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 7 AND 8 THEN 'E - 7-8'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 9 AND 10 THEN 'F - 9-10'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 11 AND 12 THEN 'G - 11-12'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 13 AND 14 THEN 'H - 13-14'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 15 AND 16 THEN 'I - 15-16'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 17 AND 18 THEN 'J - 17-18'
        WHEN (ticket_resolved_date - ticket_created_date) BETWEEN 19 AND 20 THEN 'K - 19-20'
        ELSE 'L - 20+'
    END
ORDER BY 1;

SQL 服务器:

SELECT CASE 
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) = 0 THEN '0'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 1 AND 2 THEN 'A - 1-2'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 3 AND 4 THEN 'B - 3-4'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 5 AND 6 THEN 'C - 5-6'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 7 AND 8 THEN 'D - 7-8'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 9 AND 10 THEN 'E - 9-10'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 11 AND 12 THEN 'F - 11-12'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 13 AND 14 THEN 'G - 13-14'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 15 AND 16 THEN 'H - 15-16'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 17 AND 18 THEN 'I - 17-18'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 19 AND 20 THEN 'J - 19-20'
        ELSE 'K - 20+'
    END AS DAYS_CATEGORY
    ,COUNT(*) AS "#TICKETS"
FROM TEST
GROUP BY CASE
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) = 0 THEN '0'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 1 AND 2 THEN 'A - 1-2'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 3 AND 4 THEN 'B - 3-4'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 5 AND 6 THEN 'C - 5-6'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 7 AND 8 THEN 'D - 7-8'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 9 AND 10 THEN 'E - 9-10'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 11 AND 12 THEN 'F - 11-12'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 13 AND 14 THEN 'G - 13-14'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 15 AND 16 THEN 'H - 15-16'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 17 AND 18 THEN 'I - 17-18'
        WHEN DATEDIFF(DAY,ticket_created_date,ticket_resolved_date) BETWEEN 19 AND 20 THEN 'J - 19-20'
        ELSE 'K - 20+'
    END
ORDER BY 1;
于 2020-01-24T12:59:38.360 回答