with cus as (
select
distinct country,
sum(fruit) over(partition by country) as fruit,
sum(veg) over (partition by country) as veg,
sum(meat) over (partition by country) as meat
from
customers
)
select
country,
case when fruit > veg
AND fruit > meat THEN 'fruit' WHEN veg > fruit
AND veg > meat THEN 'veg' WHEN meat > fruit
AND meat > veg THEN 'meat' end as popular_product
from
cus;
| 国家|流行产品| | --|--| | 德国|肉| | 印度|水果| | 西班牙|水果| | 美国|肉| View on DB Fiddle
1条答案
按热度按时间rxztt3cl1#
Schema(MySQL v8.0)
查询
| 国家|流行产品|
| --|--|
| 德国|肉|
| 印度|水果|
| 西班牙|水果|
| 美国|肉|
View on DB Fiddle