Get most common value for each value of another column in SQL

前端 未结 9 1593
生来不讨喜
生来不讨喜 2020-11-30 02:29

I have a table like this:

 Column  | Type | Modifiers 
---------+------+-----------
 country | text | 
 food_id | int  | 
 eaten   | date | 
<
9条回答
  •  伪装坚强ぢ
    2020-11-30 03:12

    select country,food_id, count(*) ne  
    from   food f1  
    group by country,food_id    
    having count(*) = (select max(count(*))  
                       from   food f2  
                       where  country = f1.country  
                       group by food_id)  
    

提交回复
热议问题