简体   繁体   English

来自多个联接的SQL错误SUMS

[英]SQL Incorrect SUMS from multiple JOINS

I'm trying to sum multiple tables using Joins and Sums in MySQL and not having much success. 我正在尝试使用MySQL中的Joins和Sums对多个表求和,但没有成功。

My Tables (Unnecessary Columns Removed) 我的表(删除了不必要的列)

Students 学生们

idStudent   studentname   studentyear
1           foobar        11
2           barfoo        11
3           thing         8

Athletics_Results Athletics_Results

idResult   idStudent   points
1          1           14
2          1           11
3          3           7
4          2           9

Team_Results Team_Results

idTeamResults   year   points
1               11     9
2               8      8
3               7      14

So let me explain about the tables, because I admit they're poorly named and designed. 因此,让我解释一下表格,因为我承认它们的名称和设计不当。

Students holds the basic info about each student, including their year and name. 学生拥有有关每个学生的基本信息,包括他们的年龄和姓名。 Each student has a unique ID. 每个学生都有一个唯一的ID。

Athletics_Results stores the results from athletics events. Athletics_Results存储田径比赛的结果。 The idStudent column is a foreign key and relates to idStudent in the student column. idStudent列是一个外键,与Student列中的idStudent相关。 So student foobar (idStudent 1) has scored 14 and 11 points in the example. 因此,在示例中,学生foobar(idStudent 1)分别获得了14分和11分。

Team_Results stores results from events that more than one student took part in. It just stores the year group and points. Team_Results存储来自多个学生参加的活动的结果。它仅存储年份组和分数。

The Aim 目的

I want to be able to produce a sum of points for each year - combined from both athletics_results and team_results. 我希望能够产生每年的总积分-来自sports_results和team_results的总和。 EG: 例如:

year   points
7      14     <-- No results in a_r, just 14 points in t_r
8      15     <-- 7 points in a_r (idResult 4) and 8 in t_r
11     43     <-- 14, 11, 9 points in a_r and 9 in t_r

What I've tried For testing purposes, I've not tried combining the a_r scores and t_r scores yet but left them as two columns so I can see what's going on. 我尝试过的内容为了进行测试,我还没有尝试将a_r得分和t_r得分结合起来,而是将它们保留为两列,以便了解发生了什么。

The first query I tried: 我尝试的第一个查询:

SELECT students.studentyear as syear, SUM(athletics_results.points) as score, SUM(team_results.points) as team_score
FROM students
JOIN team_results ON students.studentyear = team_results.year
JOIN athletics_results ON students.idStudent = athletics_results.idStudent
GROUP BY syear;

This gave different rows for each year (as desired) but had incorrect SUMS. 每年(根据需要)给出不同的行,但是SUMS不正确。 I learnt this was due to not grouping the joins. 我了解到这是由于未对联接进行分组。

I then created this code: 然后,我创建了以下代码:

SELECT studentyear as sYear, teamPoints, AthleticsPoints
FROM students st

JOIN    (SELECT year, SUM(tm.points) as teamPoints
        FROM team_results tm
        GROUP BY year) tr ON st.studentyear = tr.year 

JOIN    (SELECT idStudent, SUM(atr.points) as AthleticsPoints
        FROM athletics_results atr
        ) ar ON st.idStudent = ar.idStudent

Which gave correct SUMS but only returned one year group row (eg the scores for Year 11). 给出正确的SUMS,但仅返回一年的组行(例如,11年级的分数)。

EDIT - SQLFiddle here: http://sqlfiddle.com/#!9/dbc16/ . 编辑-SQLFiddle在这里: http ://sqlfiddle.com/#!9/dbc16/。 This is with my actual test data which is a bigger sample than the data I posted here. 这是我的实际测试数据,比我在此处发布的数据更大。

http://sqlfiddle.com/#!9/ad111/7 http://sqlfiddle.com/#!9/ad111/7

SELECT tr.`year`,  COALESCE(tr.points,0)+COALESCE(SUM(ar.points),0)
FROM Team_Results tr
LEFT JOIN Students s
ON tr.`year`=s.studentyear
LEFT JOIN Athletics_Results ar
ON s.idStudent = ar.idStudent
GROUP BY tr.year

According to your comment and fiddle provided check http://sqlfiddle.com/#!9/dbc16/3 根据您的评论和提琴提供的内容检查http://sqlfiddle.com/#!9/dbc16/3

SELECT tr.`year`,  COALESCE(tr.points,0)+COALESCE(SUM(ar.points),0)
FROM (
  SELECT `year`, SUM(points) as points
  FROM Team_Results
  GROUP BY `year`) tr
LEFT JOIN Students s
ON tr.`year`=s.studentyear
LEFT JOIN Athletics_Results ar
ON s.idStudent = ar.idStudent
GROUP BY tr.year

Can be done in multiple ways. 可以通过多种方式完成。 My first thought is: 我的第一个想法是:

SELECT idStudent, year, SUM(points) AS totalPoints FROM (
SELECT a.idStudent, c.year, a.points+b.points AS points
FROM students a
INNER JOIN Athletics_Results b ON a.idStudent=b.idStudent
INNER JOIN Team_Results c ON a.studentyear=c.year) d
GROUP BY idStudent,year

Try this http://sqlfiddle.com/#!9/2bfb1/1/0 试试这个http://sqlfiddle.com/#!9/2bfb1/1/0

SELECT 
    year, SUM(points)
FROM
    ((SELECT 
        a.year, SUM(b.points) AS points
    FROM
        student a
    JOIN at_result b ON b.student_id = a.id
    GROUP BY a.year) UNION (SELECT 
        a.year, SUM(a.points) AS points
    FROM
        t_result a
    GROUP BY a.year)) c
GROUP BY year;

On your data I get: 根据您的数据,我得到:

   year      points 
    7          14
    8          15
    11         43

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM