pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kyle Bateman <kyle@actarg.com>
To: G. Anthony Reina <reina@nsi.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] sum of two queries
Date: Thu, 22 Apr 1999 12:15:01 -0600
Message-ID: <371F6724.C05E431@actarg.com> (raw)
References: <371F5BBF.874251FD@actarg.com>
	<371F5CE9.98C540FD@nsi.edu>

G. Anthony Reina wrote:

> Kyle Bateman wrote:
>
> > Is there a way to return the sum of two separate queries as in:
> >
> > select (select sum(price) from items) + (select sum(price) from
> > widgets))
> >
> > select 3 + 4 works OK but the parser doesn't like sticking a subquery in
> > place
> > of the numbers.
> >
> > Is there another way to do this?
> >
> > kyle@actarg.com
>
> Try :
>
> select sum(a.price + b.price) from items as a, widgets as b;
>
> -Tony Reina

I explained my problem badly.

Here's the full thing.  I'm doing this, which works:

select -sum(tquant) from mtr_reg where
    status = 'clsd' and
    fr_proj = 20 and
    pnum = '1122'
union
select sum(tquant) from mtr_reg where
    status = 'clsd' and
    to_proj = 20 and
    pnum = '1122'
;
This yields two numbers, one negative and the other positive.  If I add them
together
in the application, I get the number I really want which represents the total
number of
part 1122 that have come into inventory (project 20) minus the total number
that have gone out
(i.e. current stock level).

What I'm trying to do is to have SQL do the addition rather than having to
read the
two sums separately and add them externally.

I think the example you gave adds up all the prices of all the combinations
of pairs from
aliases a and b (billions and billions...).


--
 ----------------------------------------------------
 Kyle Bateman       President, Action Target Inc.
 "Viva Yo!"         kyle@actarg.com (801)377-8033x101
 ----------------------------------------------------

view thread (4+ messages)  latest in thread

Message-ID: <371F6724.C05E431@actarg.com>
Permalink:  ../371F6724.C05E431@actarg.com/
Also on:    postgresql.org/message-id/371F6724.C05E431@actarg.com

 · 

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-sql@postgresql.org
  Cc: kyle@actarg.com, reina@nsi.edu
  Subject: Re: [SQL] sum of two queries
  In-Reply-To: <371F6724.C05E431@actarg.com>
  References: <371F5BBF.874251FD@actarg.com> <371F5CE9.98C540FD@nsi.edu> <371F6724.C05E431@actarg.com>

* 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