joining 2 tables & selecting the most recent record via date field [message #86364] |
Fri, 24 September 2004 10:13 ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
Mark
Messages: 284 Registered: July 1998
|
Senior Member |
|
|
Hi,
I have 2 tables that I'm joining. In 1 table, I have a list of all employees. In the other, i have a list of cars associated with employees and non-employees (empnum in both tables). In the list of cars, an employee may have more than one car (record). I want to select the most recent record by date. I have been able to extract what I want but it's hard coded. I Oracle enough to be dangerous so any help would be appreciated.
Here is my statement:
select a.* from lease a , employee e where a.empno = e.empno and a.empno='081718'
and a.delivery_date = (select max(delivery_date) from lease where empno='081718')
This returns only the most recent record. How can I make it generic for all records???
Thanks in advance!
Mark
|
|
|
|
Re: joining 2 tables & selecting the most recent record via date field [message #86386 is a reply to message #86373] |
Mon, 27 September 2004 06:02 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
Mark
Messages: 284 Registered: July 1998
|
Senior Member |
|
|
Hi Vamsi,
I tried the SQL and made one change:
select a.* from lease a , employee e where a.empno = e.empno and a.delivery_date = (select max(delivery_date) from lease a, employee e where e.empno=a.empno)
This returns a single record of the maximum delivery_date of the lease table. I'm closer, but not
quite there yet. I'll keep trying and let you know what I come up with. Thanks for your help in the meantime!
Mark
|
|
|
|