Groups > Interbase > Interbase SQL > Re: Aggregating two tables




Aggregating two tables

Aggregating two tables
25 Feb 2008 20:42:15 -0700
I have two identical tables (A and B), and I want a SELECT to show the 
difference of two respective SUMs:

SELECT SUM(A.a)-SUM(B.b)

for an ID that is the same for both tables.

For example, A table holds incoming movements, B table holds outgoing 
movements, and we want to see what's remaining.

Any ideas how I can accomplish that? For some reason, I don't get the 
correct result.
Post Reply
Re: Aggregating two tables
25 Feb 2008 22:02:51 -0700
Ron <ron@hotma.com> wrote in news:Xns9A5043CC6F5B6r341n@localhost:

> I have two identical tables (A and B), and I want a SELECT to show the 
> difference of two respective SUMs:
> 
> SELECT SUM(A.a)-SUM(B.b)
> 
> for an ID that is the same for both tables.
> 
> For example, A table holds incoming movements, B table holds outgoing 
> movements, and we want to see what's remaining.
> 
> Any ideas how I can accomplish that? For some reason, I don't get the 
> correct result.
> 
> 

Let me answer my own question :)

select C.somefield, (select sum(A,a) from A where A.id = C.id)
    	    	    	    	-(select sum(B,a) from B where B.id = C.id)

Post Reply
about | contact