Postgresql 使用Postgres中每个客户的记录计算天数
假设我得到了以下数据(注意日期差异的id): 我想得到客户记录的天数。如果一个客户在一天内做了多个记录,那么它仍然计为1。因此,上述数据的输出应为:Postgresql 使用Postgres中每个客户的记录计算天数,postgresql,distinct,Postgresql,Distinct,假设我得到了以下数据(注意日期差异的id): 我想得到客户记录的天数。如果一个客户在一天内做了多个记录,那么它仍然计为1。因此,上述数据的输出应为: customer_id count 1000 4 1001 3 1002 2 我尝试了不同的查询,尝试使用to_char(在'YYYY-mm-dd'创建)和DISTINCT ON(在创建)和count,但我没有得到我想要的聚合结果。F.e.: SELECT distinct on (to_char
customer_id count
1000 4
1001 3
1002 2
我尝试了不同的查询,尝试使用to_char(在'YYYY-mm-dd'创建)
和DISTINCT ON(在创建)
和count
,但我没有得到我想要的聚合结果。F.e.:
SELECT distinct on (to_char(created_at, 'YYYY-mm-dd')) count(customer_id), customer_id
FROM registration
WHERE created_at >= '2017-12-29' and created_at <= '2018-01-03' and customer_id in (1000,1001,1002)
group by customer_id, created_at;
在(到字符(在'YYYY-mm-dd'创建)计数(客户id)、客户id上选择不同
从注册开始
其中创建时间>='2017-12-29'和创建时间在派生表中使用distinct
(from
子句中的子查询):
实际上,用户1001活动了3天,而不是4天。在派生表中使用distinct
(from
子句中的子查询):
实际上,用户1001活动了3天,而不是4天。您可以在
计数中使用不同的:
WITH sample (customer_id, created_at) AS (
VALUES
(1000, '2017-12-29 20:48:54+00'::TIMESTAMP),
(1000, '2017-12-30 12:48:56+00'),
(1000, '2017-12-30 12:49:26+00'),
(1002, '2017-12-30 12:52:36+00'),
(1001, '2017-12-30 12:54:15+00'),
(1002, '2017-12-30 13:54:15+00'),
(1001, '2017-12-30 13:56:58+00'),
(1000, '2018-01-02 13:01:13+00'),
(1001, '2018-01-02 20:29:19+00'),
(1002, '2018-01-02 20:29:31+00'),
(1000, '2018-01-03 20:30:28+00'),
(1001, '2018-01-03 20:38:40+00')
)
SELECT
customer_id,
COUNT(DISTINCT created_at::DATE)
FROM
sample
GROUP BY
customer_id
您可以在COUNT
内部使用DISTINCT
:
WITH sample (customer_id, created_at) AS (
VALUES
(1000, '2017-12-29 20:48:54+00'::TIMESTAMP),
(1000, '2017-12-30 12:48:56+00'),
(1000, '2017-12-30 12:49:26+00'),
(1002, '2017-12-30 12:52:36+00'),
(1001, '2017-12-30 12:54:15+00'),
(1002, '2017-12-30 13:54:15+00'),
(1001, '2017-12-30 13:56:58+00'),
(1000, '2018-01-02 13:01:13+00'),
(1001, '2018-01-02 20:29:19+00'),
(1002, '2018-01-02 20:29:31+00'),
(1000, '2018-01-03 20:30:28+00'),
(1001, '2018-01-03 20:38:40+00')
)
SELECT
customer_id,
COUNT(DISTINCT created_at::DATE)
FROM
sample
GROUP BY
customer_id
是的,现在是3点而不是4点这台机器计算得更好。是的,它是3而不是4这台机器计算得更好。
WITH sample (customer_id, created_at) AS (
VALUES
(1000, '2017-12-29 20:48:54+00'::TIMESTAMP),
(1000, '2017-12-30 12:48:56+00'),
(1000, '2017-12-30 12:49:26+00'),
(1002, '2017-12-30 12:52:36+00'),
(1001, '2017-12-30 12:54:15+00'),
(1002, '2017-12-30 13:54:15+00'),
(1001, '2017-12-30 13:56:58+00'),
(1000, '2018-01-02 13:01:13+00'),
(1001, '2018-01-02 20:29:19+00'),
(1002, '2018-01-02 20:29:31+00'),
(1000, '2018-01-03 20:30:28+00'),
(1001, '2018-01-03 20:38:40+00')
)
SELECT
customer_id,
COUNT(DISTINCT created_at::DATE)
FROM
sample
GROUP BY
customer_id