sql-ex exercise 39在第二次服务器错误时失败了吗?

时间:2013-10-22 17:27:20

标签: sql sql-server sql-server-2008

  

练习39:定义“为未来的战斗而存活”的船只;   他们在一场战斗中受伤,参加了另一场战斗。

数据库架构:http://www.sql-ex.ru/help/select13.php#db_3

我的方法:

SELECT distinct o.ship from Outcomes o
WHERE o.RESULT = 'damaged' 
AND exists (select 1 FROM Outcomes o2 WHERE o2.ship = o.ship 
AND (o2.result='OK' OR o2.result='sunk'))

sql-ex说

  

您的查询在主数据库上生成了正确的结果集,但失败了   第二次测试,检查数据库

正确的结果与我的输出匹配。

我失败的地方?

6 个答案:

答案 0 :(得分:2)

解决!需要添加Distinct

select distinct kk.ship from
(Select ship,date from outcomes oo,battles bb where oo.battle=bb.name) tt 
inner join 
(Select ship,date as damagedate from outcomes oo,battles bb where result='damaged' AND oo.battle=bb.name) kk 
ON tt.ship=kk.ship AND tt.date>kk.damagedate

答案 1 :(得分:1)

with a as (select * from outcomes o join battles b on o.battle=b.name)select distinct a1.ship from a a1, a a2 where a1.ship=a2.ship and a1.result='damaged'and a1.date<a2.date

答案 2 :(得分:0)

试试这个:

    select o.ships from (select a.ships, a.battle from outcomes as a where result in (damaged, unharmed)) as o
    where o.battle <> a.battle

答案 3 :(得分:0)

select distinct outcomes.ship
from outcomes, (select outcomes.ship, battles.[date], outcomes.result
            from outcomes, battles
            where battles.name = outcomes.battle
            and result = 'damaged' ) t1, 
 battles
where outcomes.ship = t1.ship and
outcomes.battle = battles.name and
battles.[date] > t1.[date]

答案 4 :(得分:0)

这是我的答案,并没有在页面上解析为真,但我不同意。不知道我的解决方案与你的解决方案有什么不同

select distinct damaged.ship from 
(Select oo.ship,date from outcomes oo
inner join battles bb on oo.battle=bb.name
) as ships
inner join (Select ship,date as damagedate from outcomes oo1
inner join battles bb1 on oo1.battle=bb1.name and result = 'damaged') as damaged
on (ships.ship = damaged.ship and ships.date != damaged.damagedate)

答案 5 :(得分:0)

也许这不是最棒的设计,但对我也有用:

with damaged as (Select b.date, o.ship
from outcomes o 
inner join battles b on b.name = o.battle
where result = 'damaged') 

Select distinct o.ship
from outcomes o 
right join damaged d on d.ship = o.ship
right join battles b on b.name = o.battle
where b.date > d.date