Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要获取Sql中两列的差异吗_Sql_Oracle - Fatal编程技术网

要获取Sql中两列的差异吗

要获取Sql中两列的差异吗,sql,oracle,Sql,Oracle,我想计算一个表中各列的差异。我们的列名为Planned_date,所以现在我想计算这两列的差异 A = Planned_Date of stop1 - Planned_Date of stop5 那么我如何编写查询来获取 下面是我编写的示例查询,但不知何故它对我不起作用 select (select planned_arrival as val1 from shipment_stop where stop_num = 1 and shi

我想计算一个表中各列的差异。我们的列名为Planned_date,所以现在我想计算这两列的差异

A = Planned_Date of stop1 - Planned_Date of stop5
那么我如何编写查询来获取 下面是我编写的示例查询,但不知何故它对我不起作用

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')
请帮忙。

试试这个-

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;

您的查询应使用from子句:

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

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';

到目前为止,您尝试过什么?我尝试过使用-编写查询,如下所示。选择“从发货站选择计划到货作为val1,其中stop_num=1,发货站_gid=”IFFCO/LOGISTICS.L171009358'-从发货站选择计划到货作为val2,其中stop_num=5,发货站_gid=”IFFCO/LOGISTICS.L71009358'-from我的问题是如何用最好的方法得到A的值。我们可以在这里使用交叉连接吗?如果你想得到的不仅仅是这一个差异,你可以将Shipping_stop本身与Shipping_gid和a.stop_num.stop_num进行内部连接。这将告诉你在同一批货件上从一站到另一站以及从任何一站到任何其他站的所有时间。计划到达是一个日期,因此如果我使用-它给出的结果是3.03745637474,这不是我的预期。因此,基本上假设Val1的时间是2017年10月13日,Val2的时间是2017年10月10日,因此如果我在做Val2-Val1,那么它应该显示3天,但看起来它也在使用时间。有没有办法处理这个问题?要删除时间元素,只需在日期列上使用TRUNC。
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';