【发布时间】:2018-08-14 07:49:32
【问题描述】:
我有一个包含以下数据的表格。
**Priority** **Mydate** **ID**
Critical 2018/01/20 1090
High 2018/01/27 1091
High 2018/01/18 1092
High 2018/01/24 1093
Low 2017/09/28 1083
要求获取所有优先级类型(关键、高、中和低)及其计数的最近 12 个月记录。如果特定月份的数据库中不存在任何优先级类型,则显示零而不是实际计数。
SELECT TO_CHAR(Mydate, 'MM/YYYY') AS Mydate, PRIORITY, count(*)
FROM MYTABLE
WHERE Mydate >= add_months(trunc(sysdate, 'month'), - 12)
GROUP BY TO_CHAR(Mydate, 'MM/YYYY'), PRIORITY
ORDER BY TO_CHAR(Mydate, 'MM/YYYY') ASC, PRIORITY ASC;
通过上面的查询,我只能做到这一点:
Mydate PRIORITY Count
---------------------------------
01/2018 High 3
01/2018 Critical 1
09/2017 Low 1
预期结果是:
Mydate PRIORITY Count
---------------------------------
01/2018 Critical 1
01/2018 High 3
01/2018 Medium 0
01/2018 Low 0
09/2017 Critical 0
09/2017 High 0
09/2017 Medium 0
09/2017 Low 1
【问题讨论】:
-
分区外连接语法解决了这个问题docs.oracle.com/cd/B28359_01/server.111/b28313/…