Re: simple query question: return latest - Mailing list pgsql-general

From Scott Frankel
Subject Re: simple query question: return latest
Date
Msg-id 342BE3B0-3461-11D9-B6EE-000A95A7B782@pacbell.net
Whole thread Raw
In response to Re: simple query question: return latest  (Michael Fuhr <mike@fuhr.org>)
List pgsql-general
ORDER BY DESC LIMIT 1 is much simpler and more readable than a
sub-query.
Though the sub-query approach looks to be a good template for ensuring
more
accurate results by being more explicit.

Thanks to all who responded!
Scott



SELECT * FROM colortable WHERE color = 'red' ORDER BY date DESC LIMIT 1;



SELECT g.color, g.date, g.entered_by
     FROM giventable g
  WHERE g.color     = 'red'
       AND g.date =
                 (SELECT MAX(g2.date)
                       FROM giventable g2
                    WHERE g2.color        = g.color
                 )



pgsql-general by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: simple query question: return latest
Next
From: Christopher Browne
Date:
Subject: Re: When to switch to Postgres 8.0?