如何 SQL DML - sql-ex.ru 练习 18
How to SQL DML - sql-ex.ru exercise 18
我已经处理这个查询好几次了,我设法找到了一个解决方案,但在练习解决方案本身,它说我得到了正确数量的结果,但缺少一些东西。
这是他们提出的练习。
You should add from the Outcomes table to the Ships table all the head ships (first ship in a class), which are absent in the Ships table.
Launching year is the average index with accuracy to within 1 year for country ships of a given ship.
If this average year is unknown, the record must not be inserted into the table.
“您的查询在第一个(可用)数据库上返回了正确的数据集,但在第二个检查数据库上返回了错误的数据集。
* 错误的记录数(减去 12)”,这是他们给我的提示。
"Check up whether you add the same ship more than once if she took part in several battles."
这是我写的查询:
insert into ships
select
ship, classes.class,
(select round(avg(1.0 * launched), 0, 0)
from ships
join classes on ships.name = classes.class
where
launched is not null
group by country) as l_avgo
from
outcomes
join
classes on outcomes.ship = classes.class
where
not exists (select name from ships )
group by
outcomes.ship, classes.class, classes.country
having
count(ship) = 1
union
select
name, ships.class, round(avg(1.0 * launched), 0, 0) as l_avgs
from
ships
join
outcomes on ships.name = outcomes.ship
where
launched is not null
group by
ships.name, ships.class
这是数据库描述
参加世界War II 的海军舰艇数据库正在考虑中。数据库具有以下关系:
- 类(class, 类型, 国家, numGuns, bore, displacement)
- 飞船(名称,class,下水)
- 战斗(姓名、日期)
- 结果(飞船、战斗、结果)
classes 中的船舶被安排到一个项目中。 A class 通常分配给正在考虑的 class 中第一艘船的名称(头船);否则,class 名称与数据库中的任何船舶名称都不一致。
类 关系包括 class 名称、类型(bb 表示战舰,或 bc 表示战列巡洋舰)、船舶建造国家、主炮数量、火炮口径(主炮直径)枪管,以英寸为单位)和排水量(以吨为单位的重量)。 Ships 关系包括船名、class 名称和发射年份。 Battles 关系包含船只参加的战斗的名称和日期;而他们参与战斗的结果(沉没、受损或未受伤 - 好的)在 Outcomes 关系中。
注:1) Outcomes 关系可能包括Ships 关系中未包含的船舶。 2) 之后沉船不能参战
insert into ships
select ship name,class,launched from
(select distinct ship,c.class,country
from outcomes o join classes c on c.class=o.ship
where ship not in(select distinct name from ships)) f join
(select country,round(avg(launched*1.0),0) launched
from ships s join classes c on c.class=s.class
group by country) n on f.country = n.country
where n.launched is not null
我已经处理这个查询好几次了,我设法找到了一个解决方案,但在练习解决方案本身,它说我得到了正确数量的结果,但缺少一些东西。
这是他们提出的练习。
You should add from the Outcomes table to the Ships table all the head ships (first ship in a class), which are absent in the Ships table.
Launching year is the average index with accuracy to within 1 year for country ships of a given ship.
If this average year is unknown, the record must not be inserted into the table.
“您的查询在第一个(可用)数据库上返回了正确的数据集,但在第二个检查数据库上返回了错误的数据集。 * 错误的记录数(减去 12)”,这是他们给我的提示。 "Check up whether you add the same ship more than once if she took part in several battles."
这是我写的查询:
insert into ships
select
ship, classes.class,
(select round(avg(1.0 * launched), 0, 0)
from ships
join classes on ships.name = classes.class
where
launched is not null
group by country) as l_avgo
from
outcomes
join
classes on outcomes.ship = classes.class
where
not exists (select name from ships )
group by
outcomes.ship, classes.class, classes.country
having
count(ship) = 1
union
select
name, ships.class, round(avg(1.0 * launched), 0, 0) as l_avgs
from
ships
join
outcomes on ships.name = outcomes.ship
where
launched is not null
group by
ships.name, ships.class
这是数据库描述
参加世界War II 的海军舰艇数据库正在考虑中。数据库具有以下关系:
- 类(class, 类型, 国家, numGuns, bore, displacement)
- 飞船(名称,class,下水)
- 战斗(姓名、日期)
- 结果(飞船、战斗、结果)
classes 中的船舶被安排到一个项目中。 A class 通常分配给正在考虑的 class 中第一艘船的名称(头船);否则,class 名称与数据库中的任何船舶名称都不一致。 类 关系包括 class 名称、类型(bb 表示战舰,或 bc 表示战列巡洋舰)、船舶建造国家、主炮数量、火炮口径(主炮直径)枪管,以英寸为单位)和排水量(以吨为单位的重量)。 Ships 关系包括船名、class 名称和发射年份。 Battles 关系包含船只参加的战斗的名称和日期;而他们参与战斗的结果(沉没、受损或未受伤 - 好的)在 Outcomes 关系中。 注:1) Outcomes 关系可能包括Ships 关系中未包含的船舶。 2) 之后沉船不能参战
insert into ships
select ship name,class,launched from
(select distinct ship,c.class,country
from outcomes o join classes c on c.class=o.ship
where ship not in(select distinct name from ships)) f join
(select country,round(avg(launched*1.0),0) launched
from ships s join classes c on c.class=s.class
group by country) n on f.country = n.country
where n.launched is not null