如何计算 PostgreSQL 中多个日期范围内的出现次数

How to count occurences in several date ranges in PostgreSQL

我可以查询每个商店每个月 18 岁到 24 岁的顾客数量。 我是这样做的:

select year, month, shop_id, count(birthday) 
from customers 
where birthday 
BETWEEN '1992-01-01 00:00:00' AND '1998-01-01 00:00:00'
group by year, month, shop_id;

现在,我在同时查询多个范围时遇到问题。

我目前有这个数据库模式:

shop_id | birthday | year | month |
--------+----------+------+--------
 567   | 1998-10-10 | 2014 | 10 |
 567   | 1996-10-10 | 2014 | 10 |
 567   | 1985-10-10 | 2014 | 10 |
 234   | 1990-10-10 | 2014 | 10 |
 123   | 1970-01-10 | 2014 | 10 |
 123   | 1974-01-10 | 2014 | 11 |

我想得到这样的东西:

shop_id | year | month | 18 < age < 25 | 26 < age < 35
--------+------+-------+---------------+-------------
567   |  2014  | 10    | 2             | 1
234   |  2014  | 10    | 1             | 0
123   |  2014  | 10    | 0             | 0

第一次查询,没有管理一个店铺没有顾客的情况。没有怎么取0?

如何同时查询多个日期范围?

使用案例语句代替过滤器:

select year, month, shop_id, 
count(case when birthday between <range1> then 1 end) RANGE1,
count(case when birthday between <range2> then 1 end) RANGE2,
count(case when birthday between <range3> then 1 end) RANGE3
from customers 
group by year, month, shop_id;

"No rows with zeros" 是 GROUP BY 查询的常见问题。解决办法是让你的 FROM 成为任何 table 有完整列表的东西,然后做一个 LEFT JOIN。由于您也按年和月分组,因此您需要生成完整的年月列表。您可以使用 generate_series:

SELECT  t.t, s.id, COUNT(c.birthday) 
FROM    shops s
CROSS JOIN generate_series('2014-01-01 00:00:00', '2015-01-01 00:00:00', interval '1 month') t(t)
LEFT OUTER JOIN customers c
ON      c.shop_id = s.id
AND     c.birthday 
        BETWEEN '1992-01-01 00:00:00' AND '1998-01-01 00:00:00'
AND     c.year = EXTRACT(YEAR FROM t.t)
AND     c.month = EXTRACT(MONTH FROM t.t)
GROUP BY t.t, s.id
ORDER BY s.id, t.t;

要获取两个日期范围的计数,您可以按照@mo2 的建议进行操作,或者您可以加入 customers table 两次:

SELECT  t.t, s.id, COUNT(DISTINCT c1.id), COUNT(DISTINCT c2.id) 
FROM    shops s
CROSS JOIN generate_series('2014-01-01 00:00:00', '2015-01-01 00:00:00', interval '1 month') t(t)
LEFT OUTER JOIN customers c1
ON      c1.shop_id = s.id
AND     c1.birthday 
        BETWEEN '1992-01-01 00:00:00' AND '1998-01-01 00:00:00'
AND     c1.year = EXTRACT(YEAR FROM t.t)
AND     c1.month = EXTRACT(MONTH FROM t.t)
LEFT OUTER JOIN customers c2
ON      c2.shop_id = s.id
AND     c2.birthday 
        BETWEEN '1982-01-01 00:00:00' AND '1992-01-01 00:00:00'
AND     c2.year = EXTRACT(YEAR FROM t.t)
AND     c2.month = EXTRACT(MONTH FROM t.t)
GROUP BY t.t, s.id
ORDER BY s.id, t.t;

请注意,在这两个查询中我都SELECT输入完整的日期时间而不是yearmonth。我认为这更灵活,但如果您愿意,应该很容易更改。

编辑: 我意识到你的 yearmonth 不是生日相关的,而是其他的,我猜是访问日期?所以我更新了我的查询。如果您一次只检查一个月,则可以删除 generate_series 并将年和月整数直接放入连接条件中。