Solved

T-SQL Query for Top 1 for each date

Posted on 2012-03-13
2
507 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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