我想显示Q1,Q2,Q3,Q4,虽然值为空或零...我的查询是这样的
Select Coalesce(Quarter(day_date), 0), year(day_date)
from daily
group by year(day_date)
order by year(day_date)
但是当Q1,Q2,Q3,Q4没有值或零
时,它仍然排除该值答案 0 :(得分:0)
您可以使用cross join
执行此操作,以生成数据中年份和季度的所有组合:
Select y.yyyy, q.qq, count(d.day_date)
from (select distinct year(day_date) as yyyy from daily) y cross join
(select 1 as qq union all select 2 union all select 3 union all select 4) q left join
daily d
on year(d.day_date) = y.yyyy and
quarter(d.day_date) = q.qq
group by y.yyyy, q.qq
order by y.yyyy, q.qq;
实际上,只有在您真正想要从left join
表中计算某些内容时才需要daily
。