我需要特定team_id的目标总和
例如:
如果team_id 16分别得分6和2,则另一个新列sum_goal中的总和为8
我无法继续下去。请帮助。
我尝试的查询是:
选择(match_id,Sum(Goal)From ( 从match_result_updation中选择team1_id ID,team1_goal目标 联盟全部 从match_result_updation中选择team2_id ID,team2_goal目标 )
as A)
这
(SELECT
c.tournament_id,g。*
来自team_trophies d
内部联接
tournament_match e
上
e.tournament_id = d.tournament_id
内部联接
tournament_scheduling c
上
c.tournament_id = e.tournament_id
内部联接
匹配f
上
f.match_id = c.id
内部联接
match_result_updation g
上
g.match_id = f.id
哪里
f.match_type ='锦标赛'
和
d.tournament_id = 1)作为p
按比例分组
从第二个选择到id = 1我得到了附件图片中的输出,但是我无法找到每个team_id的目标总和。 如果teamid 16或17,则team1或team2相应地计算目标总和。
答案 0 :(得分:1)
我的第一个想法是将它们放入列中,然后求和。 编辑任何SQL引擎
Select ID, Sum(Goal) From
(
Select Team1_ID ID, Team1_Goal Goal From Table
Union All
Select Team2_ID ID, Team2_Goal Goal From Table
) A
Group By ID
ps如果表格设计规范化,则不需要这种解决方法。
答案 1 :(得分:0)
使用此查询有助于实现目标总和
select team1_id, sum(team1_goal) as sum_goal from `table` group by `team1_id`;
答案 2 :(得分:0)
我们也可以这样做....
select team1_id, sum(team1_goal) as sum_goal from `table` group by `team1_id`
union all
select team2_id, sum(team2_goal) as sum_goal from `table` group by `team1_id`;
如果你想再次总结,那么可以像这样使用
select t.team_id, sum(t.sum_goal) as total_goal from (
select team1_id as team_id, sum(team1_goal) as sum_goal from `table` group by `team1_id`
union all
select team2_id as team_id, sum(team2_goal) as sum_goal from `table` group by `team1_id`
) as t group by t.team_id;
答案 3 :(得分:0)
试试这个
Select ID, Sum(Goal) From
(
Select Team1_ID ID, Team1_Goal Goal From Table
Union All
Select Team2_ID ID, Team2_Goal Goal From Table
)l,
table_name Group By ID
答案 4 :(得分:0)
另一个版本:
SELECT SUM(nr)
FROM (
(SELECT sum(team1_goal) as nr FROM goals WHERE team1_id = 16)
UNION ALL
(SELECT sum(team2_goal) as nr FROM goals WHERE team2_id = 16)
)
sum
答案 5 :(得分:0)
SELECT team1_id AS Team_ID , sum(team1_goal) as Team_Goal FROM teams GROUP BY team1_id UNION SELECT team2_id AS Team_ID , sum(team2_goal) as Team_Goal FROM teams GROUP BY team2_id
这将创建一个表,其中包含每个团队的ID以及每个团队ID的目标总数