我有两个返回不同结果的查询,我想合并它们,但我仍然在学习如何用postgreSQ编写,所以我在做这件事时遇到了一些困难。我的问题是:
SELECT * FROM generate_series('2019-01-01', now(), '1 month')
EXAMPLE:
generate_series
------------------------
2019-01-01 00:00:00+00
2019-02-01 00:00:00+00
2019-03-01 00:00:00+00
2019-04-01 00:00:00+00SELECT date_trunc('MONTH', (date_signed_up::date)) AS monthly, count(id) AS count FROM customer
WHERE group_id = 1
GROUP BY monthly
ORDER BY monthly asc
EXAMPLE:
monthly | count
-----------------------------------
2019-01-01 00:00:00+00 | 3
2019-02-01 00:00:00+00 | 1
2019-04-01 00:00:00+00 | 1预期的结果或我需要的是有一个这样的表格:
monthly | count
-----------------------------------
2019-01-01 00:00:00+00 | 3
2019-02-01 00:00:00+00 | 1
2019-03-01 00:00:00+00 | 0
2019-04-01 00:00:00+00 | 1
2019-05-01 00:00:00+00 | 0
..etc发布于 2019-06-18 20:23:27
SELECT period monthly, count(id) count
FROM generate_series('2019-01-01', now(), '1 month') p (period)
LEFT JOIN customer
ON p.period = date_trunc('MONTH', (date_signed_up::date)) AND group_id = 1
GROUP BY monthly
ORDER BY monthly aschttps://stackoverflow.com/questions/56656460
复制相似问题