Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2005
    Posts
    74

    Unanswered: Obtaining the last record

    Let's say I need to be able to dynamically retrieve the last record in a table (ChData) in order to be able to continuously monitor the flow of data. Since theoretically there is no such thing defined in SQL, let's say I have an index column - ChTimestamp - (which, for some odd reason, is a DOUBLE), which grows monotonically. Thus, I want the row with the largest ChTimestamp value. The data columns would be Ch0, Ch1, and so on. Let's also assume that for some reason I am limited to using the LabVIEW database connectivity toolkit and because of that, I don't have full use of the SELECT statement but I have to supply the table and column names for the "SELECT[*] FROM[*]" and only the WHERE and the like predicates can be freely specified (I don't know how freely). What would be an elegant query to achieve the desired result? Thanks!
    Kamen

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    elegant query to achieve your result:
    Code:
    select phonenumber
      from dbms_vendors
     where dbms in ('sql server','oracle','mysql','postgresql','sybase','db2')
    sorry, couldn't help it

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2007
    Posts
    62
    select * from chdata
    where
    chtimestamp=(select max(chtimestamp) from chdata)

    or am I missing something?

    Alternatively you could write a trigger to update a known row in another version of the table with a single row so you can have a simpler where clause.

    select * from mystupidtable where x='1'

  4. #4
    Join Date
    Apr 2007
    Posts
    183

    Talking Homework?

    SELECT TOP 1 * FROM Table1 ORDER BY chTimestamp DESC

  5. #5
    Join Date
    Dec 2005
    Posts
    74
    Thank you very much, LoztInSpace and Peso. No it's not a homework, I'm a professional, it's just that I have to deal with so many different technologies, programming languages, APIs, etc., that sometimes it's just easier (not to mention quicker) to ask those who deal with it every day instead of trying to jump start my fried brain.

    Kamen

  6. #6
    Join Date
    Apr 2007
    Posts
    183
    Maybe it is time to make your niche?

  7. #7
    Join Date
    Dec 2005
    Posts
    74
    Actually, small companies appreciate multivalent engineers like me. Plus, I'm too old to change.
    Kamen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •