我有一个查询,它按小时返回有关销售的不同信息,问题是如果在某个时间该表上没有注册表,我需要得到 0。
这是查询:
SELECT COUNT(a.DOC_NO) AS "# TICKET",
SUM(a.TRANSACTION_TOTAL_AMT) AS "$ TICKET",
SUM(a.sold_qty) AS "# ITEMS", a.store_code AS "TIENDA",
to_char(a.CREATED_DATETIME,'yyyy-mm-dd') AS "FECHA",
to_char(a.CREATED_DATETIME,'HH24') || ':00' AS "HORA"
FROM document a
WHERE a.receipt_type in (0,1)
AND a.status=4 AND a.store_name!='CEDIS'
AND to_char(a.CREATED_DATETIME,'yyyy-mm-dd') = '2024-06-01'
AND a.STORE_CODE = 'ESF'
GROUP BY a.store_code,to_char(a.CREATED_DATETIME,'yyyy-mm-dd'), to_char(a.CREATED_DATETIME,'HH24')
ORDER BY a.store_code, to_char(a.CREATED_DATETIME,'yyyy-mm-dd'), to_char(a.CREATED_DATETIME,'HH24')
所以我得到这样的信息:
门票 | 金额 | 商品 | 商店 | 日期 | 小时 |
---|---|---|---|---|---|
2 | 1398 | 2 | ESF | 2024-06-01 | 12:00 |
1 | 819 | 2 | ESF | 2024-06-01 | 14:00 |
2 | 758.6 | 4 | ESF | 2024-06-01 | 15:00 |
4 | 1386.6 | 4 | ESF | 2024-06-01 | 16:00 |
到目前为止,我尝试过:
SELECT CASE WHEN EXISTS(
SELECT COUNT(a.DOC_NO) AS "# TICKET",
SUM(a.TRANSACTION_TOTAL_AMT) AS "$ TICKET",
SUM(a.sold_qty) AS "# ITEMS", a.store_code AS "TIENDA",
to_char(a.CREATED_DATETIME,'yyyy-mm-dd') AS "FECHA",
to_char(a.CREATED_DATETIME,'HH24') || ':00' AS "HORA"
FROM document a
WHERE a.receipt_type in (0,1) and a.status=4
AND a.store_name!='CEDIS'
AND to_char(a.CREATED_DATETIME,'yyyy-mm-dd') = '2024-06-01'
AND a.STORE_CODE = 'ESF'
GROUP BY a.store_code,to_char(a.CREATED_DATETIME,'yyyy-mm-dd'), to_char(a.CREATED_DATETIME,'HH24')
ORDER BY a.store_code, to_char(a.CREATED_DATETIME,'yyyy-mm-dd'), to_char(a.CREATED_DATETIME,'HH24')
)
THEN to_char(a.CREATED_DATETIME,'HH24')
ELSE NULL END AS hours
FROM (VALUES('12'),('13'),('14'),('15'),('16')) CON(hours)
但我认为我错过了一些东西,在另一篇文章中找到了这种方式,但我得到的只是“缺少右括号”。
所以,我需要得到的是,如果在 13:00 没有销售,则查询显示 0(或 null)
门票 | 金额 | 商品 | 商店 | 日期 | 小时 |
---|---|---|---|---|---|
2 | 1398 | 2 | ESF | 2024-06-01 | 12:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 13:00 |
1 | 819 | 2 | ESF | 2024-06-01 | 14:00 |
2 | 758.6 | 4 | ESF | 2024-06-01 | 15:00 |
4 | 1386.6 | 4 | ESF | 2024-06-01 | 16:00 |
使用行生成器生成所有小时,然后使用
PARTITION
ed OUTER JOIN
,然后将 SUM
聚合包装在 COALESCE
中,将 NULL
值更改为 0
:
SELECT COUNT(a.DOC_NO) AS "# TICKET",
COALESCE(SUM(a.TRANSACTION_TOTAL_AMT), 0) AS "$ TICKET",
COALESCE(SUM(a.sold_qty), 0) AS "# ITEMS",
a.store_code AS "TIENDA",
a.day AS "FECHA",
h.hour AS "HORA"
FROM ( SELECT TO_CHAR(LEVEL - 1, 'FM00') || ':00' AS hour
FROM DUAL
CONNECT BY LEVEL <= 24 ) h
LEFT OUTER JOIN (
SELECT store_code,
doc_no,
transaction_total_amt,
sold_qty,
TO_CHAR(CREATED_DATETIME,'yyyy-mm-dd') AS day,
TO_CHAR(CREATED_DATETIME,'HH24') || ':00' AS hour
FROM document
WHERE receipt_type in (0,1)
AND status=4
AND store_name!='CEDIS'
AND CREATED_DATETIME >= DATE '2024-06-01'
AND CREATED_DATETIME < DATE '2024-06-02'
AND STORE_CODE = 'ESF'
) a
PARTITION BY (a.store_code, a.day)
ON (a.hour = h.hour)
GROUP BY
a.store_code,
a.day,
h.hour
ORDER BY
a.store_code,
a.day,
h.hour
对于样本数据:
CREATE TABLE document (
doc_no, transaction_total_amt, sold_qty, created_datetime,
receipt_type, status, store_name, store_code
) AS
SELECT 1,
10,
100,
DATE '2024-06-01' + INTERVAL '0' HOUR,
0,
4,
'NOT_CEDIS',
'ESF'
FROM DUAL;
输出:
# 门票 | $ 门票 | # 项目 | 天达 | 费查 | 霍拉 |
---|---|---|---|---|---|
1 | 10 | 100 | ESF | 2024-06-01 | 00:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 01:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 02:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 03:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 04:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 05:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 06:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 07:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 08:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 09:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 10:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 11:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 12:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 13:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 14:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 15:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 16:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 17:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 18:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 19:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 20:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 21:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 22:00 |
0 | 0 | 0 | ESF | 2024-06-01 | 23:00 |