Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select only the important records

Status
Not open for further replies.
Jul 28, 2011
167
NG
Hi all,

I have a database (of overtime applications) that looks like this
Code:
apply_date  |   start_time     |   end_time       |   overtime_rsn                            |   1st lvl Sup
2012-11-14  |   13:00:00.000   |   18:00:00.000   |   Very good reason                        |   Rejected
2012-11-15  |   16:00:00.000   |   21:00:00.000   |   Why do you ask                          |   Not yet Approved
2012-11-20  |   09:00:00.000   |   14:00:00.000   |   Good for me                             |   Approved
2012-11-20  |   09:00:00.000   |   14:00:00.000   |   Good for me                             |   Approved
2012-11-20  |   09:00:00.000   |   15:00:00.000   |   So, so Good for me                      |   Approved
2012-11-21  |   11:00:00.000   |   17:00:00.000   |   A reason you must not underestimate     |   Approved

Notice the date 2012-11-20 is showing 3 times. This is as a result of the fact that the applicant updated the overtime twice.

What I want is a select statement that will show me a result like this

Code:
apply_date  |   start_time     |   end_time       |   overtime_rsn                            |   1st lvl Sup
2012-11-14  |   13:00:00.000   |   18:00:00.000   |   Very good reason                        |   Rejected
2012-11-15  |   16:00:00.000   |   21:00:00.000   |   Why do you ask                          |   Not yet Approved
2012-11-20  |   09:00:00.000   |   15:00:00.000   |   So, so Good for me                      |   Approved
2012-11-21  |   11:00:00.000   |   17:00:00.000   |   A reason you must not underestimate     |   Approved

Notice I now have only the last record of the date 2012-11-20 rather than the three dates. How can I call this kind of query.

I know it is something simple but I cant just figure it out right away.
Thanks in advance.
 
imex, why would you take the 1st row?

he said he wanted "only the last record of the date" and the example shows that

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Well, thanks guys,

I think imex's solution works fine. I think you can truely select the first row since ROW_NUMBER() OVER(PARTITION BY apply_date ORDER BY end_time DESC) as RowNum will give each distinct apply date a row number.

I however ORDER BY timestamp since I have a timestamp column (and it worked better than ORDER BY end_time)

Thanks again Imex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top