or variant
Main Topics
Browse All TopicsI need to fetch data from two tables(Vehicles, VData) in my database using the following criterion.
Vehicles table has fields (UnitID, vNo, vName) UnitID is Primary key and is unique for each record.
VData table has fields (UnitID, TimeDate, value1, value2) There are multiple records with same UnitID in this table.
Now I want to select the following
Each Unique UnitID in VData that is also present in Vehicles table and also its last record(Latest TimeDate value) from VData.
Part I of this query is easy i.e "Each Unique UnitID in VData that is also present in Vehicles table"
This can be simply done by using the following query
Select Vehicles.UnitID, Vehicles.vNo, Vehicles.vName from Vehicles where Vehicles.UnitID IN ( Select Distinct UnitID from VData)
But I am stuck as how to fetch the latest record for each UnitID in VData table.
Backend database Oracle 9i
Frontend VB6 and ADO
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can try the below :
select A.*, B.timedate, B.value1 , B.value2
from
(select unitid, vno , vname from vehicles ) A,
( select temp1.unitid unitid,
temp1.timedate timedate,
temp1.value1 value1, temp1.value2 value2
from
( select vd.unitid vd_ui, max(vd.timedate) vd_td
from vdata vd
group by vd.unitid ) x , vdata temp1
where temp1.unitid = x.vd_ui
and temp1.timedate = x.vd_td
) B
where a.unitid = b.unitid;
nav_kum_v,
In fact the query you have supplied doesnot work perfectly.... When there is a case where a UnitID has more then one record for the maximum timeDate then it returns multiple records. What I mean is explained in the Code view below....
awking00,
Your query performs perfectly but it's only drawback is that it is slow as compared to the one supplied by nav_kum_v.
this is one way of doing it.... not sure about the performance.. give it a try :
select unitid, vno, vname, timedate, value1, value2
from (
select A.*, B.timedate, B.value1 , B.value2 , row_number() over ( partition by a.unitid order by timedate desc ) xx
from
(select unitid, vno , vname from vehicles ) A,
( select temp1.unitid unitid,
temp1.timedate timedate,
temp1.value1 value1, temp1.value2 value2
from
( select vd.unitid vd_ui, max(vd.timedate) vd_td
from vdata vd
group by vd.unitid ) x , vdata temp1
where temp1.unitid = x.vd_ui
and temp1.timedate = x.vd_td
) B
where a.unitid = b.unitid
)
where xx = 1 ;
Here is the another version....again not sure about the performance.. give this as well a try :
select a.*,
( select value1 from vdata xx where xx.unitid=a.unitid and xx.timedate = a.timedate and rownum = 1 ) value1,
( select value2 from vdata xx where xx.unitid=a.unitid and xx.timedate = a.timedate and rownum = 1 ) value2
from (
select v.* , ( select max(timedate) from vdata vd where vd.unitid = v.unitid ) timedate
from vehicles v
where exists ( select 1 from vdata vv where vv.unitid = v.unitid )
) a ;
Business Accounts
Answer for Membership
by: ivostoykovPosted on 2009-04-22 at 23:41:26ID: 24212261
try following
Select allOpen in new window