你可以使用
DISTINCT
在…内
COUNT
:
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