使用聚合函数进行DISTINCT ON

时间:2017-08-23 17:50:26

标签: postgresql distinct-on

我想让Postgres中每个用户拥有最多浏览量的项目。这是我到目前为止的代码:

  SELECT DISTINCT ON (user_id)
    project, user_id, COUNT(view_id) views
  FROM
    table_name
  GROUP BY
    user_id
  ORDER BY
    user_id, views DESC, project

0 个答案:

没有答案