Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7BSr-00000000Aq4-09GW for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Sep 2026 12:43:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x7BSq-00000004Hx6-0sYG for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Sep 2026 12:43:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7BSp-00000004Hwq-3UZF for pgsql-hackers@lists.postgresql.org; Thu, 17 Sep 2026 12:43:43 +0000 Received: from relay6-d.mail.gandi.net ([217.70.183.198]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7BSn-000000008ch-1Sn6 for pgsql-hackers@postgresql.org; Thu, 17 Sep 2026 12:43:42 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 53C883ED08; Thu, 17 Sep 2026 12:43:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=postgresfriends.org; s=gm1; t=1789649018; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5e0ehVe3vG0dcWKZinG15QhgjIFEfALILmkqAVY6Yb0=; b=hVpgqai8cu2EEcjyN8DAhHT0nYznP0gw26+Gxqgupm+KRLjPBKOHKJGobP28jDChmsnXEy gV18Wt/OoRfRqVpruw5BJ0hfxASsoS1pNe4VgHLozWGWBbkOl9RAR5GgOZkITaIR/TLfyo CePvJdj84cm3nJtmpB/tUkITxUNWia2FLpUwU9oar67UDQXr8AC9/jcfiEuMZpxVZXfOZ/ 5Xq9EIFqzlUuGUIUDiHLM5qNXjsyLGPGCXL0sQDaA2XbmMksZ3rGX1jCgYx2hn606YZbm2 42RPhTloNc+JB7EJY48WioChzy7A8LVhwcc8XebiPSbzvuSeJZOsqVoajxSsrQ== Message-ID: Date: Thu, 17 Sep 2026 14:43:36 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: ANSI SQL proposal: SELECT DISTINCT ON (... ORDER BY ...) and UNION DISTINCT ON (... ORDER BY ...) To: Hannu Krosing Cc: pgsql-hackers , Peter Eisentraut , Dilip Kumar References: Content-Language: en-US From: Vik Fearing In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: vik@postgresfriends.org X-GND-State: clean X-GND-Score: -100 X-GND-Cause: dmFkZTF5G8PzSlRgCMEyY6z7/oscQ1GIIyUgHH41R2YzFSHKgEw3S87EVn/QVJHUutyzazS3zMXiBO6s96henaXY32sDiDt9yTvvs+aNc6Q0SRfWxVjpZrBvE2yyhffgMTOKK1G/MTorcRVJNwspf+buFTvElFqKVNRLcoAnXQFafwu0jWpXQUJyWqXHuswDw2tGsJeR/T9DtgGc1KluTH55IQt/PoghEuz/50bHd9TiZ6lMuBB5Hu3ODvHuJgnzb8pBL2KbFtq6N4hNdoFtyqUDSa28F09WpM3yu6ofx3+Jgd+/Mjf1PPpKjoXfO7Xt+rpnOeMR7jiOUBTDAsiOZigVVtH7nADGruAQxN5YHtIMrr8E46NZOShWaHHITmJmmfALlVA6bjbMkvp742K0aqr3EOUhnEr40z7Be6wtHEvGtDYjqzSUdBGS9M7BEsqcHswrVLbHZvnNc+SZN7cPPSluEp68q72IG/QFsze5npK8hj/eiC3TSummyUOp/bZkCdbU/criOyV/JEgqsZyAatc1TdAxHa5P0L/SJD4jWkhRv8J8SMeaHn86HDmzOYe6mHXS8bF8DdUTWmk5tkWahFQaTIYUvUNPNffCoaDEWYNjhmyxIToXirEJd4kmWA9GKWZDVo6UU/ZYC0R5ZbD+r7fbtHemPhwXe36L5xX0rf9CABlrig List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 01/08/2026 15:08, Hannu Krosing wrote: > Hi Vik > > Finally had time to put this SQL Standard Propoasl together. > > Please take a quick look and tell me what is missing or wrong and what > the next steps should be. SQL doesn't actually need DISTINCT ON.  Two syntaxes already provide it: SELECT user_id, status, updated_at,        ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY updated_at DESC) as rn FROM user_statuses QUALIFY rn = 1 SELECT user_id, status, updated_at FROM user_statuses ORDER BY updated_at DESC FETCH FIRST ALL PARTITIONS BY user_id, 1 ROW ONLY However, putting the ordering inside the DISTINCT ON is a big improvement for postgres, imo. SELECT DISTINCT ON (user_id ORDER BY updated_at DESC)     user_id, status, updated_at FROM user_statuses ORDER BY status We can't get rid of the old way of doing it, but that shouldn't prevent us from having the new version. I don't understand what the use case for UNION DISTINCT ON is. Could you please provide one? -- Vik Fearing