每个时间段的唯一值

时间:2017-06-16 22:01:21

标签: sql postgresql

在我的表trips中,我有两列:created_atuser_id

我的目标是通过postgres中的查询计算每月唯一user_id秒。到目前为止,我写了这个 - 但它返回错误

SELECT user_id, 
       to_char(created_at, 'YYYY-MM') as t COUNT(*) 
FROM (SELECT DISTINCT user_id 
      FROM trips) group by t;

我该如何更改此查询?

1 个答案:

答案 0 :(得分:0)

查询比这简单得多:

SELECT to_char(created_at, 'YYYY-MM') as yyyymm, COUNT(DISTINCT user_id)
FROM trips
GROUP BY yyyymm
ORDER BY yyyymm;