我有两个查询,我想将它们合并为一个查询
第一个查询:此查询查找每个主题中每个学生的平均值:
SELECT
StudentFirstName, StudentLastName, ClassName,
AVG(Grade) AS 'average for this subject'
FROM tests
INNER JOIN students ON tests.StudentID = students.StudentID
GROUP BY StudentFirstName, StudentLastName, ClassName;
第二个查询:此查询查找每个学生的平均成绩:
SELECT
StudentFirstName, StudentLastName, AVG(average) AS total_average
FROM
(SELECT
StudentFirstName, StudentLastName, AVG(Grade) AS average
FROM
tests
INNER JOIN
students ON tests.StudentID = students.StudentID
GROUP BY
StudentFirstName, StudentLastName, ClassName) AS t
GROUP BY
StudentFirstName, StudentLastName;
例如:我的成绩(又名Error404)是:
代数:第一次考试:99,第二次考试:97,第三次考试:96 ---> 第一个查询为此主题提供平均值 97.3333
机器学习:第一次考试:95,第二次考试:94 ---> 第一个查询为此主题提供平均值 94.5
第二次查询返回名为error404的学生的AVG(97.3333,94.5)= 95.91665的总平均值
表1 - 学生:
pk-INT VARCHAR VARCHAR
+-----------+------------------+-----------------+
| StudentID | StudentFirstName | StudentLastName |
+-----------+------------------+-----------------+
| 1 | agam | rafaeli |
| 2 | amir | aizinger |
| 3 | avi | caspi |
| 4 | avia | wolf |
+-----------+------------------+-----------------+
表2 - 测试:
PK-VARCHR PK-VARCHR PK&FK-INT INT
+------------+------------+-----------+-------+
| TestDate | ClassName | StudentID | Grade |
+------------+------------+-----------+-------+
| 2017-07-01 | Algebra | 1 | 88 |
| 2017-08-02 | Algo | 1 | 97 |
| 2017-09-01 | Algebra | 1 | 80 |
| 2017-09-01 | Algebra | 1 | 97 |
| 2017-09-01 | Set-theory | 1 | 85 |
| 2017-09-04 | Calcules | 1 | 86 |
| 2016-05-03 | Set-theory | 2 | 84 |
| 2016-07-02 | Calcules | 2 | 89 |
| 2016-07-04 | Algo | 2 | 83 |
| 2016-07-05 | Algebra | 2 | 79 |
| 2016-06-03 | Algebra | 3 | 99 |
| 2016-07-02 | Algo | 3 | 97 |
| 2016-07-03 | Calcules | 3 | 96 |
| 2016-09-03 | Set-theory | 3 | 95 |
| 2016-06-03 | Algebra | 4 | 78 |
+------------+------------+-----------+-------+
示例数据:
DROP DATABASE IF EXISTS error404;
CREATE DATABASE error404;
USE error404
CREATE TABLE students
(
StudentID INT NOT NULL AUTO_INCREMENT,
StudentFirstName VARCHAR(25),
StudentLastName VARCHAR(25),
PRIMARY KEY (StudentID)
);
INSERT INTO students (StudentFirstName, StudentLastName)
VALUES ('agam', 'rafaeli'), ('amir', 'aizinger'), ('avi', 'caspi'),
('avia', 'wolf ');
CREATE TABLE tests
(
testid INT NOT NULL AUTO_INCREMENT,
TestDate DATE,
ClassName VARCHAR(25),
StudentID INT NOT NULL,
Grade INT NOT NULL,
PRIMARY KEY (testid),
KEY (StudentID)
);
INSERT INTO tests (TestDate, ClassName, StudentID, Grade)
VALUES ('2017-07-01', 'Algebra', 1, 88),
('2017-08-02', 'Algo', 1, 97),
('2017-09-01', 'Algebra', 1, 80),
('2017-09-01', 'Algebra', 1, 97),
('2017-09-01', 'Set-theory', 1, 85),
('2017-09-04', 'Calculus', 1, 86),
('2016-05-03', 'Set-theory', 2, 84),
('2016-07-02', 'Calculus', 2, 89),
('2016-07-04', 'Algo', 2, 83),
('2016-07-05', 'Algebra', 2, 79),
('2016-06-03', 'Algebra', 3, 99),
('2016-07-02', 'Algo', 3, 97),
('2016-07-03', 'Calculus', 3, 96),
('2016-09-03', 'Set-theory', 3, 95),
('2016-06-03', 'Algebra', 4, 78);
我想要这个结果:
+--------------+--------------+--------------+--------------+--------------+
| StFirstName | StLastName | ClassName | aveInSubject | totalAve |
+--------------+--------------+--------------+--------------+--------------+
| name1 | lname1 | algebra | 80 | 87 |
| name1 | lname1 | algo | 88 | 87 |
| name1 | lname1 | calcul | 93 | 87 |
| name2 | lname2 | algebra | 70 | 74.3 |
| name2 | lname2 | algo | 76 | 74.3 |
| name2 | lname2 | calcul | 77 | 74.3 |
+--------------+--------------+--------------+--------------+--------------+
答案 0 :(得分:1)
您可join
每个科目的平均成绩和每位学生的平均成绩。
SELECT t1.*,t2.totalAvg
FROM (SELECT StudentFirstName,StudentLastName,ClassName,AVG(Grade) AS `average for this subject`
FROM tests
INNER JOIN students ON tests.StudentID=students.StudentID
GROUP BY StudentFirstName,StudentLastName,ClassName
) t1
JOIN (SELECT StudentFirstName,StudentLastName,AVG(`average for this subject`) as totalAvg
FROM (SELECT StudentFirstName,StudentLastName,ClassName,AVG(Grade) AS `average for this subject`
FROM tests
INNER JOIN students ON tests.StudentID=students.StudentID
GROUP BY StudentFirstName,StudentLastName,ClassName
) t
GROUP BY StudentFirstName,StudentLastName
) t2
ON t1.StudentFirstName=t2.StudentFirstName and t1.StudentLastName=t2.StudentLastName
编辑:对于具有窗口函数的MySQL(启动版本8.0)的未来版本,查询可以简化为
select studentfirstname,studentlastname,classname,avgPerSubject
,sum(avgPerSubject) over w/count(*) over w as totalAvg
from (select distinct
s.studentfirstname,s.studentlastname,t.classname,
avg(t.grade) over(partition by s.studentfirstname,s.studentlastname,t.classname) as avgPerSubject
from tests t
join students s on s.studentid=t.studentid
) t
window w as (partition by studentfirstname,studentlastname)