2

我有一个查询,它从两个时间戳获取时间差,基本上看起来像这样

SELECT (time_a - time_b) FROM t_tabel WHERE t_some_id IN('1','2','3');

然后我得到输出

0 0:4:0.0
1 0:15:0.0
0 0:20:0.0

DAYS HOURS24H:MINUTES:SECONDS:MILLISECONDS

现在我只是想总结一下这些时间,我尝试了几件事,但没有任何工作。

SELECT sum(time_a - time_b) FROM t_tabel WHERE t_some_id IN('1','2','3');

给我:不一致的数据类型:预期 NUMBER 得到 INTERVAL DAY TO SECOND

SELECT sum(to_dsinterval(time_a - time_b)) FROM t_tabel
WHERE t_some_id IN('1','2','3');

给我:不一致的数据类型:预期 NUMBER 得到 INTERVAL DAY TO SECOND

SELECT sum(SELECT time_a - time_b FROM t_tabel
WHERE t_some_id IN('1','2','3')) from dual;

给我:缺少表达

SELECT sum(SELECT to_dsinterval(time_a - time_b) FROM t_tabel
WHERE t_some_id IN('1','2','3')) from dual;

给我:缺少表达

不知何故,它必须工作。

4

1 回答 1

4

The problem is that Oracle supplies us with these wonderful interval datatypes but doesn't provide the aggregate functions needed to sum or avg them!

Luckily Tom Kyte has written the necessary functions to implement this, see http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:5269744394385

I found this because I've used this in the past and found it again by Googling "oracle interval aggregation".

于 2013-05-07T15:36:53.387 回答