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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JOIN ONTO SINGLE RECORD 1

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
GB
Hi All

Is there any way I can have an outer join that only joins onto a single record.

I.e.

I am selecting data from my customer table but also want to include data from their last order.

so

Code:
Select C.Title, C.FirstName, C.LastName, O.OrderDate AS LastOrderDate, O.OrderTotal AS LastOrderTotal
FROM
Customer C
   LEFT OUTER JOIN
Order O
   ON
C.[ID] = O.CustomerID

If this cannot be done, what would be the best approach?

I thought about using UDF's but that just seems far too inefficient as I would have to call one UDF for each field I wanted from the Order table.

TIA

Smeat
 
Code:
SELECT ...
  FROM Customer C
LEFT OUTER 
  JOIN [Order] O
    ON O.CustomerID = C.ID
   AND O.OrderDate =
       ( select max(OrderDate)
           from [Order]
          where CustomerID = C.ID )

r937.com | rudy.ca
 
Have a start for such a great response.

Thanks loads, I just spent over an hour on msdn trying to work out the best approach, your solution took 2 minutes to implement.

Smeat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top