Re: [SQL] sum of two queries

From: Kyle Bateman <kyle(at)actarg(dot)com>
To: "G(dot) Anthony Reina" <reina(at)nsi(dot)edu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] sum of two queries
Date: 1999-04-22 18:15:01
Message-ID: 371F6724.C05E431@actarg.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)actarg(dot)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(at)actarg(dot)com (801)377-8033x101
----------------------------------------------------

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Daniel Facciolo Pires 1999-04-22 18:32:24
Previous Message Michael J Davis 1999-04-22 17:48:58 RE: [SQL] sum of two queries