kha*_*rul 4 sql postgresql aggregate-functions
我正在使用count并group by了解每天有多少订阅者购买:
select count(s.email)
from subscriptions s, orders o
where o.subcription_id=s.id
group by s.created_at
Run Code Online (Sandbox Code Playgroud)
结果:
created_at count
------------------
04-04-2011 1
06-04-2011 2
07-04-2011 1
10-04-2011 5
11-04-2011 9
Run Code Online (Sandbox Code Playgroud)
但是我仍然希望空行返回为'0'.我该怎么做呢?请注意,我必须使用这两个表.
created_at count
------------------
04-04-2011 1
05-04-2011 0
06-04-2011 2
07-04-2011 1
08-04-2011 0
09-04-2011 0
10-04-2011 5
11-04-2011 9
Run Code Online (Sandbox Code Playgroud)
SELECT s.created_at, COUNT(o.subsription_id)
FROM subscriptions s
LEFT JOIN
orders o
ON o.subcription_id = s.id
GROUP BY
s.created_at
Run Code Online (Sandbox Code Playgroud)