【问题标题】:Difference between timestamps captured in hourly timeblocks每小时时间块中捕获的时间戳之间的差异
【发布时间】:2014-06-16 11:31:57
【问题描述】:

我想捕捉两个时间戳之间的差异,输出以每 24 小时时间块的分钟数显示。

例如

数据- 开始 - 2014 年 5 月 1 日 23:56:00 ---- END-2014 年 5 月 2 日 01:04:00

输出所需的 E.G- 23 小时 -4 分钟,0 小时 - 60 分钟; 1 小时 - 4 分钟。

我已经设法通过为每个 24 小时时间段创建案例语句来计算与上述输出类似的结果,但并非所有时间都被捕获,即特别是对于 2 个日历日之间的一些差异。时间戳之间的最大差异为 23 小时和 59 分钟。

这是通过 oracle discoverer 进行的。 这里的任何建议将不胜感激。

【问题讨论】:

  • 是否有日期可以覆盖的最大天数(这些是日期而不是时间戳,对吗?)如果是 3,您想要 3 行还是 3 列?如果是 n 天,你明白你不能轻易把它放在列中吗?
  • 您好感谢您的及时回复。这些是时间戳。用于比较的任何两者之间的最大差异将小于 24 小时。这是在事务级别输出的,以行的形式向下运行,以 24 小时时间块为列。我希望这似乎是有道理的。
  • 我认为这是duplicate

标签: sql time oracle10g timestamp intervals


【解决方案1】:

如果它们是 TIMESTAMP,那么您可以使用 TIMESTAMP 算术导致 INTERVAL DAY TO SECOND 数据类型这一事实。给定超过 2 天的时间,TRUNC(<end_date>) 将提供中间日期的午夜日期。

SQL> with the_data as (
  2   select to_timestamp('2014-05-01 23:56:00','yyyy-mm-dd hh24:mi:ss') as s
  3        , to_timestamp('2014-05-02 01:04:00','yyyy-mm-dd hh24:mi:ss') as e
  4     from dual
  5    union
  6   select to_timestamp('2014-05-01 23:56:00','yyyy-mm-dd hh24:mi:ss') as s
  7        , to_timestamp('2014-05-01 23:57:00','yyyy-mm-dd hh24:mi:ss') as e
  8     from dual
  9          )
 10  select case when trunc(e) = trunc(s) then e - s
 11              else trunc(e) - s
 12         end as day1
 13       , case when trunc(e) = trunc(s) then null
 14              else e - trunc(e)
 15         end as day2
 16    from the_data;

DAY1                           DAY2
------------------------------ ------------------------------
+000000000 00:01:00.000000000
+000000000 00:04:00.000000000  +000000000 01:04:00.000000000

您需要 CASE 语句,因为您需要做不同的事情,具体取决于您的时间戳是否在同一天。如果您的数据类型实际上是 DATE,那么您最终会以天数作为返回的数据类型

SQL> with the_data as (
  2   select to_date('2014-05-01 23:56:00','yyyy-mm-dd hh24:mi:ss') as s
  3        , to_date('2014-05-02 01:04:00','yyyy-mm-dd hh24:mi:ss') as e
  4     from dual
  5    union
  6   select to_date('2014-05-01 23:56:00','yyyy-mm-dd hh24:mi:ss') as s
  7        , to_date('2014-05-01 23:57:00','yyyy-mm-dd hh24:mi:ss') as e
  8     from dual
  9          )
 10  select case when trunc(e) = trunc(s) then e - s
 11              else trunc(e) - s
 12         end as day1
 13       , case when trunc(e) = trunc(s) then null
 14              else e - trunc(e)
 15         end as day2
 16    from the_data;

      DAY1       DAY2
---------- ----------
0.00069444
0.00277777 0.04444444

然后,您可以将此分数添加到当前 SYSTIMESTAMP 中,然后再将其删除,从而将其转换为小时数和分钟数。

select systimestamp + case when trunc(e) = trunc(s) then e - s
                           else trunc(e) - s
                      end 
       - systimestamp as day1
     , systimestamp + case when trunc(e) = trunc(s) then null
                           else e - trunc(e)
                      end 
       - systimestamp
  from the_data;

之所以有效,是因为您正在创建超过当前时间戳的正确分钟数,然后再次将其删除。但是,由于 TIMESTAMP 算法会产生 INTERVAL DAY TO SECOND,因此您已将其转换为正确的格式。

最好以最适合的数据类型存储数据。如果你想format this differently,你可以在从数据库中提取时这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    相关资源
    最近更新 更多