pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Referncing a calculated column in a select?
Date: Thu, 12 Sep 2019 23:16:01 +0200
Message-ID: <20190912211601.GA3842@hjp.at> (raw)
In-Reply-To: <9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com>
References: <20190912192329.GA21150@panix.com>
	<9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com>

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. 

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=> select * from v2; 
╔══════════════╤═════════╤══════════════╤══════════════╤═══════════════════╗
║      a       │    b    │   abserror   │     mag      │     relerror      ║
╟──────────────┼─────────┼──────────────┼──────────────┼───────────────────╢
║            2 │       3 │            1 │            3 │ 0.333333333333333 ║
║           -2 │       3 │            5 │            3 │  1.66666666666667 ║
║ 3.1415926536 │ 2.71828 │ 0.4233126536 │ 3.1415926536 │ 0.134744602587137 ║
╚══════════════╧═════════╧══════════════╧══════════════╧═══════════════════╝
(3 rows)

No idea whether this is more or less efficient than writing the whole
formula for each column.

        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

view thread (5+ messages)  latest in thread

Message-ID: <20190912211601.GA3842@hjp.at>
Permalink:  ../20190912211601.GA3842@hjp.at/
Also on:    postgresql.org/message-id/20190912211601.GA3842@hjp.at

 · 

reply

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
  Subject: Re: Referncing a calculated column in a select?
  In-Reply-To: <20190912211601.GA3842@hjp.at>
  References: <20190912192329.GA21150@panix.com> <9a55a78e-6a9d-b8a0-d9c8-f5a6fbc6fcea@gmail.com> <20190912211601.GA3842@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