我有一个名为duration_d
的列varchar2
,该表中的数据如下所示
duration_d
-----------
12:25
01:35
12:10
04:21
12:18
12:24
我在下面尝试了查询
SELECT SUM( to_date( duration_d, 'mi:ss' ))
FROM table
GROUP BY calling_number;
当我执行它后出现错误
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
任何人都可以告诉我如何计算它吗?
答案 0 :(得分:3)
要获得一天的分数,您可以使用:
SELECT SUM( TO_DATE( duration_d, 'MI:SS' ) - TO_DATE( '00:00', 'MI:SS' ) ) AS total
FROM your_table
结果如下:
TOTAL
------------------------------------------
0.0383449074074074074074074074074074074074
要将此转换为区间数据类型,您可以使用NUMTODSINTERVAL
:
SELECT NUMTODSINTERVAL(
SUM( TO_DATE( duration_d, 'MI:SS' ) - TO_DATE( '00:00', 'MI:SS' ) ),
'DAY'
) AS total
FROM your_table
结果如下:
TOTAL
-------------------
+00 00:55:13.000000
答案 1 :(得分:0)
请尝试以下:
with x as
(select sum((regexp_substr(YOUR_COLUMN, '[0-9]+', 1, 1)*60) +
regexp_substr(id, '[0-9]+', 1, 2)) seconds
from YOUR_TABLE)
SELECT
TO_CHAR(TRUNC(seconds/3600),'FM9900') || ':' ||
TO_CHAR(TRUNC(MOD(seconds,3600)/60),'FM00') || ':' ||
TO_CHAR(MOD(seconds,60),'FM00')
FROM x
仅在持续时间始终为[MI:SS]时才会起作用。
您也可以根据自己的要求添加群组。
将秒数转换为所需的持续时间格式Reference。
分组
with x as
(select calling_number,sum((regexp_substr(YOUR_COLUMN, '[0-9]+', 1, 1)*60) +
regexp_substr(id, '[0-9]+', 1, 2)) seconds
from YOUR_TABLE
group by calling_number)
SELECT calling_number,
TO_CHAR(TRUNC(seconds/3600),'FM9900') || ':' ||
TO_CHAR(TRUNC(MOD(seconds,3600)/60),'FM00') || ':' ||
TO_CHAR(MOD(seconds,60),'FM00')
FROM x
答案 2 :(得分:0)
使用SUBSTR
,to_char
,to_date
,NVL
,INSTR
,reverse
和SUM
的组合。< / p>
SELECT "calling_number",
to_char(to_date(SUM(NVL(SUBSTR("duration_d", 0, INSTR("duration_d", ':')-1), "duration_d"))*60 +
SUM(substr("duration_d", - instr(reverse("duration_d"), ':') + 1)),'sssss'),'hh24:mi:ss') AS SUM_DURATION_D
FROM yourtable
GROUP BY "calling_number"
输出
calling_number SUM_DURATION_D
1 00:26:10
2 00:29:03
答案 3 :(得分:-1)
正确拼写如下
SELECT SUM( TO_DATE( duration_d, 'mi:ss' ) )
FROM YOURTABLE Group By calling_number