I have 2 tables
Delivery
--------
deliveryid int (PK)
description long varchar
DeliveryHistory
---------------
historyid int
delievryid int
statusid int
recordtime timestamp
WHat I am trying to do is a left outer join to bring back all records from table Delivery with only the most recent entry in DeliveryHistory for each delivery. However if there are no entries in the DeliveryHistory for the delivery I would like a null value
I have done this:
select d.deliveryid,d.description, h.statusid from delivery d
left outer join Deliveryhistory h on d.deliveryid = h.deliveryid
where h.recordtime =
( SELECT MAX(recordtime)
FROM Deliveryhistory
WHERE deliveryid = d.deliveryid)
But it only returns the rows that have an entry in DeliveryHistory.