Functionally dependent columns in SELECT DISTINCT

2024-09-12 Thread Willow Chargin
Hello! Postgres lets us omit columns from a GROUP BY clause if they are functionally dependent on a grouped key, which is a nice quality-of-life feature. I'm wondering if a similar relaxation could be permitted for the SELECT DISTINCT list? I have a query where I want to find the most recent few i

Re: Functionally dependent columns in SELECT DISTINCT

2024-09-13 Thread Willow Chargin
On Thu, Sep 12, 2024 at 11:13 PM wrote: > > What about using DISTINCT ON () ? > SELECT DISTINCT ON (items.id) items.* > FROM items > JOIN parts ON items.id = parts.item_id > WHERE part_id % 3 = 0 > ORDER BY items.id,items.create_time DESC > LIMIT 5; > > This gives me this

Re: Functionally dependent columns in SELECT DISTINCT

2024-09-13 Thread Willow Chargin
Thanks both for your suggestions so far. On Fri, Sep 13, 2024 at 8:43 AM David G. Johnston wrote: > > On Friday, September 13, 2024, Willow Chargin wrote: >> >> In reality I really do want the ID columns of the >> *most recent* items. > > > Use a window function

Re: re-novice coming back to pgsql: porting an SQLite update statement to postgres

2024-09-15 Thread Willow Chargin
On Sun, Sep 15, 2024 at 4:23 AM Alban Hertroys wrote: > > > On 15 Sep 2024, at 11:07, Dan Kortschak wrote: > > > > I have come to hopefully my last stumbling point. > > > > I am unable to see a way to express something like this SQLite syntax > > > > select json_group_array(json_replace(value, >