Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql having max record?

Author  Topic 

petebob796
Starting Member

35 Posts

Posted - 2008-03-20 : 12:45:08
I am trying to increase the speed of some sql statements and have got to a problem that I am sure is quite common. Basically I want to query a student stage history table for the first record based on date and it's stage the layout being:

student_id course_code stage_code dt_achieved

so the record is unique by student_id course_code and stage_code. Stage code being similar to applied, enrolled, withdrawn , completed...(I am simplifying)

Currently to obtain the first stage code of each student I would query grouping by student_id course_code and min(date achieved).

then I would join that on all 3 fields to a copy of the original table to obtain the appropriate stage code.

Can this not be done all in one query somehow. I was thinking return records HAVING min date kind of thing but don't know how to do it.

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2008-03-20 : 13:21:12
Post your table schema, sample data and the query that you are currently using. I or someone should be able to help pretty quickly.

Terry
Go to Top of Page
   

- Advertisement -