Re: Join issue on a maximum value

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Heflin <hhogan(at)tampabay(dot)rr(dot)com>
Cc: Bruno Wolff III <bruno(at)wolff(dot)to>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Join issue on a maximum value
Date: 2004-04-22 03:30:58
Message-ID: 18805.1082604658@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Heflin <hhogan(at)tampabay(dot)rr(dot)com> writes:
> Bruno Wolff III 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 ORDER BY DESC is what forces the max image_id to be selected. Read
the discussion of SELECT DISTINCT ON in the SELECT reference page; the
"weather report" example may be illuminating.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 2004-04-22 05:18:12 Re: Server Side C programming Environment Set up
Previous Message Bruno Wolff III 2004-04-22 01:51:45 Re: Join issue on a maximum value