1

我想取一个表的列的差异。我们的列名是 Planned_date 所以现在我想取这两列的差异

A = Planned_Date of stop1 - Planned_Date of stop5

所以我如何编写查询来获取下面 A 的值是我编写的示例查询,但不知何故它对我不起作用。

select (select planned_arrival as val1 
        from shipment_stop 
         where stop_num = 1 
         and shipment_gid = 'IFFCO/LOGISTICS.L171009358')
       -
      (select planned_arrival as val2 
       from shipment_stop 
       where stop_num = 5 
       and shipment_gid = 'IFFCO/LOGISTICS.L171009358')

请帮忙。

4

2 回答 2

2

尝试这个 -

SELECT
    s1.planned_arrival - s2.planned_arrival AS val
FROM
    shipment_stop s1,
    shipment_stop s2
WHERE
    s1.stop_num = 1
    AND s2.stop_num = 5
    AND s1.shipment_gid = 'IFFCO/LOGISTICS.L171009358'
    AND s1.shipment_gid = s2.shipment_gid;
于 2018-06-25T18:31:04.670 回答
1

您的查询应使用from子句:

select (select planned_arrival as val1 
        from shipment_stop 
         where stop_num = 1 
         and shipment_gid = 'IFFCO/LOGISTICS.L171009358')
       -
      (select planned_arrival as val2 
       from shipment_stop 
       where stop_num = 5 
       and shipment_gid = 'IFFCO/LOGISTICS.L171009358')
from dual;

就个人而言,我会使用条件聚合来写这个:

select (max(case when stop_num = 1 then planned_arrival end) -
        max(case when stop_num = 5 then planned_arrival end)
       )
from shipment_stop 
where stop_num in (1, 5) and 
      shipment_gid = 'IFFCO/LOGISTICS.L171009358';
于 2018-06-25T19:22:22.720 回答