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 1i8WSj-0002Rs-Vv for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 21:17:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8WRk-0006yb-8z for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 21:16:08 +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 1i8WRj-0006yU-VT for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 21:16:08 +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 1i8WRg-0001U5-7Y for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 21:16:07 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id B403983F; Thu, 12 Sep 2019 23:16:01 +0200 (CEST) Date: Thu, 12 Sep 2019 23:16:01 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Referncing a calculated column in a select? Message-ID: <20190912211601.GA3842@hjp.at> References: <20190912192329.GA21150@panix.com> <9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="bg08WKrSYDhXBjb5" Content-Disposition: inline In-Reply-To: <9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@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 --bg08WKrSYDhXBjb5 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 calcula= tions > > 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? >=20 > I must be misunderstanding you, because you can just do more of what you'= re > doing now. >=20 > 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.=20 Something like create or replace view v as select a, b, abs(b - a) as abserror, case when abs(a) > abs(b) then abs(a) else abs(b) end as mag, abserror / mag as relerror from t; Except that this doesn't work. What you can do is nest views: create or replace view v as select a, b, abs(b - a) as abserror, case when abs(a) > abs(b) then abs(a) else abs(b) end as mag from t; create or replace view v2 as select a, b, abserror, mag, abserror / mag as relerror from v; wds=3D> select * from v2;=20 =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=A4=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=97 =E2=95=91 a =E2=94=82 b =E2=94=82 abserror =E2=94=82 = mag =E2=94=82 relerror =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=95=A2 =E2=95=91 2 =E2=94=82 3 =E2=94=82 1 =E2=94=82 = 3 =E2=94=82 0.333333333333333 =E2=95=91 =E2=95=91 -2 =E2=94=82 3 =E2=94=82 5 =E2=94=82 = 3 =E2=94=82 1.66666666666667 =E2=95=91 =E2=95=91 3.1415926536 =E2=94=82 2.71828 =E2=94=82 0.4233126536 =E2=94=82 3= =2E1415926536 =E2=94=82 0.134744602587137 =E2=95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=A7=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=9D (3 rows) No idea whether this is more or less efficient than writing the whole formula for each column. 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 --bg08WKrSYDhXBjb5 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl16tYwACgkQ8g5IURL+ KF1uMxAAqT4Rr9MswG4zHAUgt14+XYmWLURRT0oaDJwZeDEzwjur7tcScuO8vC4/ kxUq9Mlz6AlZeo5QlPqmmOZL2Y4P7pEXsDao7dbLtppZxPbDw4lLuHzDHY1S+J6B 0adF1r8KrcOxcFaFQcKJnjlTYIswIwoXG2F2ijVzNr77BC85t2aNq8thSil+4y3d ZBevtu9TajVQPaSr5oTTgIKbAgYuxyYY1G61wdcXl3jnEquLyGZUQjsgx5wyudIa aYStxnN+UzrVAkAqcHOG/kN5mnxkSvJ9LmVZOD3Li2ixOHGfxuRNvhCaDddcRKKV B35Tq6vxhnGDmryMoBngggv+wvgG6c8sMyfdXThuoxU0qzD1YV4sbGm+0ePLUOvx xg8P5UIjaenk4/z0784S80O9rEeYQJNm48+vs/eZta1kAbB+esPXWg12Xhr0FOL4 Ws7ywxXjhlT7RQ5RtVDQVDXkYaXnYV06IAl7atLAqHXyXHL87Pr6wTrOgPi2e/NK Op3w9GpJvBisVXV7oUxJikWFVRb6x4K4Eyj8K8MMG9A1fEDehlK2TnWyIFKW6y9i MmYrgFHfjI9gUurpy3v9LTIEjCH8NGH3rHRA3Asxz6AvEBFZ8KPej6kZlt8LHBtq scKNuflsV+PQbC4u8VcoxglgkUs+EYGESWzKwIp643KLMHMDhbw= =7a5q -----END PGP SIGNATURE----- --bg08WKrSYDhXBjb5--