From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: hjp-pgsql@hjp.at
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Referncing a calculated column in a select?
Date: Fri, 13 Sep 2019 09:39:19 +0200
Message-ID: <20190913073919.GA15908@hjp.at> (raw)
In-Reply-To: <20190913.114928.04657662.horikyota.ntt@gmail.com>
References: <20190912192329.GA21150@panix.com>
<9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com>
<20190912211601.GA3842@hjp.at>
<20190913.114928.04657662.horikyota.ntt@gmail.com>
On 2019-09-13 11:49:28 +0900, Kyotaro Horiguchi wrote:
> At Thu, 12 Sep 2019 23:16:01 +0200, "Peter J. Holzer" <hjp-pgsql@hjp.at> 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 think he wants to refer to other columns in the view.
> ..
> > What you can do is nest views:
>
> 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
--
_ | 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 <https://www.edge.org/;
Attachments:
signature.asc (application/pgp-signature, 832B) download
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: hjp-pgsql@hjp.at, horikyota.ntt@gmail.com
Subject: Re: Referncing a calculated column in a select?
In-Reply-To: <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> <20190913073919.GA15908@hjp.at>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox