SQL 一对多联表查询最大值

有两个数据表City表和Price表,CIty表的结构如下:

Price表的结构如下:

查询每个城市最大的销售价格,并以最大价格进行降序排列,选取前5条记录,SQL语句的代码如下:

select top 5 * from (select ROW_NUMBER() over(order by TmpTable.AdultyPrice desc) NID, City.*, TmpTable.AdultyPrice from City,(select MAX(Price.AdultyPrice) as AdultyPrice,PID from City inner join Price on City.LID = Price.PID group by Price.PID) as TmpTable where LID = PID) as tmp  

运行结果:

posted @ 2018-05-21 21:51  Nullobj  阅读(4348)  评论(0编辑  收藏  举报