我正计划在subchannel
和l_events
之间的2013-10-01
表的2014-03-31
列中计算事件数量,并按特定{{1}进行分组},subchannel
和year
。
现在我的结果如下:
month
我希望我的结果包含每个特定子通道的行,其中当月没有发生任何事件。如下所示:
year month subchannel count(*)
2013 10 creativemornings 1
2014 3 creativemornings 2
2013 11 founderinstitute 1
我尝试将我的查询加入我的日历表(其中包含所有相关日期的日期时间列),但它不起作用。有什么想法吗?
year month subchannel count(*)
2013 10 creativemornings 1
2013 11 creativemornings 0
2013 12 creativemornings 0
2014 1 creativemornings 0
2014 2 creativemornings 0
2014 3 creativemornings 2
2013 10 founderinstitute 0
2013 11 founderinstitute 1
2013 12 founderinstitute 0
2014 1 founderinstitute 0
2014 2 founderinstitute 0
2014 3 founderinstitute 0
答案 0 :(得分:1)
这样做。它创建子通道列表和日历表的笛卡尔积,并将l_events表LEFT JOIN连接到该结果集。
SELECT
year(c.datefield) as year,
month(c.datefield) as month,
s.subchannel,
count(l.created)
FROM db.calendar c
CROSS JOIN (select distinct subchannel from l_events) s
LEFT JOIN db.l_events l
ON (date(l.created) = c.datefield)
AND l.subchannel = s.subchannel
Where c.datefield between '2013-10-01' and '2014-03-31'
group by
s.subchannel,
year(c.created),
month(.created)
order by
s.subchannel,
year(c.created),
month(.created)
;