查询 sql 中两个日期列的数据透视类型及其计数
Query for kind of pivot of two date column in sql and its count
我有两列,ticket_created_date 和 ticket_resolved_date,我想通过 SQL 查询以某种方式对它们进行透视(如果可以的话),并得到如下图所示的内容,
第一列是天数,即如果2个日期相差小于1天则为0,如果相差>1则为1-2
下一列是票数,在本例中只是行条目,每个桶都有。
我是 sql 的完全新手,需要在我的 looker 中使用它来实现数据可视化。
可能有很多方法可以解决这个问题,我会使用 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 分组并计数 (*),您将已经有了每天的计数,但您想要创建一个类别(“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;
我有两列,ticket_created_date 和 ticket_resolved_date,我想通过 SQL 查询以某种方式对它们进行透视(如果可以的话),并得到如下图所示的内容,
第一列是天数,即如果2个日期相差小于1天则为0,如果相差>1则为1-2
下一列是票数,在本例中只是行条目,每个桶都有。
我是 sql 的完全新手,需要在我的 looker 中使用它来实现数据可视化。
可能有很多方法可以解决这个问题,我会使用 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 分组并计数 (*),您将已经有了每天的计数,但您想要创建一个类别(“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;