Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-14 Thread David Rowley
On Tue, 14 Mar 2023 at 16:07, Ben Clements wrote: > Similar to your "TOP() and BOTTOM() aggregate" idea, you might find Erwin > Brandstetter's solution using the LAST() aggregate function interesting: > (https://dba.stackexchange.com/a/324646/100880) Interesting. Just note that ORDER BY aggreg

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-14 Thread David Rowley
On Tue, 14 Mar 2023 at 21:01, Alban Hertroys wrote: > > On 7 Mar 2023, at 4:11, David G. Johnston > > wrote: > > TOP(city, ROW(population, land_area)) ? > > What should be the expected behaviour on a tie though? Undefined. Same as having an ORDER BY on a column that's not unique. The sort impl

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-14 Thread Alban Hertroys
> On 7 Mar 2023, at 4:11, David G. Johnston wrote: > > On Mon, Mar 6, 2023 at 7:51 PM David Rowley wrote: > On Tue, 7 Mar 2023 at 12:40, Tom Lane wrote: > > > > Ben Clements writes: > > > As shown above, the following calculated column can bring in the city > > > name, > > > even though the

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-13 Thread Ben Clements
Thanks David. Similar to your "TOP() and BOTTOM() aggregate" idea, you might find Erwin Brandstetter's solution using the LAST() aggregate function interesting: ( https://dba.stackexchange.com/a/324646/100880) If the FIRST_LAST_AGG extension is installed, then we can do something like this: SELE

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-06 Thread David Rowley
On Tue, 7 Mar 2023 at 16:11, David G. Johnston wrote: > > On Mon, Mar 6, 2023 at 7:51 PM David Rowley wrote: >> the transfn for bottom() would need to remember the city and the >> population for the highest yet seen value of the 2nd arg. > > > BOTTOM() remembers the highest value? I was thinking

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-06 Thread David G. Johnston
On Mon, Mar 6, 2023 at 7:51 PM David Rowley wrote: > On Tue, 7 Mar 2023 at 12:40, Tom Lane wrote: > > > > Ben Clements writes: > > > As shown above, the following calculated column can bring in the city > name, > > > even though the city name isn't in the GROUP BY: > > >max(city) keep (dens

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-06 Thread David Rowley
On Tue, 7 Mar 2023 at 12:40, Tom Lane wrote: > > Ben Clements writes: > > As shown above, the following calculated column can bring in the city name, > > even though the city name isn't in the GROUP BY: > >max(city) keep (dense_rank first order by population desc) > > You haven't really expla

Re: Idea: PostgreSQL equivalent to Oracle's KEEP clause

2023-03-06 Thread Tom Lane
Ben Clements writes: > As shown above, the following calculated column can bring in the city name, > even though the city name isn't in the GROUP BY: >max(city) keep (dense_rank first order by population desc) You haven't really explained what this does, let alone why it can't be implemented