Re: Beginner Question... - Mailing list pgsql-novice

From Tom Lane
Subject Re: Beginner Question...
Date
Msg-id 7974.1310234028@sss.pgh.pa.us
Whole thread Raw
In response to Beginner Question...  (James David Smith <james.david.smith@gmail.com>)
List pgsql-novice
James David Smith <james.david.smith@gmail.com> writes:
> ... What I would like to do is to
> select the beginning location of each journey. This query gives me the
> date_time of the beginning of the journey:

> SELECT crimes_link, MIN(date_time)
> FROM camdengps3
> GROUP BY crimes_link;

> However I need to add the osgb36_geom column into the query and am unable too.

You could do it with SELECT DISTINCT ON; see the "weather reports"
example in the SELECT reference page in the PG manual.

The more SQL-standard way is to use a subselect, viz

SELECT whatever
FROM camdengps3 upper
WHERE date_time = (SELECT MIN(date_time) FROM camdengps3 lower
                   WHERE lower.crimes_link = upper.crimes_link);

However this is generally a lot slower, and it also outputs
multiple rows if there are multiple rows meeting the MIN date_time
in any particular group, which might not be what you want.

            regards, tom lane

pgsql-novice by date:

Previous
From: Michael Wood
Date:
Subject: Re: Beginner Question...
Next
From: James David Smith
Date:
Subject: Re: Beginner Question...