pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedsum of two queries
4+ messages / 3 participants
[nested] [flat]
* sum of two queries
@ 1999-04-22 17:26 Kyle Bateman <kyle@actarg.com>
0 siblings, 1 reply; 4+ messages in thread
From: Kyle Bateman @ 1999-04-22 17:26 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* RE: [SQL] sum of two queries
@ 1999-04-22 17:48 Michael J Davis <michael.j.davis@tvguide.com>
0 siblings, 0 replies; 4+ messages in thread
From: Michael J Davis @ 1999-04-22 17:48 UTC (permalink / raw)
To: 'Kyle Bateman' <kyle@actarg.com>; pgsql-sql
I use functions. For example:
Create function1 as
'select sum(price) from items';
Create function2 as
'select sum(price) from widgets';
select function1 + function2;
-----Original Message-----
From: Kyle Bateman [SMTP:kyle@actarg.com]
Sent: Thursday, April 22, 1999 11:26 AM
To: pgsql-sql@postgreSQL.org
Subject: [SQL] sum of two queries
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] sum of two queries
@ 1999-04-22 18:15 Kyle Bateman <kyle@actarg.com>
parent: Kyle Bateman <kyle@actarg.com>
0 siblings, 1 reply; 4+ messages in thread
From: Kyle Bateman @ 1999-04-22 18:15 UTC (permalink / raw)
To: G. Anthony Reina <reina@nsi.edu>; +Cc: 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@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
----------------------------------------------------
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] sum of two queries
@ 1999-04-22 21:23 G. Anthony Reina <reina@nsi.edu>
parent: Kyle Bateman <kyle@actarg.com>
0 siblings, 0 replies; 4+ messages in thread
From: G. Anthony Reina @ 1999-04-22 21:23 UTC (permalink / raw)
To: Kyle Bateman <kyle@actarg.com>; pgsql-sql
Kyle Bateman wrote:
>>
>
> 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'
> ;
I think this will work (it seems to work when I tried it with my
database):
select sum(a.tquant - b.tquant) from mtr_reg as a, mtr_reg as b where
a.status = 'clsd' and
b.status = 'clsd' and a.pnum = '1122' and b.pnum = '1122' and a.fr_proj
= 20 and b.to_proj = 20;
-Tony
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~1999-04-22 21:23 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-04-22 17:26 sum of two queries Kyle Bateman <kyle@actarg.com>
1999-04-22 18:15 ` Kyle Bateman <kyle@actarg.com>
1999-04-22 21:23 ` G. Anthony Reina <reina@nsi.edu>
1999-04-22 17:48 RE: [SQL] sum of two queries Michael J Davis <michael.j.davis@tvguide.com>
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