Hello,
I am quite new to SQL and especially timescaleDB but already enjoying it and I see the power of it.
Anyway, I have a problem with one query.
I have two tables which I want to join. Each table has a timestamp and a machine-id as PK.
One table (position) has data in very high frequency (every millisecond) and one is event based (events).
If I left join the event based table on the high frequency table, I get incorrect results.
Query:
select time as ts, x as hf_data, changes as event_data from position
left join events on position.machine_id = events.machine_id and position.time = events.time
where position.machine_id ='xyxy' and position.time between '...' and '...'
and events.machine_id ='xyxy' and events.time between '...' and '...'
order by ts
I expect an output as follows (every millisecond):
ts | hf_data | event_data |
---|---|---|
2022-01-25 14:06:34.505+00 | 302.0 | 1265.0 |
2022-01-25 14:06:34.506+00 | 404.0 | Null |
2022-01-25 14:06:34.507+00 | 500.0 | Null |
2022-01-25 14:06:34.508+00 | 350.0 | Null |
… | … | … |
2022-01-25 14:06:34.545+00 | 299.0 | 1264.0 |
But instead I only get the output of the timestamps where both tables have values:
ts | hf_data | event_data |
---|---|---|
2022-01-25 14:06:34.505+00 | 302.0 | 1265.0 |
2022-01-25 14:06:34.545+00 | 299.0 | 1264.0 |
2022-01-25 14:06:34.665+00 | 541.0 | 1263.0 |
2022-01-25 14:06:34.697+00 | 786.0 | 1264.0 |
2022-01-25 14:06:34.729+00 | 771.0 | 1263.0 |
So the output is like if I do an right join but I did a left join.
Additionally I tried the query with a lateral join which works but is obviously very slow.
Any help is very much appreciated. Thanks
Marco