简体   繁体   English

组中每个子组的最大值

[英]Max of each subgroup in a group

So I have a table with following data : 所以我有一个表,其中包含以下数据:

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

So I need to find what max(Y) does each customer have. 因此,我需要找到每个客户拥有的max(Y)。 So customer1 must have B and customer2 must have C (I'm using postgresql) 因此,customer1必须具有B,customer2必须具有C(我正在使用PostgreSQL)

You want the "mode" in statistics. 您需要统计中的“模式”。 Here is one method to get exactly one value per x (ignoring duplicates): 这是一种获取每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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM