Search Postgresql Archives

Re: SQL query question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Jun 18, 2006, at 8:50 , Kevin Jenkins wrote:

I have a beginner question. I have a table with a bunch of filenames and each of them have a date. Multiple files may have the same name. For example

filename date     revision
file1    10/05/06 1
file1    10/05/07 2
file2    10/05/08 1

I want to do a query that will return the greatest date for each unique filename

I can think of two ways to do this (and there are probably more): one using standard SQL and one using PostgreSQL extensions. Here's the standard SQL way:

SELECT filename, date, revision
FROM table_with_bunch_of_filenames
NATURAL JOIN (
    SELECT filename, max(date) as date
    FROM table_with_bunch_of_filenames
    GROUP BY filename
	) AS most_recent_dates;


If you don't need the revision, you can just use the subquery-- the stuff in the
parentheses after NATURAL JOIN.

And here's the way using DISTINCT ON, which is a PostgreSQL extension.

SELECT DISTINCT ON (filename, date)
    filename, date, revision
FROM table_with_bunch_of_filenames
ORDER BY filename, date desc;

Hope this helps.

Michael Glaesemann
grzm seespotcode net





[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux