Teradata 时间戳减法,以分钟为单位

Teradata timestamp subtraction with difference in minutes

select StartDatetime,EndDatetime,
(TRIM((COALESCE(CAST((CAST(EndDatetime AS TIME) - CAST(StartDatetime AS TIME) MINUTE(4)) AS INT),'')))) as TimeDiff
From Table1

EndDatetimeStartDatetimeTimestamp(6) 类型的列。上面的查询给出了以分钟为单位的时间戳之间的差异。如果两个时间戳都出现在同一天,它就可以工作。但是当 EndDatetime 的值是明天 wrt 到 StartDatetime 时,返回的 TimeDiff 值是负值。使用绝对函数不是最佳解决方案,因为 TimeDiff 数字本身不准确。

如何以分钟为单位精确获取时间戳差异?

这是使用提取的一种方法:

Select 
    StartDatetime,EndDatetime
    ,(CAST((CAST(EndDatetime AS DATE)- CAST(StartDatetime AS DATE)) AS DECIMAL(18,6)) * 60*24)
    + ((EXTRACT(HOUR FROM EndDatetime) - EXTRACT(HOUR FROM StartDatetime))* 60)
    + ((EXTRACT(MINUTE FROM EndDatetime) - EXTRACT(MINUTE FROM StartDatetime)))
    + ((EXTRACT(SECOND FROM EndDatetime) - EXTRACT(SECOND FROM StartDatetime))/60)
AS "Difference in Minutes"
from Table1