遵循以下编程练习:SQL with Street Fighter,其语句为:
是时候评估世界上最伟大的战斗机中的哪一个通过了进入街头霸王世界半决赛的6个令人垂涎的名额格斗锦标赛。每年的每一场比赛都被记录下来每个战士的输赢都需要加起来。
桌子上的每一排战斗机都记录了战斗机的名字,他们是赢了(1(还是输了(0(,以及他们的移动类型结束了比赛。
id name won lost move_id
winning_moves
id move
然而,由于新的健康和安全规定,所有ki爆炸都有作为潜在的火灾隐患被宣布为非法。任何以Hadoken、Shoouken或Kikoken不应计入总胜利以及损失。
所以,你的工作:
Return name, won, and lost columns displaying the name, total number of wins and total number of losses. Group by the fighter's
名称。如果获胜的动作是哈多肯、肖奥肯或基科肯,则不要计算任何输赢。从最多胜到最少的顺序返回顶部6。别担心领带。
我们怎么能根据战士的名字对他们进行分组?
我们已经尝试过:
select name, won, lost from fighters inner join winning_moves on fighters.id=winning_moves.id
组按名称顺序赢得desc限制6;
但是它显示:
SQL查询出现错误:
PG::GroupingError:ERROR:列"fights.won"必须出现在GROUP BY子句或用于聚合函数LINE 3:selectname,赢了,输了从战士内部加入winning_move。。。
此外,我们还尝试包括所有选定的行:
select name, won, lost from fighters inner join winning_moves on fighters.id=winning_moves.id
group by name,won,lost order by won desc limit 6;
但结果与预期不同。
预期:
name won lost
Sakura 44 15
Cammy 44 17
Rose 42 19
Karin 42 13
Dhalsim 40 15
Ryu 39 16
实际:
name won lost
Vega 2 1
Guile 2 1
Ryu 2 1
Rose 1 0
Vega 1 0
Zangief 1 0
此外,我们已经阅读:
- https://www.w3schools.com/sql/sql_join.asp
- 带有WHERE子句的MySql内部联接
- 如何限制PostgreSQL SELECT中的行
- https://www.w3schools.com/sql/sql_groupby.asp
- GROUP BY子句或在聚合函数中使用
- PostgreSQL列必须出现在GROUP BY子句中,或者在使用case语句时用于聚合函数
- 必须出现在GROUP BY子句中或在聚合函数中使用
我想你需要sum((来聚合id的胜利和失败。除此之外,你不需要加入,因为你不想在第一个查询中显示移动
select name, sum(won) as wins,
sum(lost)
from fighters
group by name order by sum(won)
desc limit 6;