Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iz2b7-0001fr-2m for pgsql-docs@arkaria.postgresql.org; Tue, 04 Feb 2020 18:06:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iz2b5-0005cx-Sd for pgsql-docs@arkaria.postgresql.org; Tue, 04 Feb 2020 18:06:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iz2b5-0005cp-HS for pgsql-docs@lists.postgresql.org; Tue, 04 Feb 2020 18:06:51 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iz2b1-0006Mz-Sn for pgsql-docs@lists.postgresql.org; Tue, 04 Feb 2020 18:06:50 +0000 Received: by mail-wr1-x443.google.com with SMTP id a6so24247635wrx.12 for ; Tue, 04 Feb 2020 10:06:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=7tcKBwsMlgzAXEXLeClDZBVFQtmXruIbHRlY+QzkZks=; b=yIiITM65CDWeIKo7sULtigdMLH7IGNyga8xMYY7DKmhOIvJKHLqtQRnwW8wheMhDSZ otLluWllhHsoER4s6Ln1naXkCNbZ3iuUEZfCk5CLSw1ZrQORxXxbhk/8pGoeyoHfTE6B NbTJkblmHoteXiE/jY8Sq9UgUWR7er1VZfHFZUgqxA2+xlCwgvg/8kFIkId1rqjVFLNv M8clXTG2Sea5BKgm5DqWEUh87ETK7Q4fPzqjvpOsPGxw/dmOgyPLTqIFX1zs6H5pPg9x /bHnVaO8PLvLQREvP7mDWx7FppEvX5oqGTuPREMlsWKRPIZJ5dCZrCfMiFrHMlN4jM9J tk0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=7tcKBwsMlgzAXEXLeClDZBVFQtmXruIbHRlY+QzkZks=; b=N4kVZdjbOFzkC6FfL9ohVUXhib+QiNODRnovcinu5WtzRuw0ETJ8OU0mfnVapxoyIa er8wTj5AgQudqztGVFFMcByV9WLsNhhiE1vGByu/zMHe2jUh2AY5vizKJGOe6Vu4jPbR TW5um7/C0FUaMqd/MD7CNThaIY+XxSEfpIdlkx2CI5lVrSyWk6togYmhYJZXxy+K1BI9 sM1E6dlVch/gOKj01Rl1E1ycJ2dekRnNdq2aReYpE835PpSoBhv/emzGqY34oLylwwex wNaRTabVMn6EBNnoZcVuki4vRchIlsQRpub4q94fl9wogQ6Dv3NeJXAsdGyHIKJM4FJN atdg== X-Gm-Message-State: APjAAAV2bqsSxsCLQ2Nn5b5jKYsR3O+kBDOmxTMXovuwrqq1rbL5gMix UT8k451nchAPM0e4k/CA6pUgZzUaEjYoAMrO X-Google-Smtp-Source: APXvYqwneyZbkLjqhxX0Wh7lLx/RHAQjjnjzXVL8H2HUkeYtvEZc/r5XsouIoJ76Dqjl6qp2FkqD6A== X-Received: by 2002:a5d:438c:: with SMTP id i12mr22625086wrq.51.1580839605582; Tue, 04 Feb 2020 10:06:45 -0800 (PST) Received: from JW-Phone (89-212-56-189.static.t-2.net. [89.212.56.189]) by smtp.gmail.com with ESMTPSA id k13sm30289510wrx.59.2020.02.04.10.06.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2020 10:06:45 -0800 (PST) Message-ID: <5947ef175e787a4b298dfcf7879698f799fcfab7.camel@cybertec.at> Subject: Re: documentation extension request - order with function over aggregated functions From: Laurenz Albe To: estanislao.gonzalez@projo.zone, pgsql-docs@lists.postgresql.org Date: Tue, 04 Feb 2020 19:06:44 +0100 In-Reply-To: <158080678185.1098.6542165890616974586@wrigleys.postgresql.org> References: <158080678185.1098.6542165890616974586@wrigleys.postgresql.org> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, 2020-02-04 at 08:59 +0000, PG Doc comments form wrote: > SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong > > It took me a while to figure out how to do it properly so I propose to add > the following (or similar) info: > > --- > If you need to sort by this kind of expression, you will have to retrieve an > extra column with the required computation and use it as a sorting key: > > > SELECT a + b AS sum, c FROM table1, a + b + c AS my sorting_key ORDER BY > my_sorting_key; -- correct Not quite correct, because you get an extra unnecessary output column. You can either not use an alias in ORDER BY: SELECT a + b AS sum, c FROM table1 ORDER BY a + b + c; or you can use a subquery: SELECT sum, c FROM (SELECT a + b AS sum, c FROM table1) AS subq ORDER BY sum + c; Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com