0

我需要有关此查询的帮助。对于这个问题,我们有两个关于战舰和他们参加的战斗的模式: Ships(name, yearLaunched, country, numGuns, gunSize,displacement) Battles(ship, BattleName, result) 问题如下:写一个查询拥有枪支尺寸第二大的枪支。更准确地说,无论有多少其他船的火炮尺寸比这大一号,找出火炮尺寸仅超过一种火炮尺寸的船只。列出船只的名称和它们的火炮尺寸。我试图解决这个问题,我的答案如下:

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)

感谢您花时间阅读并回答!

4

2 回答 2

3
SELECT name, gunSize
  FROM (SELECT name, gunSize, dense_rank() OVER (ORDER BY gunSize DESC) r)
 WHERE r = 2
于 2013-10-06T19:19:12.803 回答
1

尝试以下操作:

Select name, gunSize from ships
where gunSize=(
  select max(gunSize) as SecondBiggest from ships 
  where gunSize<(select max(gunSize) as Biggest from ships)
  )

编辑提示SecondBiggestBiggest别名用于阐明每个子查询的作用。

于 2013-10-06T19:22:09.927 回答