从基于其他列的已排序列中选择并显示MS Access查询结果

时间:2018-04-02 12:06:37

标签: ms-access

我有一个StudentID列,一个关于在特定日期传递030级别的列,以及一个关于在特定日期传递040级别的列。

无法在与ID相同的行上获得两个结果,因为这些是指在不同时间拍摄的不同课程(有些学生在不同的学期完成了030或040,因此有N / Y和Y / N结果一些身份证。

期望的结果:StudentID具有唯一值,030和040是Y或N.所有学生ID都在一行上。

以下是我的查询设置的屏幕截图。

enter image description here

以下是当前输出的屏幕截图。如果ID号有两个Y,我希望它们显示在同一行。

enter image description here

1 个答案:

答案 0 :(得分:0)

来自您的“查询设置”'截图,我无法在LEFT JOIN表中的StudentID字段中看到您使用152_Entrants的外键,但假设此外键也被调用{ {1}},然后我会建议以下(未经测试的)代码:

StudentID

此处,每个子查询都返回SELECT [152_Entrants].StudentID, Nz(P030.PassOrFail,"N") AS 030pass, Nz(P040.PassOrFail,"N") AS 040pass FROM ( 152_Entrants LEFT JOIN ( SELECT DISTINCT StudentID, PassOrFail FROM FinalMark WHERE SemesterNumber <= 162 AND CourseType = "040" AND PassOrFail = "P" ) AS P040 ON [152_Entrants].StudentID = P040.StudentID ) LEFT JOIN ( SELECT DISTINCT StudentID, PassOrFail FROM FinalMark WHERE SemesterNumber <= 162 AND CourseType = "030" AND PassOrFail = "P" ) AS P030 ON [152_Entrants].StudentID = P030.StudentID; StudentID个唯一值,PassOrFail"P",每个SemesterNumber <= 162 CourseType的记录为152_Extrants

然后LEFT JOIN您的表格StudentID添加到每个子查询中,以便在子查询中找到匹配的Nz时,学生已通过(根据您的标准),否则学生失败,因此"N"函数将返回152_Entrants

修改

在查看SELECT E152.StudentID, Nz(P030.PassOrFail,"N") AS 030pass, Nz(P040.PassOrFail,"N") AS 040pass FROM ( ( SELECT StudentID FROM FinalMark WHERE SemesterNumber = 152 GROUP BY StudentID ) AS E152 LEFT JOIN ( SELECT DISTINCT StudentID, PassOrFail FROM FinalMark WHERE SemesterNumber <= 162 AND CourseType = "040" AND PassOrFail = "P" ) AS P040 ON E152.StudentID = P040.StudentID ) LEFT JOIN ( SELECT DISTINCT StudentID, PassOrFail FROM FinalMark WHERE SemesterNumber <= 162 AND CourseType = "030" AND PassOrFail = "P" ) AS P030 ON E152.StudentID = P030.StudentID; 查询的构成时,完整查询可能会变为:

152_Entrants

无需单独的{{1}}查询。