我在表t_mst中具有以下格式a_time=01-06-2020 15:28:06
和b_time=01-06-2020 15:39:00
的日期我想在几分钟之内将给定值的差异转换为查询
select (extract( day from diff )*24*60*60 +
extract( hour from diff )*60*60 +
extract( minute from diff )*60 +
extract( second from diff ))/60 total_Minutes
from(select b_time - a_time diff
from t_mst Where a_time between (trunc(sysdate, 'mm')) and (sysdate))
但出现错误“提取源无效的提取字段”任何想法将不胜感激。
您显示的错误表示b_time - a_time
有效。这意味着它们是date
(或也许是timestamp
)数据类型;很好。
然后,它们必须为date
;如果它们是timestamp
,则差异将是interval
数据类型,并且您确实可以从interval
中提取时间元素。因此,错误消息表明a_time
和b_time
的数据类型的确是date
。
现在:在Oracle中,两个日期之间的差为number
(以天为单位);您无法从数字中提取时间元素。相反,请考虑到差异已经是几天(包括小数部分)了;要将其转换为分钟,只需要将其乘以1440-然后根据需要舍入结果。
您已标记了3个数据库,并且代码适用于postgreSQL。我想你的意思是,postgreSQL:
select (extract( epoch from age(a_time, b_time)/60) total_Minutes
from t_mst
--Where a_time between (trunc(sysdate, 'mm')) and (sysdate))
如果您需要将其强制转换为int:
select (extract( epoch from age(a_time, b_time)/60)::int total_Minutes
from t_mst
PS:不确定您的意思是plSQL或PL \ pgSQL。
如果b_time
和a_time
数据类型为DATE
,则只需运行
select (b_time - a_time) * 24*60 AS total_Minutes
from t_mst
Where a_time between trunc(sysdate, 'mm') and sysdate
[如果它们是TIMESTAMP
数据类型,则必须提取值,例如像这样:
select
EXTRACT(DAY FROM (b_time - a_time))*24*60
+ EXTRACT(HOUR FROM (b_time - a_time))*60
+ EXTRACT(MINUTE FROM (b_time - a_time))
+ EXTRACT(SECOND FROM (b_time - a_time))/60
AS total_Minutes
from t_mst
Where a_time between trunc(sysdate, 'mm') and sysdate