我有一个包含开始时间和完成时间的表,我想获取此数据的经过时间,我已经根据它进行了查询。但它不能正常工作。
开始时间:2020-09-04 15:21:34.703333 完成时间:2020-09-04 15:28:32:28
经过时间:00:06:57
如果开始和完成时间超过 24 小时
经过时间:1.00:06:57
SELECT
starttime,
completiontime,
CONCAT (
CASE
WHEN GETDATBETWEEN(completiontime,starttime) > 0 THEN CONCAT(GETDAYBETWEEN(COMPLETIONTIME, STARTTIME),'.')
WHEN GETDATBETWEEN(completiontime,starttime) < 0 THEN ''
ELSE ''
END
CASE
WHEN SUBTRACT(GETHOUR(completiontime), GETHOUR(starttime)) > 0 THEN CONCAT(SUBTRACT(GETHOUR(completiontime), GETHOUR(starttime)),':')
WHEN SUBTRACT(GETHOUR(completiontime), GETHOUR(starttime)) < 0 THEN CONCAT(SUBTRACT(GETHOUR(completiontime), GETHOUR(starttime)) + 24,':')
ELSE '0:'
END
CASE
WHEN SUBTRACT(GETMINUTE(completiontime), GETMINUTE(starttime)) > 0 THEN CONCAT(SUBTRACT(GETMINUTE(completiontime), GETMINUTE(starttime)),':')
WHEN SUBTRACT(GETMINUTE(completiontime), GETMINUTE(starttime)) < 0 THEN CONCAT(SUBTRACT(GETMINUTE(completiontime), GETMINUTE(starttime)) + 60,':')
ELSE '0:'
END
CASE
WHEN SUBTRACT(GETSECOND(completiontime), GETSECOND(starttime)) > 0 THEN CONCAT(SUBTRACT(GETSECOND(completiontime), GETSECOND(starttime)),':')
WHEN SUBTRACT(GETSECOND(completiontime), GETSECOND(starttime)) < 0 THEN CONCAT(SUBTRACT(GETSECOND(completiontime), GETSECOND(starttime)) + 60,':')
ELSE '0'
END ) AS elapsed_time
FROM sample;
谢谢