Calculate the difference between two rows from two different tables

I have two tables with structures below

table one

╔════╦═══════════╦═══════╦══╗
β•‘ ID β•‘ Date β•‘ value β•‘ β•‘
╠════╬═══════════╬═══════╬══╣
β•‘ 1 β•‘ 1/1/2015 β•‘ 234 β•‘ β•‘
β•‘ 2 β•‘ 1/20/2015 β•‘ 267 β•‘ β•‘
β•‘ 3 β•‘ 1/25/2015 β•‘ 270 β•‘ β•‘
β•šβ•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•©β•β•β•

second table

╔════════════════════════╗
β•‘ start_date β•‘ end date β•‘
╠════════════════════════╣
β•‘ 1/1/2015 β•‘ 1/20/2015 β•‘
β•‘ 1/20/2015 β•‘ 1/25/2015 β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

My conclusion should be

╔════════════════════════╦════════════╗
β•‘ start_date β•‘ end date β•‘ difference β•‘
╠════════════════════════╬════════════╣
β•‘ 1/1/2015 β•‘ 1/20/2015 β•‘ 33 β•‘
β•‘ 1/20/2015 β•‘ 1/25/2015 β•‘ 3 β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•

, , ( ) , sql.

, .

,

select start_date, end_date, ((SELECT
                    table_one.value
                FROM
                    table_one, 
                    table_two 
                WHERE 
                    table_one.date= table_two.end_date(+)
                ) - (
                SELECT
                    table_one.value
                FROM
                    table_one, 
                    table_two 
                WHERE 
                    table_one.date = table_two.start_date(+)
                ))from table_two,table_one where table_two.start_date(+)=table_one.date

ORA-01427 . ?

+4
1

join s:

select t2.start_date, t2.end_date, (t1e.value - t1s.value) as difference
from table2 t2 join
     table1 t1s
     on t2.start_date = t1s.date join
     table1 t1e
     on t2.end_date = t1e.date;

table2 table1, left join .

+6

All Articles