Received: from www.actarg.com (root@[209.180.91.25]) by hub.org (8.9.3/8.9.1) with ESMTP id OAA95693 for ; Thu, 22 Apr 1999 14:11:13 -0400 (EDT) (envelope-from kyle@actarg.com) Received: from tao.actarg.com (root@tao.actarg.com [192.168.1.1]) by www.actarg.com (8.8.7/8.8.7) with ESMTP id MAA09086; Thu, 22 Apr 1999 12:12:57 -0600 Received: from actarg.com (kyle@chi.actarg.com [192.168.2.16]) by tao.actarg.com (8.8.7/8.8.7) with ESMTP id MAA17735; Thu, 22 Apr 1999 12:16:12 -0600 Sender: kyle@actarg.com Message-ID: <371F6724.C05E431@actarg.com> Date: Thu, 22 Apr 1999 12:15:01 -0600 From: Kyle Bateman Organization: Action Target Inc. X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.0.36 i686) MIME-Version: 1.0 To: "G. Anthony Reina" CC: pgsql-sql@postgresql.org Subject: Re: [SQL] sum of two queries References: <371F5BBF.874251FD@actarg.com> <371F5CE9.98C540FD@nsi.edu> Content-Type: multipart/alternative; boundary="------------BD7092BA3E1615817FB96CD5" --------------BD7092BA3E1615817FB96CD5 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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 ---------------------------------------------------- --------------BD7092BA3E1615817FB96CD5 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit 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
 ----------------------------------------------------
  --------------BD7092BA3E1615817FB96CD5--