我正在处理一项要求,我必须计算两个由日期得出的日期时间列之间的持续时间。结果应该以小时为单位。
WildcardEventDispatcher
有什么建议吗?
谢谢!
答案 0 :(得分:0)
您可以尝试通过TIMESTAMP
进行计算,并使用HOUR(4)来获得hour
SELECT cast((DateField1 + TimeField1)as TIMESTAMP) - cast((DateField2 + TimeField2)as TIMESTAMP) HOUR(4)
答案 1 :(得分:0)
您所需的输出(DateField1 + TimeField1)-(DateField2 + TimeField2)
相当于(DateField1-DateField2)+(TimeField1-TimeField2)
如果您只希望时间戳差异以小时为单位(忽略分钟和秒)
select ((DateField1 - DateField2) HOUR(4))+( (TimeField1 - TimeField2) HOUR(4)) required_output
from dateTable;
如果您要考虑时,分和秒,则需要时间戳差异
SELECT
(DateField1 - DateField2) HOUR(4) to SECOND(4) t1,
(TimeField1 - TimeField2) HOUR(4) to SECOND(4) t2,
EXTRACT(HOUR from t1)+EXTRACT(MINUTE from t1)/60.0 +EXTRACT(SECOND from t1)/(60.0*60.0) t1_HOUR,
EXTRACT(HOUR from t2)+EXTRACT(MINUTE from t2)/60.0 +EXTRACT(SECOND from t2)/(60.0*60.0) t2_HOUR,
t1_HOUR+t2_HOUR required_output
FROM dateTable;
注意:以上逻辑可以处理两个时间戳之间最大9999小时的时差,如果要处理更大的值,则可以考虑将Day(4)添加到Second(4)并相应地更新逻辑
表和数据,如果你想玩
CREATE volatile table dateTable( DateField1 TIMESTAMP(0),DateField2 TIMESTAMP(0),TimeField1 TIMESTAMP(0),TimeField2 TIMESTAMP(0))on commit preserve rows;
INSERT INTO dateTable VALUES (current_date,current_date-1,current_date-2,current_date-3)