解题思路:
先按照机器machine_id进行分组,然后求每台机器内的平均运行时间
使用case—when函数修改,如果状态为start,则将其时间值变为负数,如果状态为end,则保持不变
使用sum函数对同一台机器内所有进程的运行时间进行求和。
使用上一步的累加值除以同一台机器内进程的个数得到平均值
SQL代码:
select machine_id,
round(sum(case when activity_type='start'then -timestamp
when activity_type='end'then timestamp else null end)
/count(DISTINCT process_id)
,3) processing_time
from activity
group by machine_id
题解:SQL题解