Search code examples
db2db2-400db2-luw

Repeat the value from left table if date from right table falls between start date and end date


I would like to join my two tables on [Group] and [YearMonth] Dates. Where [YRMO_NB] from Table 2 falls between [ENR_START] AND [ENR_END] from Table 1 then repeat the value of column [PHASE] for each related row, just like the second picture last column = [PHASE] and leaves unmatched rows blank.

I did this which only gives me exact matches:

ON A.GROUP = PHASE.GROUP
AND A.YRMO_NB = PHASE.ENR_START

Table 1

enter image description here

Table 2

enter image description here

Is there an easy way to do this ?

Thank You!


Solution

  • I figured it out

    ON A.GROUP = PHASE.GROUP 
    AND A.YRMO_NB >= PHASE.ENR_START
    and A.YRMO_NB <= PHASE.ENR_END