Question

First table

ID  Desc    Effective date  Alias
1   abc 12-JAN-10   A1
2   efg 10-FEB-11   A2
3   hij 03-MAR-11   A3
4   klm 05-APR-12   A4

Second table

Split Date  Alias   Value
13-JAN-10   A1  2
14-JAN-11   A1  3
09-FEB-11   A2  5
04-MAR-12   A3  7
25-DEC-12   A3  1
21-JUL-13   A4  2
30-NOV-12   A4  3

Resulting table:

ID  Desc    Effective date  Alias   Value
1   abc 12-JAN-10   A1  5   --it got 5 because it sums up values from 2nd table where effective date >split date
2   efg 10-FEB-11   A2  1
3   hij 03-MAR-11   A3  8
4   klm 05-APR-12   A4  5

Condition:

Sum all the values from the second table where Effective_date > Split Date using Effective date and Alias from the first table as keys to the second table. If it did not find effective_date > split date, then value in the resulting table should be 1.

No correct solution

OTHER TIPS

Here is a method using left outer join:

select t1.id, t1.desc, t1.effective_date, t1.alias,
       coalesce(sum(t2.value), 1) as value
from table1 t1 left outer join
     table2 t2
     on t1.alias = t2.alias and
        t1.effective_date < t2.split_date
group by t1.id, t1.desc, t1.effective_date, t1.alias;

I changed the logic to t1.effective_date < t2.split_date to match the results in the question, although this is the opposite logic that you describe.

Could you please try the following query ?

select t1.id, t1.desc, t1.effective_date, t1.alias, nvl(sum(t2.value), 1) value
from table1 t1, table2 t2
where t2.alias (+) = t1.alias
  and t2.split_date (+) > t1.effective_date
group by t1.id, t1.desc, t1.effective_date, t1.alias

The joins used are old-styled, but I think this can work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top