题目二:数据统计。有一张任务处理日志表,记录了每个任务的流转日志,如系统在某个时点分配给A用户一个任务,A用户在某个时点流转到B用户,B用户在某个时点流转到处理完成节点。按问题统计出每个用户处理任务的时长。设计一张表,并且写出sql,计算出每个问题在每个用户下的处理时长。
Table_name : project_circulation
按项目归纳。
Select pro_id, user_id, sec_to_time(sum(timestampdiff(second, start_time, end_time))) as time from project_circulation group by user_id,pro_id;
创建Mysql数据表:
获取每个用户完成每个工程的结束时间:
(Select a.pro_id, a.user_id, a.start_time, b.start_time as end_time
from project_circulation a, project_circulation b
where a.pro_id = b.pro_id and a.user_id = b.post_user_id and b.start_time > a.start_time
and b.start_time = (select min(start_time) from project_circulation c where a.pro_id = c.pro_id and a.user_id = c.post_user_id and c.start_time > a.start_time)
)
运行结果图:
完整代码--获取每个用户完成每个工程所需的时间:
Select pro_id, user_id, sum(datediff(end_time, start_time)) as time
from (Select a.pro_id, a.user_id, a.start_time, b.start_time as end_time
from project_circulation a, project_circulation b
where a.pro_id = b.pro_id and a.user_id = b.post_user_id and b.start_time > a.start_time and
b.start_time = (select min(start_time) from project_circulation c where a.pro_id = c.pro_id and a.user_id = c.post_user_id and c.start_time > a.start_time))as c
group by user_id,pro_id;
运行结果图:
补充知识点:
-- 获取多列最大最小值
SELECT GREATEST(1,2,3,4);结果:4
SELECT LEAST(1,2,3,4);结果:1
-- 获取一列中最大最小值
SELECT MAX(col1);
SELECT MIN(col1);