Postgresql-窗口函数聚合

Postgresql-窗口函数聚合,sql,postgresql,aggregate-functions,window-functions,Sql,Postgresql,Aggregate Functions,Window Functions,我正在尝试按产品类型查找每月新用户的数量。但是,我继续收到一个错误,请求在聚合函数中使用cnt SELECT EXTRACT(MONTH FROM date) AS month FROM (SELECT users.date, COUNT(*) OVER(PARTITION BY product_type) AS cnt FROM users) AS u GROUP BY month ORDER BY cnt DESC; 这似乎是一个非常奇怪的结构。下面是一个不使用窗

我正在尝试按产品类型查找每月新用户的数量。但是,我继续收到一个错误,请求在聚合函数中使用
cnt

SELECT EXTRACT(MONTH FROM date) AS month
FROM (SELECT users.date,
             COUNT(*) OVER(PARTITION BY product_type) AS cnt FROM users) AS u
GROUP BY month
ORDER BY cnt DESC;

这似乎是一个非常奇怪的结构。下面是一个不使用窗口函数的方法:

select date_trunc('month', date) as  yyyymm, product_id, count(*)
from (select distinct on (u.userid) u.*
      from users u
      order by u.userid, u.date
     ) u
group by date_trunc('month', date), product_id
order by yyyymm, product_id;

编辑您的问题并提供示例数据和所需结果。