Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: PL/SQL taking data from two separate Tables, then +/- that data

Re: PL/SQL taking data from two separate Tables, then +/- that data

From: Christopher Beck <christopher.beck_at_oracle.com>
Date: Tue, 16 Jul 2002 17:52:57 -0400
Message-ID: <_B0Z8.10$387.309@news.oracle.com>

Tom,

Why do you think you need PL/SQL?

part a.

select nvl(table_a.val,0) - nvl(table_b.val,0)   from ( select sum(col) val from table_a ) table_a,

          ( select sum(col) val from table_b ) table_b /

or

select ( select nvl(sum(col),0) from table_a ) - (select nvl(sum(col),0) from table_b )
  from dual
/

part b.

This is just like part a...

select nvl(series1.val,0) - nvl(series2.val,0)   from ( select sum(col) val from table_a where id < 11 ) series1,

          ( select sum(col) val from table_a where id between 11 and 20 ) series2
/

or

select ( select nvl(sum(col),0) from table_a where id < 11 ) -

          ( select nvl(sum(col),0) from table_a where id between 11 and 20 )   from dual
/

if you want/need to use PL/SQL you can by using the queries above or use two separate queries like...

declare
  table_a_val number;
  table_b_val number;
begin
  select nvl(sum(col),0)
    into table_a_val
    from table_a;

   select nvl(sum(col),0)
    into table_b_val
    from table_b;

  dbms_output.put_line( table_a_val - table_b_val ); end;
/

hope this helps.

chris.

--
Christopher Beck, Principal Technologist, Oracle Corporation,
christopher.beck_at_oracle.com
Beginning Oracle Programming,
http://www.amazon.com/exec/obidos/ASIN/186100690X




"Tom_yaya" <tom_yaya_at_yahoo.com> wrote in message
news:628da130.0207161311.7e3aff68_at_posting.google.com...

> Ok heres a good one. I got two tables I wanna pull data from. You got
> Table A and Table B. In Table A, I have a column that is summed, and
> in Table B I have a column that is summed. I want to get the two
> totals from these columns and subtract one from the other. Very
> simply, how do I do that.
>
> Ok, now I got one last question. I have a series of rows in a table
> that are added up, and I get a value. I also have another series of
> rows in the same table that is added up and I get a different value.
> Like rows 1 - 10 = 10$ and rows 20 - 25 = 5$. I want to get the two
> summed values to subtract from one another, like 10$ - 5$ = 5$. How do
> I do that?
Received on Tue Jul 16 2002 - 16:52:57 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US