SQL查询:3列表,只搜索"John“的最大值返回城市。
数据:
Name | City | Value <br>
John | LDN | 50 <br>
Joey | MCR | 12<br>
Dave | BHM | 5<br>
John | NTH | 56 <br>预期结果:第n行(第4行)
我怎样才能做到这一点?提前谢谢。
发布于 2020-08-24 10:40:16
您可以使用row_number()
select city from
(
select *, row_number() over(partition by name order by value desc) as rn
from tablename
)A where rn=1 and name='John'或者,
select city from tablename t
where name='John' and value = (select max(value) from tablename t1 where t.name=t1.name)发布于 2020-08-24 10:50:58
您可以使用order by和一些限制结果的方法:
select t.*
from t
order by value desc
fetch first 1 row only;一些数据库使用select top (1)或limit 1限制单个行。
https://stackoverflow.com/questions/63559395
复制相似问题