Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / August 2004

Tip: Looking for answers? Try searching our database.

Newbie MySQL question: how to retrieve latest entry from a history of entries?

Thread view: 
davout - 25 Aug 2004 10:50 GMT
I'm using MySQL 4.0.16

How do I join two tables - one a master table, the second a detail table
holding multiple status entries for each master table entry - so that a
query can return the master table fields plus the latest status fields (the
status entry with the most recent date)?

table :  station
Fields: StationID INTEGER,  StationName VARCHAR

e.g.
       1, London
       2 ,Brighton

table:    stationstatus
Fields:    StationID, StatusDate, Metric1,Metric2,Metric3

e.g.

   1,  23/8/04, 1, 1, 1
   1,  24/8/04, 2, 2, 2
   1,  25/8/04, 3, 3, 3
   2,  23/8/04, 4, 4, 4
   2,  24/8/04, 5, 5, 5
   2,  25/8/04, 6, 6, 6

What I'm looking for is a query that returns

   StationID,StationName,StatusDate,Metric1,Metric2,Metric3

e.g.

   1, London, 3, 3, 3,
   2, Brighton, 6, 6, 6

Any ideas?
Flip - 25 Aug 2004 16:34 GMT
What about adding a Timestamp col to the stationstatus table?  Maybe call is
ts, and that way you can order by that column for newest/oldest in the sql.
davout - 25 Aug 2004 18:07 GMT
Can't see how that helps me. The problem is more complicated than you think.
I'm not requesting entries for a given date. Instead its the most recent
date for each station.  My original example has been expanded to show how
this impacts the 'Manchester' data

e.g.
       1,  London
       2,  Brighton
       3,  Manchester

table:    stationstatus
Fields:    StationID, StatusDate, Metric1,Metric2,Metric3

e.g.

   1,  23/8/04, 1, 1, 1
   1,  24/8/04, 2, 2, 2
   1,  25/8/04, 3, 3, 3
   2,  23/8/04, 4, 4, 4
   2,  24/8/04, 5, 5, 5
   2,  25/8/04, 6, 6, 6
   3,  24/8/04, 7, 7, 7

What I'm looking for is a query that returns

   StationID,StationName,StatusDate,Metric1,Metric2,Metric3

e.g.

   1, London, 25/8/04, 3, 3, 3,
   2, Brighton, 25/8/04, 6, 6, 6
   3, Manchester, 24/8/04, 7, 7, 7

Thoughts?

> What about adding a Timestamp col to the stationstatus table?  Maybe call is
> ts, and that way you can order by that column for newest/oldest in the sql.
Flip - 25 Aug 2004 19:08 GMT
> Can't see how that helps me. The problem is more complicated than you think.
Sorry. :<  I often do that. :<  I let my excitement get the better of me.  I
don't get to help out often. :>

> I'm not requesting entries for a given date. Instead its the most recent
> date for each station.  My original example has been expanded to show how
After looking at your example, I believe I see what you're doing now.  I
believe you'll need to do an inner select to narrow down/get the one
specific row you seek, then do the join of that result to the station table.

Depending on the RDBMS you're using, you might be able to limit the rowsize
returned in that inner resultset to just one, otherwise you might be screwed
and need to do it with java in the client.  What I mean is, if you're using
something like Oracle, I believe you limit the resultset to a number (1 in
your case I think), but in MS Access (ok, laughing aside, it illustrates my
point :>), you can't do that, and would get everything coming back.  To help
yourself there, you'd have to order properly in the resultset to make sure
the highest/most recent metric is at the top for that station and in your
client code, just pull off that top metric for each station.

Another way might be to actually put in a row num into the inner select qry
and in the outter query, only take where rownum = 1.

And yet another way to limit the inner query instead of using a rownum to
limit the resultset, I think is to do a max on that date column.  That way,
you're guaranteed to only have one row, it's the biggest (most recent) date,
and then join to station in the outter query.

Hope that helps a bit?
Frank Bates - 26 Aug 2004 04:37 GMT
Consider:
select s.*, ss.*
 from station s
    , stationstatus ss
    , (select StationID, max(StatusDate)
        from stationstatus
        group by StationID) m
 where ss.StationId=s.StationID
   and ss.StationId=m.StationID
   and ss.StatusDate=m.StatusDate
 order by ...

> I'm using MySQL 4.0.16
>
[quoted text clipped - 32 lines]
>
> Any ideas?
davout - 26 Aug 2004 07:53 GMT
Your query isn't accepted by MySQL R4.x

> Consider:
> select s.*, ss.*
[quoted text clipped - 44 lines]
> >
> > Any ideas?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.