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

From Heflin
Subject Re: Join issue on a maximum value
Date
Msg-id 4086D95A.5000609@tampabay.rr.com
Whole thread Raw
In response to Re: Join issue on a maximum value  (Bruno Wolff III <bruno@wolff.to>)
Responses Re: Join issue on a maximum value  (Bruno Wolff III <bruno@wolff.to>)
Re: Join issue on a maximum value  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
Bruno Wolff III wrote:

>On Wed, Apr 21, 2004 at 14:29:34 -0400,
>  Heflin <hhogan@tampabay.rr.com> wrote:
>  
>
>>So a basic JOIN gets this:
>>
>>SELECT 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';
>>
>>Now the problem: I can't seem to remember how to get only the max value 
>>for the image_id for each auction_id so that the result set would be:
>>    
>>
>
>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 more standard way to do it would be joining auction and image
>with a group by and max to get the highest image_id and then joining
>that result to image again to get the corresponding description.
>
>  
>
Thanks!

I was actually trying to do it the more standard way, but I've been 
bungling up the syntax. I'm going to play with that some more, since it 
might be useful elsewhere.

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?

Thanks again,

-Heflin



pgsql-sql by date:

Previous
From: Kemin Zhou
Date:
Subject: rule's behavior with join interesting
Next
From: Bruno Wolff III
Date:
Subject: Re: Join issue on a maximum value