SQL SOLUTION | | EASY TO UNDERSTAND | | [BEATS 75%]
Approach\nWe create four tables e1,e2 and e3.\n\ne1 - Represents the unique machine id in the activity table\n\n(select DISTINCT machine_id from activity) e1, \
# Approach\nWe create four tables e1,e2 and e3.\n\ne1 - Represents the unique machine id in the activity table\n```\n(select DISTINCT machine_id from activity) e1, \n```\n\ne2 - the sum of start timestamps grouped by machine_id\n```\nselect machine_id,round(sum(timestamp),3) as sum_start from \nactivity where activity_type="start" group by machine_id) e2,\n```\ne3 - the sum of end timestamps grouped by machine_id\n```\nselect machine_id,round(sum(timestamp),3) as sum_end from \nactivity where activity_type="end" group by machine_id) e3,\n```\ne4 - the count of the process_id grouped by machine_id divided by 2\n```\nselect machine_id,count(*)/2 as cnt from activity \ngroup by machine_id) e4\n```\nWe use machine_id for the connections made between each table.\n\nIn the print statement, we print the (sum_end - sum_start) / count to get the average time of process per machine_id.\n\nWe also pass it through a DISTINCT to get only DISTINCT rows in the resultant table.\n\nWe also use an order by machine_id just to provide some order, but it is not required.\n\n\n# Code\n```\n\nselect DISTINCT e1.machine_id, round((e3.sum_end - e2.sum_start)/e4.cnt,3) as processing_time from (select DISTINCT machine_id from activity) e1, \n(select machine_id,round(sum(timestamp),3) as sum_start from activity where activity_type="start" group by machine_id) e2,\n(select machine_id,round(sum(timestamp),3) as sum_end from activity where activity_type="end" group by machine_id) e3,\n(select machine_id,count(*)/2 as cnt from activity group by machine_id) e4\nwhere e2.machine_id = e1.machine_id and e3.machine_id = e1.machine_id;\n\n\n\n```
['Database', 'MySQL']