Richard Shaw
05/18/2023, 10:27 PMsql
-- Calculate utilization
SELECT
hour_bucket,
asset,
coalesce(running_duration / (running_duration + COALESCE(stopped_duration, 0)), 0) AS utilization
FROM (
SELECT
time_bucket('1 hour', time) as hour_bucket,
asset,
SUM(case when state = 'running' then duration else 0 end) AS running_duration,
SUM(CASE WHEN state = 'stopped' THEN duration ELSE 0 END) AS stopped_duration
FROM (
SELECT
time,
asset,
state,
EXTRACT(EPOCH FROM (LEAD(time) OVER (PARTITION BY asset ORDER BY time) - time)) AS duration
FROM
public.plates_makino_mills_combined
) subquery
GROUP BY
hour_bucket,
asset
) subquery2
ORDER BY
hour_bucket DESC,
asset;_glenngillen
05/19/2023, 3:57 AMrow_number window function to reduce the data set down to just the state changes. In your case I think I'd do PARTITION BY asset, state ORDER BY time. Then where the row_number column is 1 and you've got just the state changes. Calculate the duration with coalesce(lead(time over...), now()) - timeDanielH
05/19/2023, 5:22 AM_glenngillen
05/19/2023, 6:43 AM_glenngillen
05/19/2023, 6:46 AMrunning
running
stopped
running
stopped
stopped
stopped
stopped
running
running
So could be any number of rows between the current state and the next state change. I'm basing that off the case statement that's trying to check the previous and next states though.DanielH
05/19/2023, 8:06 AMRichard Shaw
05/19/2023, 12:05 PMRichard Shaw
05/19/2023, 2:22 PMRichard Shaw
05/19/2023, 2:52 PMsql
WITH subquery AS (
SELECT
time,
asset,
state,
ROW_NUMBER() OVER (PARTITION BY asset ORDER BY time) AS row_num
FROM
public.plates_makino_mills_combined
)
SELECT
time,
asset,
state,
EXTRACT(EPOCH FROM (LEAD(time) OVER (PARTITION BY asset ORDER BY time) - time)) AS duration
FROM
subquery
WHERE
state <> (
SELECT state
FROM subquery AS inner_subquery
WHERE inner_subquery.row_num = subquery.row_num - 1
LIMIT 1
) OR subquery.row_num = 1;DanielH
05/19/2023, 7:07 PMDanielH
05/20/2023, 3:30 PMDanielH
05/20/2023, 3:30 PMSELECT asset, SUM(EXTRACT(epoch FROM end_time - start_time)) AS running_time
FROM (
SELECT asset, timems AS start_time, LEAD(timems) OVER (PARTITION BY asset ORDER BY timems) AS end_time
FROM plates_makino_mills_combined
WHERE state = 'running'
) AS subquery
WHERE end_time IS NOT NULL
GROUP BY asset;_glenngillen
05/22/2023, 4:49 AMwhere clause at the end?Richard Shaw
05/22/2023, 1:14 PMRichard Shaw
05/22/2023, 1:15 PMrunning, stopped, and `offline `are generated but long term I want to create a more generalized query. Some equipment may have an `idle `state, and our ovens have a recovering state getting back up to temp after opening.Jermuk
05/22/2023, 1:35 PMJermuk
05/22/2023, 1:36 PMJermuk
05/22/2023, 1:46 PMJermuk
05/22/2023, 1:47 PM_glenngillen
05/22/2023, 8:54 PMRichard Shaw
05/23/2023, 12:54 PMasset and state, but if I do that, I won't know exactly when it changed state because I don't have the actual row where the state changed. It would be close, but not exactly right (within a secondish).Richard Shaw
05/23/2023, 12:55 PMstate_agg() function, but it's only in the toolkit version which is only available in the high availability docker image and I loaded the standard one before I knew the difference.jamesgresql
07/05/2023, 2:00 AMstate_agg, that's what it's for. It is blazing fast b/c it's written in Rustsuryavaddadi
08/11/2023, 2:57 PMJermuk
08/14/2023, 9:07 AMJermuk
08/14/2023, 9:08 AMJermuk
08/14/2023, 9:09 AMJermuk
08/14/2023, 9:09 AM