Re: Join issue on a maximum value

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Heflin <hhogan(at)tampabay(dot)rr(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Join issue on a maximum value
Date: 2004-04-22 01:51:45
Message-ID: 20040422015145.GB2160@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Apr 21, 2004 at 16:28:10 -0400,
Heflin <hhogan(at)tampabay(dot)rr(dot)com> wrote:
> >
> >The postgres specific way of doing this is:
> >SELECT DISTINCT ON (auction.auction_id)
> > auction.auction_id, image.image_id, image.image_descr
> > FROM auction JOIN image ON auction.auction_id = image.auction_id
> > WHERE auction.auction_owner = 'Mabel'
> > ORDER BY auction.auction_id, image.image_id DESC
> >;
>
> The thing that disturbs me about your syntax is that I don't really see
> an assurance that I'll get the correct image_id. Any chance you can
> tell me why this works?

The postgres semantic is that when dinstinct on is combined with order by
the first distinct row in the order defined by the order by is the one
returned. This is described in the documentation and there is an example
of a query taking advantage of this.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2004-04-22 03:30:58 Re: Join issue on a maximum value
Previous Message Blake 2004-04-21 23:41:57 Proper SQL syntax requested