2017-03-03 7 views
3

この表はOracleで、電子メールと時刻はvarcharです。Oracleの合計時間を計算する

Email    Time 
---------------- ----------- 
[email protected]  00:10:40 
[email protected]  00:40:10 
[email protected]  01:10:20 
[email protected]  00:43:40 
[email protected]  00:42:40 
[email protected] 00:30:40 
[email protected] 00:54:10 
[email protected] 00:47:40 
[email protected]  00:50:40 
[email protected]  01:05:40 
[email protected]  00:45:40 
[email protected]  00:51:40 
[email protected]  00:36:40 

私は電子メールで時間とグループの合計を取得したいと思います。 Oracleでは可能ですか?

UPDATES

時間が期間です。

+0

である私が正しい、いない時間thatsの期間を想定していますか? – tbone

+0

@tboneはい持続時間 –

+0

合計時間は11gで、とにかく単純ではありません。[this post](http://stackoverflow.com/questions/16812762/convert-varchar2-into-number/16816494#16816494) – tbone

答えて

4

使用このクエリは

SQL> WITH table_(Email, Time) as (
    2  select '[email protected]', '00:10:40' from dual union all 
    3  select '[email protected]', '00:40:10' from dual union all 
    4  select '[email protected]', '01:10:20' from dual union all 
    5  select '[email protected]', '00:43:40' from dual union all 
    6  select '[email protected]', '00:42:40' from dual union all 
    7  select '[email protected]', '00:30:40' from dual union all 
    8  select '[email protected]', '00:54:10' from dual union all 
    9  select '[email protected]', '00:47:40' from dual union all 
10  select '[email protected]', '00:50:40' from dual union all 
11  select '[email protected]', '01:05:40' from dual union all 
12  select '[email protected]', '00:45:40' from dual union all 
13  select '[email protected]', '00:51:40' from dual union all 
14  select '[email protected]', '00:36:40' from dual) 
15 --------------------- 
16 -- End if sample data 
17 --------------------- 
18 SELECT email, numtodsinterval(sum(SUBSTR(TIME, 1, 2)*3600 + SUBSTR(TIME, 4, 2)*60 + SUBSTR(TIME, 7, 2)), 'SECOND') total_duration 
19 FROM table_ 
20 GROUP BY email; 

出力:

EMAIL   TOTAL_DURATION 
---------------- -------------------------------------------------------------------------------- 
[email protected] +000000000 02:12:30.000000000 
[email protected] +000000000 04:10:20.000000000 
[email protected] +000000000 03:27:30.000000000 

だから、あなたのクエリが

SELECT email, numtodsinterval(sum(SUBSTR(TIME, 1, 2)*3600 + SUBSTR(TIME, 4, 2)*60 + SUBSTR(TIME, 7, 2)), 'SECOND') total_duration 
    FROM table_ 
GROUP BY email; 
+0

ありがとう!それは完璧だ :) –