简体   繁体   English

SAP HANA Cloud 上的时差计算

[英]Time Difference Calculation on SAP HANA Cloud

I want to calculate the difference between Occurence_TimeStamp and Response_TimeStamp using a calculated column.我想使用计算列计算 Occurence_TimeStamp 和 Response_TimeStamp 之间的差异。

I have used SECONDS_BETWEEN("Occurence_TimeStamp", "Response_TimeStamp") to convert the difference in seconds.我使用 SECONDS_BETWEEN("Occurence_TimeStamp", "Response_TimeStamp") 以秒为单位转换差异。

The challenge I am facing now is that for Response Time that occurs in the following day, SAP HANA returns a negative value.我现在面临的挑战是,对于第二天发生的响应时间,SAP HANA 返回一个负值。

Eg 00:32:00 - 23:41 should return 3,060 seconds but HANA returned -83340.例如 00:32:00 - 23:41 应该返回 3,060 秒,但 HANA 返回 -83340。 My solution is to add 86,400 (60x60x24) to every time difference affected in order to resolve the issue.我的解决方案是为每个受影响的时差添加 86,400 (60x60x24) 以解决问题。

Here is the code I'm using (TimeDrill = Response_Time - Occurence_Time)这是我正在使用的代码(TimeDrill = Response_Time - Occurence_Time)

CASE "TimeDrill" WHEN "TimeDrill" > 0 then "TimeDrill" ELSE "TimeDrill"+86400 END CASE "TimeDrill" WHEN "TimeDrill" > 0 然后 "TimeDrill" ELSE "TimeDrill"+86400 END

Error message below:下面的错误信息:

在此处输入图像描述

Thanks谢谢

It looks like the timestamps in your scenario are, in fact time of the day timestamps (eg not 2020/12/01 08:48:12 but just 08:48:12).看起来您场景中的时间戳实际上是一天中的时间戳(例如,不是2020/12/01 08:48:12,而只是 08:48:12)。

In that case, the SECONDS_BETWEEN function needs to convert the time values into HANA timestamps (date + time) first.在这种情况下, SECONDS_BETWEEN function 需要先将时间值转换为 HANA 时间戳(日期 + 时间)。

With no date provided, this yields the 0001-01-01 as the date component.如果没有提供日期,这会产生 0001-01-01 作为日期组件。

Looking at that in code gives us this:在代码中查看它给了我们这个:

SELECT 
      to_timestamp ('00:32:00', 'HH24:MI:SS') "early_TS"
    , TO_TIMESTAMP ('23:41:00', 'HH24:MI:SS') "late_TS"
    , seconds_between ( to_timestamp ('00:32:00', 'HH24:MI:SS'),  TO_TIMESTAMP ('23:41:00', 'HH24:MI:SS')) "early_first"
    , seconds_between ( TO_TIMESTAMP ('23:41:00', 'HH24:MI:SS'),  to_timestamp ('00:32:00', 'HH24:MI:SS')) "late_first"
    
 FROM 
    DUMMY 

early_TS           |late_TS            |early_first|late_first|
-------------------|-------------------|-----------|----------|
0001-01-01 00:32:00|0001-01-01 23:41:00|      83340|    -83340|

Important to note here is that the difference between those two timestamps has the same absolute number of seconds.这里需要注意的重要一点是,这两个时间戳之间的差异具有相同的绝对秒数。 The sign simply indicates the "direction" of the difference, eg which of the two timestamps was larger than the other.该符号仅指示差异的“方向”,例如两个时间戳中的哪个大于另一个。

For your calculation you could just use the ABS() (absolute) function to always get the value without the sign.对于您的计算,您可以只使用ABS() (绝对) function 来始终获得不带符号的值。

---- Update based on the comments and re-reading the original question ----根据评论更新并重新阅读原始问题

The underlying assumption seems to be that the RESPONSE_TS always is a timestamp after the OCCURRENCE_TS .基本假设似乎是RESPONSE_TS始终OCCURRENCE_TS之后的时间戳。 This makes sense in a cause-effect-kind of model.这在 model 的因果类型中是有意义的。 Eg something occurs and then there is a response to that occurrence.例如,某事发生,然后对该事件有响应。

In that case, a time of day timestamp of a response that is seemingly earlier than the occurrence has to be interpreted to have happened on the following day.在这种情况下,似乎于事件发生的响应的时间时间戳必须被解释为在第二天发生。 In the OP's example the response at 00:32:00 is supposed to happen 3060 seconds after the occurrence at 23:41:00 .在 OP 的示例中, 23:41:00的响应应该在00:32:00发生3060 秒发生。

To do that, we need a case-differentiation in the SQL statement as the actual time of day data does not contain this information.为此,我们需要在 SQL 语句中区分大小写,因为实际时间数据不包含此信息。

WITH event_ts AS 
    (SELECT   
          TO_TIMESTAMP ('23:41:00', 'HH24:MI:SS') "OCCURRENCE_TS"
        , to_timestamp ('00:32:00', 'HH24:MI:SS') "RESPONSE_TS" 
    FROM 
        DUMMY)
SELECT 
      "OCCURRENCE_TS" -- this is considered to ALWAYS happen BEFORE the RESPONSE
    , "RESPONSE_TS"   -- this is considered to ALWAYS happen AFTER the OCCURRENCE
   , CASE 
        WHEN ("RESPONSE_TS" > "OCCURRENCE_TS") THEN  -- "RESPONSE_AFTER_BUT_SAME_DAY"
            seconds_between ("OCCURRENCE_TS", "RESPONSE_TS")
        ELSE -- "RESPONSE_AFTER_BUT_NEXT_DAY"
            seconds_between ("OCCURRENCE_TS", ADD_DAYS("RESPONSE_TS", 1))
     END "SECONDS_BETWEEN_OCCURRENCE_AND_RESPONSE"
FROM 
    event_ts;
    

In this solution, whenever the response is seemingly happening earlier than the occurrence, one day is added to the RESPONSE_TS to shift it to the following day.在此解决方案中,每当响应似乎早于发生时,就会将一天添加到RESPONSE_TS以将其转移到下一天。

OCCURRENCE_TS      |RESPONSE_TS        |SECONDS_BETWEEN_OCCURRENCE_AND_RESPONSE|
-------------------|-------------------|---------------------------------------|
0001-01-01 23:41:00|0001-01-01 00:32:00|                                   3060|

It's important to understand that this approach re-computes information based on the rule about what it means when RESPONSE_TS is smaller than OCCURRENCE_TS .重要的是要理解,这种方法会根据RESPONSE_TS小于OCCURRENCE_TS时的含义规则重新计算信息。 As this is not necessarily obvious and may be used in several places across the read data model, it may be worthwhile putting this into a user-defined scalar function.由于这不一定很明显,并且可能在读取数据 model 的多个地方使用,因此将其放入用户定义的标量 function 可能是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM