I am using DolphinDB version 3.00.0.6 and have two in-memory tables A and B.
I need the equivalent of a Non-Equi Join with strict inequality (A.date > B.date) to find the latest record in B that is strictly before each record in A.
My Sample Data:
A = table(
2023.01.01 2023.01.02 2023.01.03 as date,
`AAPL`AAPL`AAPL as stock_id,
10.0 20.0 30.0 as valA
)
B = table(
2023.01.01 2023.01.02 2023.01.04 as date,
`AAPL`AAPL`AAPL as stock_id,
100.0 200.0 400.0 as valB
)
I first tried asof join:
select * from aj(A, B, `stock_id`date)
date stock_id valA B_date valB
---------- -------- ---- ---------- ----
2023.01.01 AAPL 10 2023.01.01 100
2023.01.02 AAPL 20 2023.01.02 200
2023.01.03 AAPL 30 2023.01.02 200
But what I expect is:
date stock_id valA B_date valB
---------- -------- ---- ---------- ----
2023.01.01 AAPL 10
2023.01.02 AAPL 20 2023.01.01 100
2023.01.03 AAPL 30 2023.01.02 200
I also tried to use non-equi join syntax:
select * from A join B on A.stock_id=B.stock_id and A.date > B.date
But with an syntax error:
Syntax Error: [line #14] A join filter can't involves columns in two or more tables.
DolphinDB seems not support such syntax.
Is there a built-in way to modify aj for strict inequality? How can I achieve the equivalent of A.date > B.date join condition in DolphinDB?
Since DolphinDB aj matches <=, you can subtract a minimal time unit (like 1 day for date columns, or 1 nanosecond for datetime/timestamp) from A.date before joining:
// Prepare sample data
A = table(
2023.01.01 2023.01.02 2023.01.03 as date,
`AAPL`AAPL`AAPL as stock_id,
10.0 20.0 30.0 as valA
)
B = table(
2023.01.01 2023.01.02 2023.01.04 as date,
`AAPL`AAPL`AAPL as stock_id,
100.0 200.0 400.0 as valB
)
// Use aj with adjusted date to simulate strict inequality
A1 = select date, stock_id, valA, date - 1 as date_adj from A
result = select A1.date, A1.stock_id, A1.valA, B.date as B_date, B.valB
from aj(A1, B, `stock_id`date_adj)
Ouput
date stock_id valA B_date valB
---------- -------- ---- ---------- ----
2023.01.01 AAPL 10
2023.01.02 AAPL 20 2023.01.01 100
2023.01.03 AAPL 30 2023.01.02 200