每个user_id的时间总和

时间:2017-02-13 11:04:23

标签: mysql sql

好的,我有一张表格,其中每个user_id都有operation_timestamp。要检查每个user_id操作所花费的时间,我使用查询max(operation_timestamp)-min(operation_timestamp),然后我将其转换为小数,所以让我说得到结果:

  SHIFT   USER ID       MIN         MAX       MAX-MIN    DECIMAL
  shift1  user_1     08:19:42    09:55:20    01:35:37     1.59
  shift2  user_2     10:04:27    10:28:22    00:23:54     0.40
  shift2  user_3     10:44:07    10:55:58    00:01:51     0.04
  shift2  user_4     06:25:33    10:51:52    04:26:19     4.44

现在我的问题:如何以小数形式计算出一个时间总和,这样我就可以为所有用户花费总时间?这样的事情:

 SHIFT     TOTAL_DECIMAL
  shift1        1.59
  shift2        4.88

我尝试了同样的查询,但没有group by user_id函数,但是它在不查看单独的user_id的情况下计算max和min,所以假设它会将shift2总计算为06:25:33 (user_4) to 10:55:58 (user_3)以小数形式给出4.45的结果。

这是我使用的查询没有成功:

select
case 
when SUBSTR(a.operation_ts,12,13) between '00:00:00.000' and '05:59:59.000' then 'Nights'
when SUBSTR(a.operation_ts,12,13) between '06:00:00.000' and '13:59:59.000' then 'Days'
when SUBSTR(a.operation_ts,12,13) between '14:00:00.000' and '21:59:59.000' then 'Lates'
when SUBSTR(a.operation_ts,12,13) between '22:00:00.000' and '23:59:59.000' then 'Nights'
else 'other'
end as shift,
a.userid,
substr(min(a.operation_ts),11,9),
substr(max(a.operation_ts),11,9),
substr((max(a.operation_ts)-min(a.operation_ts)),10,9) as time_on_go,
round(((substr((max(a.operation_ts)-min(a.operation_ts)),11,2))*3600+(substr((max(a.operation_ts)-min(a.operation_ts)),14,2))*60+(substr((max(a.operation_ts)-min(a.operation_ts)),17,2)))/3600,2) time_decimal
from dc_sys_common:user_operation a
where a.activity_code = 1012
and date(a.operation_ts) between today and today+1
and SUBSTR(a.operation_ts,12,9) between '00:00:00' and '21:59:59'
group by userid, shift
having max(a.operation_ts)-min(a.operation_ts)>'0 00:00:01.000'
order by shift asc

2 个答案:

答案 0 :(得分:1)

在嵌套的第一个查询上进行GROUP BY SHIFT:

func testTimer(){
  print("Timer called")
}

答案 1 :(得分:0)

我认为您需要两个级别的聚合来完成您的工作,一个级别为user_idshift,第二级级别为shift

select shift, 
from (select case when SUBSTR(a.operation_ts,12,13) between '00:00:00.000' and '05:59:59.000' then 'Nights'
                  when SUBSTR(a.operation_ts,12,13) between '06:00:00.000' and '13:59:59.000' then 'Days'
                  when SUBSTR(a.operation_ts,12,13) between '14:00:00.000' and '21:59:59.000' then 'Lates'
                  when SUBSTR(a.operation_ts,12,13) between '22:00:00.000' and '23:59:59.000' then 'Nights'
                  else 'other'
             end) as shift,
             a.userid,
             round(((substr((max(a.operation_ts)-min(a.operation_ts)),11,2))*3600+(substr((max(a.operation_ts)-min(a.operation_ts)),14,2))*60+(substr((max(a.operation_ts)-min(a.operation_ts)),17,2)))/3600,2
                  ) time_decimal
      from dc_sys_common:user_operation a
      where a.activity_code = 1012 and
            date(a.operation_ts) between today and today+1 and
            SUBSTR(a.operation_ts,12,9) between '00:00:00' and '21:59:59'
      group by userid, shift
      having max(a.operation_ts)-min(a.operation_ts)>'0 00:00:01.000'
     ) us
group by shift asc;