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 1i8bec-0005ZL-Ji for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2019 02:49:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8beb-0005KF-72 for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2019 02:49:45 +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 1i8bea-0005K7-Rm for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2019 02:49:44 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8beY-0004Y5-KV for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2019 02:49:44 +0000 Received: by mail-pf1-x433.google.com with SMTP id i1so8311471pfa.6 for ; Thu, 12 Sep 2019 19:49:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=kh5ynTLLOFXKo8pgP+PnnvDg/8N9k35t3AnxwfEu9nM=; b=JQlAXKYxaIBX3cqhlDUPynVZGiSfHxNSFCHKRRC1rrXO2dh8jfhccda1i7J8samwN8 LS5LDSv2Nl7iJx3zcI3zPkenjWC3s9lHw7UBvEM6R1G+E7gBlwEmvXR7Go/X4Cv3qJHI 11tFZisFhT1U9Wmt4VUnJuJ+4Ay46gIi4CXLJgE+4k1bccHBYCRraTzYS3BwBruCECCk 5TBzmIWBd7u2QE08qWguoXAD3ih/HnwRqt3zLplPguySjUWpxFqTE5VaI4Is+NkQzsdp 2buK6qjAWwcuUVAogmgVkBLz+JSPcsJiFHdIOSICnDPkLkv/kRAL7JQGyO3g7Ki6/ZHB kD+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=kh5ynTLLOFXKo8pgP+PnnvDg/8N9k35t3AnxwfEu9nM=; b=T40EQVmdGOHXj76s2Gm+HBSCP+nAmdtWJ3KjPlVVIplVveuGZxQJBC3GbFMCsetkDF 42MsQCb6RIzbijM2NuAb9Zj8FsXf9qjY9UMnrrjyfsWkh+zhAjGvwyKzWtODcSspuofj I9rVmauhv8nOxKxyKj6V7+mFS5KwfsUQ6yB2OLO0vntyWHWoYuyLk5LIl9HeKfPR9H5o a90FfeP0X844skV93/aGHT3h/61/ieiFs+yGZzclM5Nz2D3DH/UbKYNMX1LGak+cXaeo 92SRguxEkH5oNmN8N0BOMPvvsPxdMpARaDmGNu5IXTZkD4zyPlNd6n36Lxxh6bG4Zh93 d6SA== X-Gm-Message-State: APjAAAVn3wcbq7Lc4eGtdWzdrKhth9LqeUc96B4yAGBv2W4JPkxecors KLBlibxI0+O1dRS7D4IzOYk= X-Google-Smtp-Source: APXvYqw+5NZs9d5xvtsRfEZJrLBSRoUls72d8GGyXbnHBN+O9IsfYde/Ut/Bl32g7Mx3SfpNSIPq4A== X-Received: by 2002:a63:5c06:: with SMTP id q6mr41486428pgb.45.1568342979933; Thu, 12 Sep 2019 19:49:39 -0700 (PDT) Received: from localhost (w204177.dynamic.ppp.asahi-net.or.jp. [121.1.204.177]) by smtp.gmail.com with ESMTPSA id x20sm14011113pfa.153.2019.09.12.19.49.37 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 12 Sep 2019 19:49:39 -0700 (PDT) Date: Fri, 13 Sep 2019 11:49:28 +0900 (Tokyo Standard Time) Message-Id: <20190913.114928.04657662.horikyota.ntt@gmail.com> To: hjp-pgsql@hjp.at Cc: pgsql-general@lists.postgresql.org Subject: Re: Referncing a calculated column in a select? From: Kyotaro Horiguchi In-Reply-To: <20190912211601.GA3842@hjp.at> References: <20190912192329.GA21150@panix.com> <9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com> <20190912211601.GA3842@hjp.at> User-Agent: Mew version 6.5 on Emacs 22.2 / Mule 5.0 =?iso-2022-jp?B?KBskQjpnGyhCKQ==?= / Meadow-3.01-dev (TSUBO-SUMIRE) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello. At Thu, 12 Sep 2019 23:16:01 +0200, "Peter J. Holzer" wrote in <20190912211601.GA3842@hjp.at> > On 2019-09-12 15:35:56 -0500, Ron wrote: > > On 9/12/19 2:23 PM, stan wrote: > > > I am creating some views, that have columns with fairly complex calculations > > > in them. The I want to do further calculations using the result of this > > > calculation. Right now, I am just duplicating the first calculation in the > > > select fro the 2nd calculated column. There must be a batter way to do > > > this, right? > > > > I must be misunderstanding you, because you can just do more of what you're > > doing now. > > > > Here's an example of doing some calculations in the creation of the view, > > and then more calculations when selecting from the view: > > I think he wants to refer to other columns in the view. .. > What you can do is nest views: Doesn't subquery work? SELECT x, y, z FROM (SELECT f * 3 AS x, f AS y, g + 2 AS z FROM (SELECT a + 3 AS f, b + c AS g FROM t) AS t1 ) AS t2; t2 uses f in two columns, where f is calculated from t.a. Or CTE (WITH clause) might look better. WITH t1 AS (SELECT a + 3 AS f, b + c AS g FROM t), t2 AS (SELECT f * 3 AS x, f AS y, g + 2 AS z FROM t1) SELECT x, y, z FROM t2; regards. -- Kyotaro Horiguchi NTT Open Source Software Center