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)
 join with just the last record

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-08-03 : 14:42:56
I have customers table and then
customertransactions
I want to query all customers and the last custormertransactions.name of the last record entered in cutomertranactions

how do I do this?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-03 : 14:55:47
This code assumes the tables are correlated by [CustomerID] and the transaction table has a [transactionDate] column and that is what determines the "last" transaction:

select c.*
,oa.*
from customers c
outer apply (
select top 1 *
from customerTransactions
where customerid = c.customerid
order by trasactionDate desc
) oa


Be One with the Optimizer
TG

EDIT:
and you should replace the "SELECT *"s with explicit column lists
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-08-03 : 15:11:27
thanks :)
Go to Top of Page
   

- Advertisement -