我有一个带有created
时间戳和id
标识符的表。
我可以通过以下方式获得id
每周唯一的数量:
SELECT date_trunc('week', created)::date AS week, count(distinct id)
FROM my_table
GROUP BY week ORDER BY week;
现在我想获得每周created
唯一的累计数量,如下所示:id
SELECT date_trunc('week', created)::date AS week, count(distinct id),
(SELECT count(distinct id)
FROM my_table
WHERE date_trunc('week', created)::date <= week) as acc
FROM my_table
GROUP BY week ORDER BY week;
但这不起作用,因为week
在 sub select ( ERROR: column "week" does not exist
) 中无法访问。
我该如何解决这个问题?
我正在使用 PostgreSQL