请帮我写一个子查询来加入。
表格页
P1
P2
P3
P4
操作
A1: page_id = P1, type = 'view', date = '2015-02-10 12:23:00'
A2: page_id = P1, type = 'view', date = '2015-02-11 13:12:23'
A3: page_id = P3, type = 'view', date = '2015-02-12 09:33:23'
A4: page_id = P4, type = 'click', date = '2015-02-12 10:12:12'
A5: page_id = P5, type = 'view', date = '2015-02-12 15:47:32'
我需要按天计算网页浏览量(我们不需要计算其他网页浏览量,只需先打开):
'2015-02-10': 1
'2015-02-12': 2
SQL模板
SELECT DATE(date) AS period, COUNT(*) AS num
FROM pages p
LEFT JOIN actions a ON a.page_id = p.id { ??? }
WHERE a.type = 'view'
GROUP BY period
更新
我曾尝试编写此SQL,但执行时间很长(~2秒)
SELECT DATE(a.date) AS period, COUNT(*) AS num
FROM pages p
LEFT JOIN (
SELECT MIN(date) as date, page_id
FROM actions
WHERE type = 'view'
GROUP BY page_id
) a ON a.page_id = p.id
WHERE a.type = 'view'
GROUP BY period