我有带数据的汽车表
country | car | price
---------------------
Germany | Mercedes | 30000
Germany | BMW | 20000
Germany | Opel | 15000
Japan | Honda | 20000
Japan | Toyota | 15000
我需要从表中获得国家,汽车和价格,价格最高 对于每个国家
Germany Mercedes 30000
Japan Honda 20000
答案 0 :(得分:3)
试
select cars.* FROM cars
INNER JOIN (
select country, max(price) AS maxprice from cars
GROUP BY country
) m
ON cars.country = m.country AND cars.price = m.maxprice
答案 1 :(得分:1)
使用ROW_NUMBER()
SELECT *
FROM ( SELECT *,
ROW_NUMBER() OVER (PARTITION BY country
ORDER BY price DESC) as rn
FROM cars ) as T
WHERE T.rn = 1
如果允许绑定,请改用RANK
SELECT *
FROM ( SELECT *,
RANK() OVER (PARTITION BY country
ORDER BY price DESC) as rn
FROM cars ) as T
WHERE T.rn = 1