0

I have a table, which can be seen as a evaluation of two courses in several classroom tests, like this:

student_ID     Evaluation           Course1   Course2
------------------------------------------------------
1                   5                 88         93
2                   4                 70         87
1                   5                 93         90
2                   5                 99         91
3                   3                 65         60
3                   4                 88         70

I need to get the result of the Evaluation=5 for each student, if any. If that student has more than one Evaluation=5, the query only show any one of them. So for the above example table, the query result will be

student_ID     Evaluation           Course1   Course2
------------------------------------------------------
1                   5                 88         93
2                   5                 99         91

Of course in my real table, the "Courses" number is more than 2.

Thanks for the help.

1 Answer 1

2

Since you only want to get only one record for every student_id, you can use ROW_NUMBER() which generates sequential number. The number generated will always starts with 1 which you can use to filter out row for every partition, in this case Student_ID.

SELECT  Student_ID, Evaluation, Course1, Course2
FROM    
        (
            SELECT  Student_ID, Evaluation, Course1, Course2,
                    ROW_NUMBER() OVER (PARTITION BY Student_ID 
                    ORDER BY Student_ID) rn
            FROM    TableName
            WHERE   Evaluation = 5
        ) a
WHERE   a.rn = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god! You are so quick. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.