需要有关涉及多种排序的SQL查询的帮助

时间:2016-07-12 19:41:35

标签: sql sorting

我不确定这是否可行,但在思考如何解决这个问题时,我的头开始受伤。我已经阅读了子查询和PARTITION,但我不在我的知识范围内。以下是我的数据示例:

StudentID

可能会有多次测试尝试附加到同一TestIDTestComponentID注意到了尝试。

我需要能够针对每个StudentID的所有尝试查询每StudentID = 14773次的最高测试分数。只有5个组件ID。因此,对于ComponentID,在SELECT DISTINCT sts.StudentStandardizedTestID, sts.StandardizedTestComponentID, sts.StudentID, MAX(sts.score) OVER (PARTITION BY sts.StudentID) HIGHSCORE FROM StandardizedTestScore sts JOIN StudentStandardizedTest sst ON sst.StudentStandardizedTestID = sts.StudentStandardizedTestID AND sst.standardizedtestid = 1 WHERE sst.TranscriptSchoolID = 10 AND sts.StandardizedTestComponentID = 1 OR sts.StandardizedTestComponentID = 4 OR sts.StandardizedTestComponentID = 8 OR sts.StandardizedTestComponentID = 11 OR sts.StandardizedTestComponentID = 13 ORDER BY sts.studentid, sts.StandardizedTestComponentID 的1之间,我只需要得分最高。我需要相同的4,8,11和13.我希望这是有道理的。我突出显示了需要返回的行。任何帮助是极大的赞赏。

以下是我尝试的查询。它只返回与原始行数相同的行数。

Dim ctl As Control

For Each ctl In Forms!ParentForm!ChildForm.Controls
    If ctl.Tag = "BR_SelectFormating" Then
        ctl.FormatConditions.Delete
        ctl.FormatConditions.Add(...,...)
        ....'other formatting details here...
    end if
Next ctl

2 个答案:

答案 0 :(得分:1)

以下是创建表格和数据的代码。

CREATE TABLE StandardizedTestScore (`StudentStandardizedTestID` int(11) ,`studentid` int(11) ,`StandardizedTestComponentID` int(11),`score` int(11)); 
INSERT INTO StandardizedTestScore 
    (`TestID`, `studentid`, `componentid`, `score`)
VALUES
    (14919,3445,1,20),
    (14919,3445,4,17),
    (14919,3445,8,20),
    (14919,3445,11,19),
    (14919,3445,13,19),
    (11339,3448,1,15),
    (11339,3448,4,23),
    (11339,3448,8,23),
    (11339,3448,11,22),
    (11339,3448,13,20),
    (14773,3448,1,20),
    (14773,3448,4,21),
    (14773,3448,8,23),
    (14773,3448,11,21),
    (14773,3448,13,21);

您正在寻找的查询是..

SELECT studentid,StandardizedTestComponentID as componentID,MAX(score) AS score  
FROM StandardizedTestScore 
GROUP BY studentid,StandardizedTestComponentID

结果是这个......

studentid ComponentID Score
3445         1         20
3445         4         17
3445         8         20
3445        11         19
3445        13         19
3448         1         20
3448         4         23
3448         8         23
3448        11         22
3448        13         21

答案 1 :(得分:0)

听起来像你需要聚合而不是排序。类似的东西:

SELECT studentid,testid,componentid,MAX(score) AS score  
FROM yourtable  
GROUP BY studentid,testid,componentid