组中每个子组的最大值

时间:2017-09-18 19:53:21

标签: sql database postgresql relational-database

所以我有一张包含以下数据的表格:

X------------------Y
customer1------A
customer1------A
customer1------B
customer2------B
customer2------B
customer2------B
customer2------C
customer2------C
customer2------C
customer2------C
customer2------C

所以我需要找出每个客户的max(Y)。所以customer1必须有B,customer2必须有C(我正在使用postgresql)

1 个答案:

答案 0 :(得分:0)

你想要"模式"在统计中。这是一种方法,每个x只能得到一个值(忽略重复):

select distinct on (x) x, y
from (select x, y, count(*) as cnt
      from t
      group by x, y
     ) xy
order by x, cnt desc;