I have a same problem in Python before: Convert Interval Outer Join SQL in Python Pandas Dataframe and I would like to do it in R.
I'm converting an Oracle SQL outer interval join into R. Below is the Oracle SQL:
WITH df_interval AS
(SELECT '1' id,
'AAA' interval,
1000 begin,
2000 end
FROM DUAL
UNION ALL
SELECT '1' id,
'BBB' intrvl,
2100 begin,
3000 end
FROM DUAL
UNION ALL
SELECT '2' id,
'CCC' intrvl,
3100 begin,
4000 end
FROM DUAL
UNION ALL
SELECT '2' id,
'DDD' intrvl,
4100 begin,
5000 end
FROM DUAL),
df_point AS
(SELECT '1' id, 'X1' point, 1100 mid FROM DUAL
UNION ALL
SELECT '1' id, 'X2' point, 2050 mid FROM DUAL
UNION ALL
SELECT '1' id, 'X3' point, 3200 mid FROM DUAL
UNION ALL
SELECT '2' id, 'X4' point, 4200 mid FROM DUAL
UNION ALL
SELECT '2' id, 'X5' point, 5500 mid FROM DUAL)
SELECT pt.id,
point,
mid,
interval
FROM df_interval it RIGHT OUTER JOIN df_point pt ON pt.id = it.id AND pt.mid BETWEEN it.begin AND it.end
I expect the result like this:
ID point mid interval
0 1 X1 1100 AAA
1 1 X2 2050 NaN
2 1 X3 3200 NaN
3 2 X4 4200 DDD
4 2 X5 5500 NaN
Appreciate anyone can help me on this?