【问题标题】:Calculate sum time in Oracle在 Oracle 中计算总时间
【发布时间】:2017-07-23 15:39:11
【问题描述】:

我在Oracle中有这张表,电子邮件和时间都是varchar。

Email               Time
----------------    -----------
paolo@gmail.com     00:10:40
paolo@gmail.com     00:40:10
paolo@gmail.com     01:10:20
paolo@gmail.com     00:43:40
paolo@gmail.com     00:42:40
chiara@gmail.com    00:30:40
chiara@gmail.com    00:54:10
chiara@gmail.com    00:47:40
simo@gmail.com      00:50:40
simo@gmail.com      01:05:40
simo@gmail.com      00:45:40
simo@gmail.com      00:51:40
simo@gmail.com      00:36:40

我想通过电子邮件获取时间和组的总和。甲骨文可以吗?

更新

时间就是持续时间。

【问题讨论】:

  • 我假设那是持续时间而不是时间,对吗?
  • @tbone 是持续时间
  • 对持续时间求和(无论如何在 11g 中)并不简单,请参阅 this post
  • 恐怕您可能不得不为此使用 PL/SQL...

标签: oracle time sum timestamp


【解决方案1】:

使用这个查询

SQL> WITH table_(Email, Time) as (
  2      select 'paolo@gmail.com', '00:10:40' from dual union all
  3      select 'paolo@gmail.com', '00:40:10' from dual union all
  4      select 'paolo@gmail.com', '01:10:20' from dual union all
  5      select 'paolo@gmail.com', '00:43:40' from dual union all
  6      select 'paolo@gmail.com', '00:42:40' from dual union all
  7      select 'chiara@gmail.com', '00:30:40' from dual union all
  8      select 'chiara@gmail.com', '00:54:10' from dual union all
  9      select 'chiara@gmail.com', '00:47:40' from dual union all
 10      select 'simo@gmail.com', '00:50:40' from dual union all
 11      select 'simo@gmail.com', '01:05:40' from dual union all
 12      select 'simo@gmail.com', '00:45:40' from dual union all
 13      select 'simo@gmail.com', '00:51:40' from dual union all
 14      select 'simo@gmail.com', '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
---------------- --------------------------------------------------------------------------------
chiara@gmail.com +000000000 02:12:30.000000000
simo@gmail.com   +000000000 04:10:20.000000000
paolo@gmail.com  +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;

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多