Hello all,
I have a question trying to merge two historical tables which the dates are not aligned.
I would like to merge the historical company table active start and end date
with the most recent address within the date range based on the enddate.
example below:
Code:
Company table
CompanyId CompanyName StartDate EndDate
1 APPLE 2016-05-01 2016-12-31
1 APPLE 2017-01-01 2017-05-31
2 GOOGLE 2017-06-01 2017-06-30
2 GOOGLE 2017-07-01 2017-08-31
2 GOOGLE 2017-09-01 2017-09-30
3 FACEBOOK 2017-10-01 2017-12-31
3 FACEBOOK 2018-05-01 2018-10-31
Location Table
CompanyId Location StartDate EndDate
1 NY 2016-05-01 2016-06-30
1 LONDON 2016-07-01 2016-10-31
1 TOKYO 2017-01-01 2017-04-30
1 HONGKONG 2017-05-01 2017-05-21
2 SINGAPORE 2017-06-01 2017-06-15
2 BOSTON 2017-06-16 2017-06-20
2 LA 2017-07-30 2017-08-31
And the expected output would be:
Code:
CompanyId CompanyName StartDate EndDate Location
1 APPLE 2016-05-01 2016-12-31 london
1 APPLE 2017-01-01 2017-05-31 hongkong
2 GOOGLE 2017-06-01 2017-06-30 boston
2 GOOGLE 2017-07-01 2017-08-31 LA
2 GOOGLE 2017-09-01 2017-09-30 null
3 FACEBOOK 2017-10-01 2017-12-31 null
3 FACEBOOK 2018-05-01 2018-10-31 null
Any help would be greatly appreciated.
Thanks