没有子查询就无法编写oracle / SQL Query日志transions数据

时间:2018-08-02 21:04:01

标签: sql oracle oracle11g

我在同一张表中有2种不同的翻译,我如何将它们加入一行而不使用子查询,然后再加入它们,因为如果我为每个翻译案例创建子查询并将它们合并,则我试图合并多个翻译案例问题。真的很需要您的帮助

当前视图

ORDER         ACTION    DATE_TIME_CREATED
D8ZH4RJSB-689   PICK    8/1/2018 0:35
D8ZH4RJSB-689   SHIP    8/1/2018 12:03

要求的输出

ORDER          PICK_DATE_TIME_CREATED    SHIP_DATE_TIME_CREATED
D8ZH4RJSB-689     8/1/2018 0:35            8/1/2018 12:03

这是我当前的代码

SELECT "ORDER", --,"ACTION",ship_complete_time as date_time_created
CASE WHEN "ACTION" = 'SHIP'
THEN ship_complete_time
END AS SHIP_COM_TIME,
CASE WHEN "ACTION" = 'PICK'
THEN ship_complete_time
END AS pick_time  
FROM TRANSATIONS
WHERE "ORDER" = 'D8ZH4RJSB-689'

当前输出

ORDER           SHIP_COM_TIME              PICK_TIME
D8ZH4RJSB-689        (null)              2018-08-01 00:35
D8ZH4RJSB-689   2018-08-01 12:03          (null)

2 个答案:

答案 0 :(得分:3)

使用聚合:

SELECT "ORDER", --,"ACTION",ship_complete_time as date_time_created
       MAX(CASE WHEN "ACTION" = 'SHIP' THEN ship_complete_time END) AS SHIP_COM_TIME,
       MAX(CASE WHEN "ACTION" = 'PICK' THEN ship_complete_time END) AS pick_time  
FROM TRANSATIONS
WHERE "ORDER" = 'D8ZH4RJSB-689'
GROUP BY "ORDER";

答案 1 :(得分:0)

如果您知道会有一行用于提货和运输,则可以执行以下操作:

SELECT P.ORDER "ORDER", P.DATE_TIME_CREATED "PICK_DATE_TIME_CREATED", S.DATE_TIME_CREATED "SHIP_DATE_TIME_CREATED"
  FROM TRANSACTIONS P, TRANSACTIONS S
 WHERE P.ORDER = S.ORDER
   AND P.ACTION = 'PICK'
   AND S.ACTION = 'SHIP'
   AND P.ORDER = 'D8ZH4RJSB-689';

如果不能确定给定顺序的两行都存在,请使用外部联接。为了提高性能,您可能还想在ORDER,ACTION列上添加索引。