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 2005 Forums
 Transact-SQL (2005)
 Select most recent

Author  Topic 

mlawton40
Starting Member

15 Posts

Posted - 2007-05-16 : 05:59:58
Hi I have the following table with the following columns:

TransferRequest
TransferRequestId
OrderLineId
Status
SiteRequested
RequestUsername
RequestDateTime

Now I would like to make a select statement that selects all where the OrderLineId = 23 and the RequestDateTime is the most recent.

Any ideas? and I would be very gratefull, been trying to do this for ages now.

Cheers, Mark

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-16 : 06:02:12
[code]
select t.*
from table t
inner join
(
select OrderLineId, TransferRequest = max(TransferRequest)
from table
group by OrderLineId
) r
on t.OrderLineId = r.OrderLineID
and t.TransferRequest = r.TransferRequest
where t.OrderLineId = 23
[/code]


KH

Go to Top of Page
   

- Advertisement -