2017-02-13 13 views
0

だから、それぞれuser_idoperation_timestampというテーブルがあります。私は結果を取得しましょう、私は小数に変換し、クエリmax(operation_timestamp)-min(operation_timestamp)を使用し、各user_idのためにかかった時間、アクションを確認するには:各user_idの合計時間

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 

今、私の質問:私は小数で時間の合計をうまくする方法すべてのユーザーのアクティビティに費やす時間はどれくらいですか?このような何か:私はそれは、その後、別のuser_idを見ずに、最大と最小を計算しているが、group by user_id機能なしで同じクエリを試してみましたので、それが結果を与えるだろう06:25:33 (user_4) to 10:55:58 (user_3)としてshift2合計を計算します言わせている

SHIFT  TOTAL_DECIMAL 
    shift1  1.59 
    shift2  4.88 

小数点では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 

答えて

1

は、入れ子にされて、あなたの最初のクエリにSHIFT、GROUP BYを行います

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

試しましたが、エラーが発生します: '集約内に集約はありません。 ' – user7557185

+0

これはうまくいくはずです。質問を編集し、実際のクエリを追加してください。 –

0

を私はあなたが何のために集約2つのレベルが必要だと思います1つは、user_idshiftのレベルで、もう1つはレベル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; 
+0

これはうまくいきません。 – user7557185