I have a Postgres table like this, with device ID, timestamp, and the status of the device at that time:
dev_id | timestamp | status
----------------------------------------
1 | 2020-08-06 23:00:00 | 1
2 | 2020-08-06 23:00:00 | 0
3 | 2020-08-06 23:00:00 | 1
2 | 2020-08-06 23:05:00 | 1
3 | 2020-08-06 23:05:00 | 0
1 | 2020-08-06 23:10:00 | 0
I want to see in their respective latest timestamp, how many of devices were functioning and how many not functioning. In Postgres, I can use DISTINCT ON
and write the query like this:
SELECT status, COUNT(status)
FROM
(
SELECT DISTINCT ON (dev_id) dev_id,
timestamp,
status
FROM
sample_metrics_data
ORDER BY
dev_id,
timestamp DESC
) sub
GROUP BY status;
This will result in:
value | count
---------------
0 | 2
1 | 1
(2 devices, #1 & #3, have a status of 0, while 1, #2, has a status of 1.)
How can I create something like this in CubeJS? Is DISTINCT ON
supported, and if not, what is the way around it?
Alternatively, the query can be written using inner join:
SELECT status,
Count(status)
FROM sample_metrics_data
JOIN (SELECT dev_id id,
Max(timestamp) ts
FROM sample_metrics_data
GROUP BY dev_id) max_ts
ON timestamp = max_ts.ts
AND dev_id = max_ts.id
GROUP BY status;
I would need to do an inner join, but it seems only LEFT JOIN is available.