我为这样的客户提供了一张桌子
cust_id | date_signed_up | location_id
-----------------------------------------
1 | 2019/01/01 | 1
2 | 2019/03/05 | 1
3 | 2019/06/17 | 1
我需要的是每月计数,但即使是0也要有月份。例如:
monthly_count | count
-------------------------
Jan | 1
Feb | 0
Mar | 1
Apr | 0
(月份可以是数字)
现在我进行了以下查询:
SELECT date_trunc('MONTH', (date_signed_up::date)) AS monthly, count(customer_id) AS count FROM customer
WHERE group_id = 1
GROUP BY monthly
ORDER BY monthly asc
但这只是几个月来提供给我的信息,跳过了零的信息。即使有或没有信息,我怎么能得到所有月份。
答案 0 :(得分:0)
您需要一个月列表。
How to generate Month list in PostgreSQL?
SELECT a.month , count( y.cust_id )
FROM allMonths a
LEFT JOIN yourTable y
ON a.month = date_trunc('MONTH', (date_signed_up::date))
GROUP BY a.month