我需要这个查询的帮助。对于这个问题,我们有两个关于战列舰及其战斗的模式: 船舶(名称,年份,国家,号码,枪支,排量) 战斗(船,战斗名称,结果) 问题如下:写一个查询,其中战列舰拥有第二大枪支的枪支。更准确地说,找到枪支尺寸仅超过一个枪支尺寸的船只,无论其他船只有多大的枪支尺寸。列出船舶名称及其枪支尺寸。 我试图解决问题,我的答案如下:
Select smax.name, smax.gunSize
From ships smax
Where ((select s.name,s.gunSize
from Ships s
where s.gunSize NOT EXISTS ( select ss.gunSize from ships ss where ss.gunSize >= ALL(select
ss1.gunSize from ships ss1))) AS temp)
AND smax.gunSize >= ALL (select temp.gunSize from ships temp)
感谢您花时间阅读并回答!
答案 0 :(得分:3)
SELECT name, gunSize
FROM (SELECT name, gunSize, dense_rank() OVER (ORDER BY gunSize DESC) r)
WHERE r = 2
答案 1 :(得分:0)
尝试以下方法:
Select name, gunSize from ships
where gunSize=(
select max(gunSize) as SecondBiggest from ships
where gunSize<(select max(gunSize) as Biggest from ships)
)
编辑:提示:SecondBiggest
和Biggest
别名用于说明每个子查询的作用。