我有一個包含 sales_date、sales_id、sales_region 列的表,我希望顯示過去 7 天、20 天和 YTD 的銷售計數。
我在下面有這個查詢,它回傳 7 天和 20 天的正確計數,但 YTD 顯示的計數減去 7 天和 20 天。如何調整此查詢以正確顯示 YTD?謝謝
select region,
case when current_date- sales_date <=7 then 'Past7'
when current_date- sales_date <=28 then 'Past20'
else 'YTD'
end as "trendsales",
count(*) as salescount
from sales_table
where sales_date >= '2022-01-01'
group by 1
uj5u.com熱心網友回復:
你可以稍微轉動一下。4 列 Region、YTD、Past7 和 Past20 將是??列。
select region,
sum(case when current_date- sales_date <=7 then 1 else 0 end) as Past7,
sum(case when current_date- sales_date <=28 then 1 else 0 end) as Past20,
count(*) as YTD
from sales_table
where sales_date >= '2022-01-01'
group by 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486967.html
