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 1i8gAx-0006H0-B1 for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2019 07:39:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8gAv-0001VN-Vu for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2019 07:39:25 +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 1i8gAv-0001VD-Lg for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2019 07:39:25 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8gAs-0007UI-3P for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2019 07:39:25 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 53C7E86B; Fri, 13 Sep 2019 09:39:19 +0200 (CEST) Date: Fri, 13 Sep 2019 09:39:19 +0200 From: "Peter J. Holzer" To: Kyotaro Horiguchi Cc: hjp-pgsql@hjp.at, pgsql-general@lists.postgresql.org Subject: Re: Referncing a calculated column in a select? Message-ID: <20190913073919.GA15908@hjp.at> References: <20190912192329.GA21150@panix.com> <9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com> <20190912211601.GA3842@hjp.at> <20190913.114928.04657662.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="zYM0uCDKw75PZbzx" Content-Disposition: inline In-Reply-To: <20190913.114928.04657662.horikyota.ntt@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --zYM0uCDKw75PZbzx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-09-13 11:49:28 +0900, Kyotaro Horiguchi wrote: > 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 cal= culations > > > > 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 think he wants to refer to other columns in the view.=20 > .. > > What you can do is nest views: >=20 > Doesn't subquery work? Yes, a subquery would work, too. > Or CTE (WITH clause) might look better. CTEs (which I quite like in general) are optimisation barriers, so using them in a view may lead to inefficient plans (depending on what you do with the view). So, yes, it would work, but you have to check the plans of your queries (and be prepared to rewrite your view if they aren't good enough), which is why I didn't suggest it. hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --zYM0uCDKw75PZbzx Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl17R6AACgkQ8g5IURL+ KF0bhw/+O/XlHSJGO7Cq6YPHGf1Zzl8AP53iXTS+EFbZy1q+qoaswSFkAnN+78LI BiADizV9x9RBHsnH59mB6dU6eV0Mzmk2jI983/6bDSD3iJaEy9dmCXnHc5YB7Jvs FSf/rU3YvggRn/mtYHq/QwGUQAp9BDrzN8RvM+Ni0QFnFXEvKAIug/OMSppMeUe2 ni/yNdsdIRq86z1WpMirEP0kGtjvZFgNmcd8GTRQb0s/wc36vByEXlCbuIvZo5XB wr77LH/zmWR6QfM1LIHV+OgGG8DhXwZLZXMKlqkQ+GaXqfLR7iBWoZXQCUDG0MiZ PWrIpepTwphK0gm7vQjOprsi5plo1gQ9T6W9TiC+K68SJGe0P0NQ+DNoN1LtXWg2 ySdurzzLccKGDogB+kJ3+uRkjyLmzKOfi1vai1HLaIsK/h3tfmHl0RF3bUr02JnE Yr5qJwbGbVXNEHGeranCw/Li7qG9CQrFJ4RM8sp/OiWIkPk0/4pmLLmHgm1n/KCx X5+QOSLJrdhOizcxavFthiJzQhSQJbfgZ3p2WgVIbeMUVjjoLlitdSOumwpbCU2+ QKJW5g5AQbgxlDdpfPm6bTLqxl2zxSRqiyvVc7yLflmvg9ndxdIon72B6K4oIXo0 ujH70xNBlFtKdJaKrHHsfU9tzcw/Yc6CiY5SA0KYfWXBjzv6/T8= =BtNF -----END PGP SIGNATURE----- --zYM0uCDKw75PZbzx--