我正在尝试做这样的事情,我不确定是否可以使用简单的 GROUP BY:
SELECT GD.GradebookDetailId, G.SubjectCode, G.Description, G.UnitsAcademic, G.UnitsNonAcademic,
GD.Grade, GD.Remarks, G.FacultyName, STR_TO_DATE(G.DateApproved, '%m/%d/%Y %h:%i:%s') AS 'DateAproved'
FROM gradebookdetail GD
INNER JOIN gradebook G ON GD.GradebookId=G.GradebookId
WHERE G.DateApproved IS NOT NULL AND G.GradebookType='final' AND StudentIdNumber='2012-12345'
GROUP BY ???????
ORDER BY G.SubjectCode ASC
所以当我有一张这样的桌子时:
学生:2012-12345
SubjectCode | Grade | DateApproved
SUBJ123 | 2.00 | 1/4/2012
SUBJ123 | 1.75 | 1/5/2012
SUBJ987 | 1.50 | 1/5/2012
它应该如下所示:
学生:2012-12345
SubjectCode | Grade | DateApproved
SUBJ123 | 1.75 | 1/5/2012
SUBJ987 | 1.50 | 1/5/2012
编辑:这是解决方案:
SELECT
GD.GradebookDetailId,
G.SubjectCode,
G.Description,
G.UnitsAcademic,
G.UnitsNonAcademic,
GD.Grade,
GD.Remarks,
G.FacultyName,
STR_TO_DATE(G.DateApproved, '%m/%d/%Y %h:%i:%s') AS 'DateAproved'
FROM
gradebookdetail GD INNER JOIN
gradebook G ON GD.GradebookId=G.GradebookId
WHERE
G.DateApproved IS NOT NULL AND
G.GradebookType='final' AND
StudentIdNumber='2011-10172' AND
NOT EXISTS (
SELECT 1 FROM gradebook G2 INNER JOIN gradebookdetail GD2 ON G2.GradebookId=GD2.GradebookId
WHERE
G2.GradebookType = 'final' AND
G2.DateApproved IS NOT NULL AND
GD2.StudentIdNumber = GD.StudentIdNumber AND
G2.SubjectCode = G.SubjectCode AND
G2.DateApproved > G.DateApproved
)
ORDER BY G.SubjectCode ASC