Hello again, Gary,
I don't know if this query works OK, i havent tried it.
But, If I understood correctly this can be one way to do what you want.
Could you please tell me if it worked and if it didn't why, so we can tweak
it.
Best,
Oliver
SELECT subq.s_stock_no,subq.s_regno,subq.s_vin,subq.s_created,subq.m,
sec.s_creacted
FROM
(select s_stock_no, s_regno, s_vin, s_created, MAX(ud_id) as m
from stock s
left outer join used_diary u
on s.s_regno = u.ud_pex_registration
where s_stock_no = 'UL15470';
GROUP s_stock_no,s_regno,s_vin,s_created
) subq
JOIN
used_diary sec
ON subq.m = sec.ud_id
Sent: Wednesday, May 23, 2012 10:47 AM
Subject: Re: [SQL] left outer join only select newest record
> Appologies for not making it clearer. stock_details is simply a view of
> table
> stock, pulling in some lookup values. used_diary is the name of the table
> containing the tax requests. It's called the used_diary because it was the
> diary for taxing used vehicles.
>
> Here is a select to show the problem. There is one stock record and two
> tax
> records. What I'm looking for is how I can return only the second tax
> record,
> the one with the highest ud_id
>
> select s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from
> stock s left outer join used_diary u on s.s_regno = u.ud_pex_registration
> where s_stock_no = 'UL15470';
>
> s_stock_no | s_regno | s_vin | s_created |
> ud_id | ud_handover_date
> ------------+---------+-------------------+----------------------------+-------+------------------
> UL15470 | YG12*** | KNADN312LC6****** | 2012-05-21 09:15:31.569471 |
> 41892 | 2012-04-06
> UL15470 | YG12*** | KNADN312LC6****** | 2012-05-21 09:15:31.569471 |
> 42363 | 2012-05-16
> (2 rows)
>
>