试下:
select a.name, a.productplace, a.price from fruitablt a inner join (select productplace, max(price) as price from fruitablt group by productplace) b on a.price = b.price and a.productplace = b.productplace
select ft.name,ft.ProductPlace,ft.price from #fruitTablt ft where ft.price=(select MAX(price) from #fruitTablt ft2 where ft.ProductPlace=ft2.ProductPlace group by ProductPlace );
同意一楼。
一楼已经很理想了, 还可以用 row_number(), partition by
select * from( select *, row_number = row_number() over (partition by productplace order by price desc) from fruitTable ) b where b.row_number = 1
select * from fruitTablt where price in (
select a.price from
(select max(price) as price ,ProductPlace ,name from fruitTablt group by ProductPlace ,name )a )
感觉还是row_number 好用,对于排序的问题,建议使用row_number
WITH CTA AS (
SELECT '苹果' name ,'中国' ProductPlace,1.1 price UNION ALL
SELECT '苹果','日本',1.0 UNION ALL
SELECT '苹果','美国',2.0 UNION ALL
SELECT '橘子','中国',3.0 UNION ALL
SELECT '香蕉','中国',3.1 UNION ALL
SELECT '香','中国',3.1 UNION ALL
SELECT '梨子','美国',3.0
)
SELECT name,ProductPlace,price FROM (
SELECT * , MAX(price) OVER(PARTITION BY ProductPlace) expPri FROM CTA
) a WHERE price = exppri
这个可以查出并列最贵