【问题标题】:Group data based on higest value of a partcular column根据特定列的最高值对数据进行分组
【发布时间】:2022-07-11 19:38:02
【问题描述】:
 I have two tables department and category.In my query result listing same department multiple times.I want to group departments based on category table priority column.

 Eg Data:

 id    dept_name     dept_category  priority

 1      Cardio         category 2     2
 2      Ortho          category 3     3
 3      Ortho          category 1     1
 4      ENT            category 1     1
 5      Ortho          category 2     2

I wannt the reslt like:

 id    dept_name     dept_category  priority

 1      Cardio         category 2     2
 3      Ortho          category 1     1
 4      ENT            category 1     1

如何获取这样的结果。在我的类别表中,优先级为 1、2、3,然后 1 为最高优先级。我需要根据最高优先级对部门进行分组。

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    我们可以在 Postgres 中使用DISTINCT ON

    SELECT DISTINCT ON (dept_name) *
    FROM yourTable
    ORDER BY dept_name, priority;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-06
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多