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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Selecting a record from each group

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-08-27 : 11:10:09
Roy writes "I have a table that contains a truck number, source location, destination location, dateout and trip duration. I need to determine the last trip made by each truck before a certain date. I only want 1 record returned for each truck. I have been able to achieve this using temp tables but it's seem messy and slow. There must be a better way."

nr
SQLTeam MVY

12543 Posts

Posted - 2002-08-27 : 11:27:06
select *
from tbl
where dateout = (select max(dateout) from tbl t2 where tbl.truckid = t2.truckid where dateout < @date)

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -