Skip to content

Instantly share code, notes, and snippets.

@mlivingston40
Last active March 29, 2024 17:54
Show Gist options
  • Save mlivingston40/db28e49161db2ce73df625f57d5f82b7 to your computer and use it in GitHub Desktop.
Save mlivingston40/db28e49161db2ce73df625f57d5f82b7 to your computer and use it in GitHub Desktop.
find the average processing time by machine for all processes
-- Write your PostgreSQL query statement below
/*
Lets first do a table scan to get every process_id's time delta
*/
with process_time_delta as (
Select
machine_id,
process_id,
activity_type,
timestamp,
LAG(timestamp, 1) OVER (
Partition by machine_id, process_id
ORDER by timestamp asc
) as start_timestamp
From Activity
)
select
a.machine_id,
round(avg(a.timestamp - p.start_timestamp)::numeric, 3) as processing_time
from Activity a
inner join process_time_delta p
on p.machine_id = a.machine_id
and p.process_id = a.process_id
where a.activity_type = 'end' /* only need the end activity timestamp */
group by 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment