Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract values using SQL

Could you tell me how can I subtract values?

SQL>

select SUM(bytes/1024/1024) from dba_data_files where TABLESPACE_NAME='UNDOTBS1'

2 ;

SUM(BYTES/1024/1024)
--------------------
            7000

SQL>

select SUM(BYTES/1024/1024) from DBA_UNDO_EXTENTS where STATUS LIKE 'ACTIVE';

SUM(BYTES/1024/1024)
--------------------
               8

I need to get a value 7000 - 8

When I do

select SUM(bytes/1024/1024) from dba_data_files where TABLESPACE_NAME='UNDOTBS1'
minus
select SUM(BYTES/1024/1024) from DBA_UNDO_EXTENTS where STATUS='ACTIVE';

I get result only from the first select.

like image 694
ntdrv Avatar asked Aug 23 '26 09:08

ntdrv


1 Answers

Use the dual pseudo table here and evaluate the sums as scalars:

SELECT 
   (SELECT SUM(bytes/1024/1024) as sum_a 
    from dba_data_files where TABLESPACE_NAME='UNDOTBS1')
 - (select SUM(BYTES/1024/1024) as sum_b 
    from DBA_UNDO_EXTENTS where STATUS LIKE 'ACTIVE') as Difference
FROM
    dual;
like image 116
StuartLC Avatar answered Aug 26 '26 23:08

StuartLC