Re: SQL query question
| От | Michael Glaesemann |
|---|---|
| Тема | Re: SQL query question |
| Дата | |
| Msg-id | 45F6FB15-3DD1-4E68-96C6-1DF74F02FC8F@seespotcode.net обсуждение исходный текст |
| Ответ на | SQL query question (Kevin Jenkins <gameprogrammer@rakkar.org>) |
| Ответы |
Tips for storing files in the database
|
| Список | pgsql-general |
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
В списке pgsql-general по дате отправления: