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. So customer1 must have B and customer2 must have C (I'm using postgresql)
You want the "mode" in statistics. Here is one method to get exactly one value per x (ignoring duplicates):
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.