给定下表:
table1:
user_id | date_created | product
1 2020-10-02 1
1 2020-10-08 1
1 2020-10-08 1
1 2020-10-09 1
和选择:
select to_char(date_created, 'Dy') as day,
count(product)
from table1
where user_id = 1
and date_created >= now() - interval '10 days'
group by day
它产生以下输出:
day | count
-----+-------
Fri | 1
Thu | 2
Fri | 1
(3 rows)
我如何填写不products存在的日子的空白以便它们显示0?像这样的东西:
day | count
-----+-------
Fri | 1
Sat | 0
Sun | 0
Mon | 0
Tue | 0
Wed | 0
Thu | 2
Fri | 1
(8 rows)
用一个连接连接两个表是可行的left outer,但我在这里使用一个表。我可以在前端捏造它,但如果能够在 SQL 中处理这个问题,那就太好了。
我会很感激评论!
谢谢