Skip to content
Advertisement

SQL Left Join Within Timeframe Window

I have two datasets:

I want to create a dataset where if dataset b is within 1 day of timestamp of dataset a by timestamp, user, and group then it is a match. Has anyone done something like this before where it is something like left join on dataset_b.timestamp between dataset_a.timestamp and date_add(dataset_a.timestamp,-1). I’d like to have the flexibility where in the future I can test out -7 days as well so it is easily modifiable.

Expected output below:

Advertisement

Answer

The JOIN condition does not have to be only equality operator so:

is a valid join.

db<>fiddle demo

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