Thanks to visit codestin.com
Credit goes to postgrespro.com

Re: Join issue on a maximum value - Mailing list pgsql-sql

From Tom Lane
Subject Re: Join issue on a maximum value
Date
Msg-id [email protected]
Whole thread Raw
In response to Re: Join issue on a maximum value  (Heflin <[email protected]>)
List pgsql-sql
Heflin <[email protected]> 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


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Server Side C programming Environment Set up
Next
From: "Becky Alcorn"
Date:
Subject: Select from two databases