Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ofZum-00049W-Cm for pgsql-novice@arkaria.postgresql.org; Tue, 04 Oct 2022 04:52:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ofZul-0004AY-5r for pgsql-novice@arkaria.postgresql.org; Tue, 04 Oct 2022 04:52:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ofZuk-0004AO-RF for pgsql-novice@lists.postgresql.org; Tue, 04 Oct 2022 04:52:18 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ofZui-00024i-MB for pgsql-novice@lists.postgresql.org; Tue, 04 Oct 2022 04:52:18 +0000 Received: by mail-lf1-x132.google.com with SMTP id j16so19493541lfg.1 for ; Mon, 03 Oct 2022 21:52:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=keksi-io.20210112.gappssmtp.com; s=20210112; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date; bh=QPSrIh82nnCLTKuH+sarqFtLyrzYxxNIbPurT1gtWhk=; b=nZuAwH4LVXUN8uM78i9nwn+0JECn0GhLOIN5efo2pr99FaOXnaLd4G685B4lr6ts/L 06HFjM6UCzifAH09pLe0mPqNbfMTr6nTdqbSDSzFQeWPWf9oPIQ8xWSUsWMIlk9XSvYE Bg1PYbc8Dfje3drG+0FpLnVUMgkATQTjcCwffe6H841m7A4TYrkd1J7H/uCY9M6TcngX 35FNy9LK/2yH1V1DBkrrdwM411MBSk5YgYZ6J1PUvBgtgB05xth4lO6iH93i+JNWGGw6 H6tv+P9hgqvOUY1CoI0l48D4E/PfhEH2WlDIIzWs44xlG+Ctb0vJNBOR6jjU8hqa39aK MgAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date; bh=QPSrIh82nnCLTKuH+sarqFtLyrzYxxNIbPurT1gtWhk=; b=gFjqY8gs1E+eUJsFTnV8ehVIWC+/v6pLNS5v7uiXE8flbZy2IqlffWx9jshTr7q9IG pzM4OoSF7be/NqgrLIpKPx5pyZ6L2JwcICP8aWUNj9beDr674Hk9M4cLz0u2ngKFw/D7 EqG+LTmq1M0JSqy3HcynXx7jvpxlRL5V2ct+U8e0HD2PU5dBnmDLKbDro8trAc4pna40 HaWnMbTv2aL9ssjHQudYn4dZssmCI4hb10oggCzRj+uX82FnF2AqNAuLtaUyTtYUWASg /NNXMajhG6dqFGJzqr4AaDm2jwXdplhFmALLy9T7ooaylIfAkNrYeTtBuFyLQEObxD9W ajfg== X-Gm-Message-State: ACrzQf1B4PgbtAA3OVhHBer/jRYuVxI/9NUsdOKW7R8OgBSMdw20C0Jd wdYbgLlF7OoiU5DBpEseA5R2Y94xpvAtX2rJ X-Google-Smtp-Source: AMsMyM4AYoyTNtU4YwlrXKDNCek5h8PFyEBkOgIp1X5e3cNLFJKjTkvWsEajtEX0YciA7RzYvh43KQ== X-Received: by 2002:a05:6512:3ca2:b0:497:9db7:ec10 with SMTP id h34-20020a0565123ca200b004979db7ec10mr9378196lfv.350.1664859135293; Mon, 03 Oct 2022 21:52:15 -0700 (PDT) Received: from smtpclient.apple (150-12-191-90.dyn.estpak.ee. [90.191.12.150]) by smtp.gmail.com with ESMTPSA id bi9-20020a0565120e8900b0048b08e25979sm1735796lfb.199.2022.10.03.21.52.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Oct 2022 21:52:14 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: onni@keksi.io Mime-Version: 1.0 (1.0) Subject: Re: How can I create a feature request for QUALIFY clause into PostgreSQL? Date: Tue, 4 Oct 2022 07:52:03 +0300 Message-Id: <95FC2266-B9FF-4977-BA27-B5C70CC691D3@keksi.io> References: <1701225.1664830660@sss.pgh.pa.us> Cc: Laurenz Albe , pgsql-novice@lists.postgresql.org In-Reply-To: <1701225.1664830660@sss.pgh.pa.us> To: Tom Lane X-Mailer: iPhone Mail (20A362) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks Tom for showing me a different way to think about this problem and ye= s I meant the latest 2 events per person. And anyway I can just use a subque= ry for this too as shown in my earlier emails. The ergonomics of your cross j= oin lateral don't seem that much better the subquery. This still doesn't answer the original question of how to ask for new featur= es in Postgres. Best regards, Onni Hakala > On 3. Oct 2022, at 23:57, Tom Lane wrote: >=20 > =EF=BB=BFOnni Hakala writes: >> Can you show me how you could use that to get the latest 2 events from th= e events table using DISTINCT ON? >=20 > I assume you mean latest 2 events per person, else it's trivially > solved with ORDER BY ... LIMIT 2. But I'd still be inclined to > solve it with ORDER BY ... LIMIT: >=20 > =3D> select e.* from > (select distinct person from events) p > cross join lateral > (select e.* from events e where p.person =3D e.person > order by created_at desc limit 2) e; > person | event_type | created_at =20 > --------------+-------------------+---------------------------- > laurenz.albe | non-helpful reply | 2022-10-03 17:16:39.957743 > someone.else | other reply | 2022-10-03 17:36:39.957743 > someone.else | other reply | 2022-10-03 17:26:39.957743 > onni.hakala | other reply | 2022-10-03 17:31:39.957743 > onni.hakala | clarifying reply | 2022-10-03 17:21:39.957743 > (5 rows) >=20 > In a real application you could probably avoid the SELECT DISTINCT > by joining to some other table that has just one row per person. >=20 > regards, tom lane