Solved

T-SQL Query for Top 1 for each date

Posted on 2012-03-13
2
488 Views
Last Modified: 2012-08-13
Hello Experts,

I have a table called [Exam_Results], and it has to following colums:
[ID] int
[UserID] int
[ExamID] int
[Score] float
[Date] datetime

Users can take exams multiple times and the data looks as follows:

ID     UserID     ExamID     Score      Date
----------------------------------------------------------------------------
1          20            1               75          2012-02-15 13:30:00
2          20            1               83          2012-02-17 10:30:00
3          20            2               80          2012-02-15 13:30:00
4          54            3               64          2012-02-16 13:30:00
5          54            3               78          2012-02-20 14:30:00

What I am trying to get is the first time each user took the exam, so I want my select to return the top 1 result for each user.

So with the data above, my query would return:
UserID     ExamID     Score
-------------------------------------------
20                1              75
20                2              80
54                3              64

So, I need help to write a select query that for each User and Exam ID, will return the score from the first time the exam was completed by each user.
0
Comment
Question by:NursingCorp
2 Comments
 
LVL 8

Accepted Solution

by:
fundacionrts earned 500 total points
ID: 37717576
SELECT ER1.UserID, ER1.ExamID, ER1.Score
FROM Exam_Results AS ER1
WHERE ER1.Date <= (
SELECT MIN( ER2.Date )
FROM Exam_Results AS ER2
WHERE ER2.UserID = ER1.UserID
AND ER2.ExamID = ER1.ExamID )
0
 

Author Closing Comment

by:NursingCorp
ID: 37717665
Worked Perfectly - Thanks for the clear, quick solution!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 27
sql query 7 34
Access 2003 form, automatically input query parameter from list 5 31
SQL Select Query help 3 31
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now