在我的查询中,我想取两个日期之差。
(SELECT
(SELECT estimated_arrival AS val2
FROM shipment_stop
WHERE stop_num =5
AND shipment_gid ='IFFCO/LOGISTICS.LG171009102'
) -
(SELECT estimated_arrival AS val1
FROM shipment_stop
WHERE stop_num = 1
AND shipment_gid = 'IFFCO/LOGISTICS.LG171009102'
)
FROM dual
)
因此,基本上假设Val1的价格为2017年10月10日,Val2的价格为2017年10月13日,因此,如果我执行Val2-Val1,则它应该显示3天,但显示为3.237856747474747474,看起来它正在使用时间也。有办法解决吗? 注意:estimated_arrival是日期列
答案 0 :(得分:1)
您要用于删除时间部分。目前尚不清楚您要在减法之前还是之后进行此操作。基本上,您认为一天的上午11:00是第二天的上午10:00是0天还是1天?
在这种情况下,我猜您想要1天。如果是这样,请使用trunc()
在之前减去:
SELECT ( (SELECT trunc(estimated_arrival) AS val2
FROM shipment_stop
WHERE stop_num = 5 AND shipment_gid = 'IFFCO/LOGISTICS.LG171009102'
) -
(SELECT trunc(estimated_arrival) AS val1
FROM shipment_stop
WHERE stop_num = 1 AND shipment_gid = 'IFFCO/LOGISTICS.LG171009102'
)
) as diff_days
FROM dual;
答案 1 :(得分:1)
除了使用trunc()
将DATE列的时间部分设置为午夜(00:00:00)的建议外,您不需要两个select语句即可计算出差异。
例如假设shipping_gid和stop_num是唯一的,则可以执行以下操作:
SELECT MAX(CASE WHEN stop_num = 5 THEN TRUNC(estimated_arrival) END)
- MAX(CASE WHEN stop_num = 1 THEN TRUNC(estimated_arrival) END) date_difference
FROM shipment_stop
WHERE stop_num IN (1, 5)
AND shipment_gid = 'IFFCO/LOGISTICS.LG171009102';