Re: simple query question: return latest

From: Scott Frankel <leknarf(at)pacbell(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: simple query question: return latest
Date: 2004-11-12 04:13:27
Message-ID: 342BE3B0-3461-11D9-B6EE-000A95A7B782@pacbell.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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
)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Browne 2004-11-12 04:13:59 Re: When to switch to Postgres 8.0?
Previous Message Bruce Momjian 2004-11-12 04:01:56 Re: GUC custom variables broken