即使在mysql中没有值,如何在Q1,Q2,Q3,Q4中显示零值

时间:2017-01-26 02:14:49

标签: mysql

我想显示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没有值或零

时,它仍然排除该值

1 个答案:

答案 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