我想知道是否可以将两个查询合并为一个? 我有两个SQL查询。第一个输出第一年级学生的平均最终成绩。第二个输出为三年级学生的平均最终成绩。
我尝试使用UNION,但是不幸的是,它只是将所有内容都移到了一列。我正在通过MS Access使用SQL。
第一个查询:
SELECT Marks.Student_ID AS Year, SUM(Final_Mark)/COUNT(Marks.Student_ID) AS Year_1_Marks
FROM Marks, Students
WHERE Marks.Year_Taken = 1 AND Students.Student_ID IN
(SELECT Marks.Student_ID
FROM Marks, Students
WHERE Students.Student_ID = Marks.Student_ID)
GROUP BY Marks.Student_ID
第二次查询:
SELECT Marks.Student_ID AS Year, SUM(Final_Mark)/COUNT(Marks.Student_ID) AS Year_3_Marks
FROM Marks, Students
WHERE Marks.Year_Taken = 3 AND Students.Student_ID IN
(SELECT Marks.Student_ID
FROM Marks, Students
WHERE Students.Student_ID = Marks.Student_ID)
GROUP BY Marks.Student_ID
我希望将输出分成3列,分别是Student_ID,Year_1_Marks,Year_3_Marks。我们非常感谢您的帮助。
答案 0 :(得分:0)
您应该使用联接。我将对您的代码进行一些更改以实现可维护性和可能的性能,但仅是为了给您一个想法,这应该可行。
Select A.Student_ID, A.Year_1_Marks, B.Year_3_Marks
From (
SELECT Marks.Student_ID AS Year, SUM(Final_Mark)/COUNT(Marks.Student_ID) AS Year_1_Marks
FROM Marks, Students
WHERE Marks.Year_Taken = 1 AND Students.Student_ID IN
(SELECT Marks.Student_ID
FROM Marks, Students
WHERE Students.Student_ID = Marks.Student_ID)
GROUP BY Marks.Student_ID
) A
Join (
SELECT Marks.Student_ID AS Year, SUM(Final_Mark)/COUNT(Marks.Student_ID) AS Year_3_Marks
FROM Marks, Students
WHERE Marks.Year_Taken = 3 AND Students.Student_ID IN
(SELECT Marks.Student_ID
FROM Marks, Students
WHERE Students.Student_ID = Marks.Student_ID)
GROUP BY Marks.Student_ID
) B On B.Student_Id = A.Student_Id
答案 1 :(得分:0)
首先,请先对格式进行更正,以使其更具可读性。
然后您可以尝试一下,我认为它应该可以正常工作:
SELECT Students.Student_ID as Student_ID
, SUM(marks1.Final_Mark)/COUNT(marks1.Student_ID) AS Year_1_Marks
, SUM(marks3.Final_Mark)/COUNT(marks3.Student_ID) AS Year_3_Marks
FROM Students LEFT JOIN Marks marks1
ON marks1.Student_ID = Students.Student_ID
AND marks1.Year_Taken = 1 LEFT JOIN Marks marks3
ON marks3.Student_ID = Students.Student_ID
AND marks3.Year_Taken = 3
这将显示所有学生的成绩,因此,如果您的学生没有成绩,我会添加一个WHERE子句以将其排除在外,以避免出现任何此类问题。
答案 2 :(得分:0)
@KevinKevin
您的加入不是好习惯。当心“添加零错误”。
以下是“普通” SQL中的示例:
SELECT
Marks.Student_ID AS Year,
(
case when COUNT(case when Marks.Year_Taken = 1 then Marks.Student_ID else 0 end) <> 0
then SUM(
case when Marks.Year_Taken = 1 then Final_Mark else 0 end
) / COUNT(
case when Marks.Year_Taken = 1 then Marks.Student_ID else 0 end
) else 0 end
) AS Year_1_Marks,
(
case when COUNT(case when Marks.Year_Taken = 3 then Marks.Student_ID else 0 end) <> 0
then SUM(
case when Marks.Year_Taken = 3 then Final_Mark else 0 end
) / COUNT(
case when Marks.Year_Taken = 3 then Marks.Student_ID else 0 end
) else 0 end
) AS Year_3_Marks
FROM
Marks
INNER JOIN Students ON Students.Student_ID = Marks.Student_ID
GROUP BY
Marks.Student_ID
以下是“ Access” SQL中的示例:
SELECT
Marks.Student_ID AS Year,
Iif ( COUNT(Iif( Marks.Year_Taken = 1 ,Marks.Student_ID, 0))=0,0,
SUM(Iif ( Marks.Year_Taken = 1,Final_Mark, 0) ) / COUNT(Iif( Marks.Year_Taken = 1 , Marks.Student_ID, 0))
) AS Year_1_Marks,
Iif ( COUNT(Iif( Marks.Year_Taken = 3 , Marks.Student_ID, 0))=0,0,
SUM(Iif ( Marks.Year_Taken = 3 Final_Mark, 0) ) / COUNT(Iif( Marks.Year_Taken = 3 , Marks.Student_ID, 0))
) AS Year_3_Marks,
FROM
Marks
INNER JOIN Students ON Students.Student_ID = Marks.Student_ID
GROUP BY
Marks.Student_ID