[英]SQL - How to return every day of month even with no data available
如果表中有所有日期,則可以將其用作來源。 然后是cross join
和left join
:
select uc.user, uc.client, d.date,
coalesce(st.total, 0)
from (select distinct user, client
from sampletable
) uc cross join
(select distinct date
from sampletable
) d left join
sampletable st
on st.user = uc.user and st.client = uc.client and
st.date = d.date;
如果基表沒有所有日期,您可以通過多種方式生成它們:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.