我希望 SQL 從以下查詢回傳所有日期,無論它們是 null 還是 0:
SELECT count(id) as Record_Count, delete_date as Date
FROM table
WHERE marketing_tag = 'XYZ'
GROUP BY delete_date
ORDER BY delete_date desc
電流輸出:
| Record_Count | Date |
|100 | 01/07/22|
|200 | 01/05/22|
|250 | 01/02/22|
期望的輸出:
| Record_Count | Date |
|100 | 01/07/22|
|0 | 01/06/22|
|200 | 01/05/22|
|0 | 01/04/22|
|0 | 01/03/22|
|250 | 01/02/22|
我沒有另一個包含要參考的日期的表 我的最小值應該是從查詢中提取的第一個日期(在本例中為 01/02/22),然后我的最大值應該是邏輯適用的最近日期(在此情況下,22 年 1 月 7 日)在不創建另一個表的情況下這可能嗎?
uj5u.com熱心網友回復:
考慮以下方法
select ifnull(record_count, 0) record_count, date
from unnest((
select generate_date_array(min(delete_date), max(delete_date))
from your_table
)) date
left join (
select count(id) as record_count, delete_date as date
from your_table
where marketing_tag = 'xyz'
group by delete_date
)
using (date)
order by date desc
如果應用于您問題中的樣本資料 - 輸出是

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431096.html
