'Netezza SQL Join dataset A to dataset B but pull fields from B when b_date > a_date

I have 2 datasets from 2 different sources but many of the members are the same in both datasets. My select statement is :

Select a.member_id, a.start_date, a.customer_id, a.region_id, b.b_start_date, b.customer_id, b.region_id
from dataset1 a
left join dataset2 b
on a.member_id=b.member_id

I want to somehow pick up all recs in A and recs in B where a.member_id = b.member_id but bring in the fields from A when a.start_date = b.b_start_date or a.start_date > b.b_start_date and bring in the fields from B when b.b_start_date > a.start_date.

Here's a pretty small example:

Dataset A:

member_id start_date customer_id region_id
1111 1/30/2021 123 555
2222 1/30/2021 222 555
3333 1/1/2021 345 678

Dataset B:

member_id b_start_date customer_id region_id
1111 1/1/2022 567 444
2222 1/30/2021 222 555

Result:

member_id customer_id region_id
1111 567 444
2222 222 555
3333 345 678


Solution 1:[1]

/* try this */

select a.* from a inner join b using (member_id) where a.start_date >= b.b_start_date
union all
select b.* from a inner join b using (member_id) where b.b_start_date > a.start_date;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark F