我有两张这样的桌子。
create table teams (
"ID" Integer NOT NULL ,
"STADIUM_ID" Integer NOT NULL ,
"NAME" Varchar2 (50) NOT NULL ,
primary key ("ID")
) ;
create table matches (
"ID" Integer NOT NULL ,
"WINNER_ID" Integer NOT NULL ,
"OPPONENT_ID" Integer NOT NULL ,
"WINNERSCORE" Integer,
"OPPONENTSCORE" Integer,
primary key ("ID","WINNER_ID","OPPONENT_ID")
) ;
他们有以下数据:
select * from matches;
ID WINNER_ID OPPONENT_ID WINNERSCORE OPPONENTSCORE
---------- ---------- ----------- ----------- -------------
1 5 2 5 2
2 4 5 1 0
3 3 2 1 0
4 3 2 1 0
5 1 2 2 0
6 3 1 2 1
select * from teams;
ID STADIUM_ID NAME
---------- ---------- -----------
1 1 Team1
2 3 Team2
3 4 Team3
4 2 Team4
5 5 Team5
我需要得到每个团队的目标总和。 为此目的,尝试了以下查询并得到以下结果:
select name,
(select sum(opponentscore)
from matches
where opponent_id = teams.id) +
(select sum(winnerscore) from matches where winner_id = teams.id) sum
from teams;
NAME SUM
-------------------------------------------------- ----------
Team1 3
Team2
Team3
Team4
Team5 5
你有什么建议吗?
答案 0 :(得分:2)
您需要的是分别计算每个团队的 opponentscore 和 winnerscore ,并将它们与 UNION ALL 结合起来:
select name, sum(score) total_score
from
(
select name, sum(winnerscore) score
from teams t join matches m on ( t.id = m.winner_id )
group by name
union all
select name, sum(opponentscore) score
from teams t join matches m on ( t.id = m.opponent_id )
group by name
)
group by name
order by 1;
答案 1 :(得分:0)
你应该使用join和group by
select name, sum(matches.opponentscore) + sum(matches.winnerscore) my_sum
from matches
inner join teams on teams.id = matches.winner_id
group by teams.name
答案 2 :(得分:0)
您可以使用表格匹配两次加入表团队:
SELECT name, SUM(wonMatches.WINNERSCORE + lostMatches.OPPONENTSCORE) as goals
FROM (teams INNER JOIN matches as wonMatches ON teams.ID = wonMatches.WINNER_ID)
INNER JOIN matches as lostMatches ON teams.ID = lostMatches.OPPONENT_ID
GROUP BY name
答案 3 :(得分:0)
我的解决方案是:更改数据库架构。重新考虑您的应用程序的要求。此架构无法满足用户期望的价值。
从我看到的情况来看,我会说你正在尝试为想要跟踪他们的球队/最喜欢的球员进度的球迷建立一个应用程序,以便他们可以吹嘘。
话虽这么说,最后我会得到那些表格:
所以现在,我相信编写查询会更加简单。你只需要加入球队,球员,计算目标和按球队分组。
答案 4 :(得分:0)
问题在于NULL
s - 当找不到结果时子查询返回NULL
,NULL + anything == NULL
。
最直接的解决方法是:
select name,
nvl(
(select sum(opponentscore) from matches where opponent_id = teams.id),
0
)
+
nvl(
(select sum(winnerscore) from matches where winner_id = teams.id),
0
) sum
from teams;
出于性能原因,您可能需要考虑按照其他人的建议使用GROUP BY的联接查询。