Skip to content
Advertisement

Join two tables on a condition of ID and a date from first table is between two other dates in another table

I’m trying to add to table 1 time-related data from table 2. In table 1 I have ID, date. In table 2 I have ID, DateFrom, DateTo. IDs, dates repeating. t1 for example:

t2:

What I want to get – is to fill Type with proper data from t2:

What I have done for now:

Is it correct?

Advertisement

Answer

A join seems the relevant approach here.

The parentheses around conditions are not necessary. Whether you want an inner join or a left join depends on the possibility of orphan records and how you want to handle them: inner join removes records in t1 that have no match in t2, while left joins allows them (the resulting type will be null):

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement