Hi!
I have 3 tables in my in the database, each of them corresponds to a signal from different sensors.
Each sensor collects data at a different frequency. Eg:
Ecg sensor- 200Hz
SpO2 sensor - 10Hz
Heart_rate sensor - 3Hz
Tables can be created as follows:
CREATE TABLE ecg_data (
time TIMESTAMPTZ NOT NULL,
ecg DOUBLE PRECISION
);
INSERT INTO ecg_data SELECT generate_series(‘2022-01-01 00:00:00’,‘2022-01-01 00:01:00’,INTERVAL ‘0.005 second’) , random();
The task is to collect all signal values from sensor 1 (e.g. Ecg) at the moments when the values from sensor 2 and 3 meet a certain condition (E.g Spo2_value <95 and Heart_rate_value < 10)
The query below provides a solution to this problem, but I wonder if I can do it in a better way with your extension.
SELECT DISTINCT ecg_data.time, ecg FROM
(
SELECT
time_bucket(‘1 second’, hr_data.time) AS second,
avg(hr) as hr_avg,
avg(spo2) as spo2_avg
FROM hr_data, spo2_data
WHERE hr_data.hr < 40 AND spo2_data.spo2 < 90 AND time_bucket(‘1 second’, hr_data.time) = time_bucket(‘1 second’, spo2_data.time)
GROUP BY second
ORDER BY second
) AS cnd, ecg_data
WHERE ABS(DATE_PART(‘second’, time_bucket(‘1 second’, ecg_data.time) - cnd.second)) <= 1
ORDER BY ecg_data.time;
Sample output of this query:
time | ecg
2022-01-01 00:00:00+00 | 0.48795574125247043
2022-01-01 00:00:00.005+00 | 0.42955517787342856