簡體   English   中英

每個user_id的時間總和

[英]Sum of time for each user_id

好的,所以我有一個表,其中每個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函數進行group by user_id但是它隨后在不查看單獨的user_id的情況下計算了最大值和最小值,因此,可以說它會將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

使嵌套的第一個查詢的GROUP BY SHIFT:

SELECT `SHIFT`, SUM(`DECIMAL`)
FROM
(
    -- your query here
)
GROUP BY `SHIFT`

我認為您需要進行兩個級別的聚合,一個級別在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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM