Oracle查询未返回两个子查询的所有行

时间:2019-03-26 19:05:58

标签: sql oracle

我有两个子查询,它们本质上是在查询相同的东西减去一个维度(标志列)。然后,我从这两个子查询中获取数据,并在“外部查询”中返回它们。 (Orig和RE值(VAL)位于同一行)

我要加入的组合密钥由以下几列组成:

P_NAME, REAL_DATE, UTC_TIME, DATA_TYPE & LOCAL_TS

第一个子查询返回的一些示例数据

P_NAME    REAL_DATE   UTC_TIME    DATA_TYPE   VAL   LOCAL_TS      FLAG
John      01-01-2018  01:00:00AM  Height      60    04:00:00AM    ORIG
Jacob     01-01-2018  02:00:00AM  Height      68    05:00:00AM    ORIG
Jingle    01-01-2018  01:00:00AM  Height      65    04:00:00AM    ORIG
Heimer    01-01-2018  04:00:00AM  Height      68    07:00:00AM    ORIG
Schmidt   01-01-2018  05:00:00AM  Height      72    08:00:00AM    ORIG

第二个子查询返回的一些示例数据

P_NAME    REAL_DATE   UTC_TIME    DATA_TYPE   VAL   LOCAL_TS      FLAG
John      01-01-2018  01:00:00AM  Height      90    04:00:00AM    RE
Jacob     01-01-2018  02:00:00AM  Height      98    05:00:00AM    RE
Jingle    01-01-2018  01:00:00AM  Height      95    04:00:00AM    RE

我从第一和第二个子查询JOINED返回了什么

P_NAME    REAL_DATE   UTC_TIME    DATA_TYPE   O_VAL  RE_VAL    LOCAL_TS
John      01-01-2018  01:00:00AM  Height      60       90      04:00:00AM
Jacob     01-01-2018  02:00:00AM  Height      68       98      05:00:00AM
Jingle    01-01-2018  01:00:00AM  Height      65       95      04:00:00AM
Heimer    01-01-2018  04:00:00AM  Height      68      null     07:00:00AM
Schmidt   01-01-2018  05:00:00AM  Height      72      null     08:00:00AM

我尝试过但不起作用的内容:

select O.O_P_NAME as P_NAME
     , O.O_REAL_DATE as REAL_DATE
     , O.O_UTC_TIME as UTC_TIME
     , O.O_DATA_TYPE as DATA_TYPE
     , CASE WHEN O.O_VAL = R.R_VAL
            THEN null
            ELSE R.R_VAL
             END     as RESUBMITTED_VAL
     , O.O_VAL       as ORIG_VAL
     , O.O_LOCAL_TS  as LOCAL_TS
    FROM(
        (SELECT P_NAME as O_P_NAME
              , REAL_DATE as O_REAL_DATE
              , UTC_TIME as O_UTC_TIME
              , DATA_TYPE as O_DATA_TYPE
              , VAL as O_VAL
              , LOCAL_TS as O_LOCAL_TS
              , FLAG as O_FLAG
           FROM TABLE
          WHERE FLAG = 'ORIG' 
        ) O
        JOIN
        (SELECT P_NAME as R_P_NAME
              , REAL_DATE as R_REAL_DATE
              , UTC_TIME as R_UTC_TIME
              , DATA_TYPE as R_DATA_TYPE
              , VAL as R_VAL
              , LOCAL_TS as R_LOCAL_TS
              , FLAG as R_FLAG
           FROM TABLE
          WHERE FLAG = 'RE' 
        ) R
        ON O.O_P_NAME = R.R_P_NAME
       AND O.O_REAL_DATE = R.R_REAL_DATE
       AND O.O_UTC_TIME = R.R_UTC_TIME
       AND O.O_DATA_TYPE = R.R_DATA_TYPE
       AND O.O_LOCAL_TS = R.R_LOCAL_TS
        );

1 个答案:

答案 0 :(得分:1)

您不需要两个子查询。只需执行一个case语句即可分隔字段。

SELECT P_NAME 
              , REAL_DATE 
              , UTC_TIME 
              , DATA_TYPE 
              , MAX(CASE WHEN FLAG='ORIG' THEN VAL END) as O_VAL
              , MAX(CASE WHEN FLAG='RE' THEN VAL END) as R_VAL
              , LOCAL_TS 
           FROM TABLE
GROUP BY P_NAME, REAL_DATE, UTC_TIME, DATA_TYPE, LOCAL_TS

奖励答案:如果您确实确定需要子查询,请使用UNION而不是联接,因为您不知道哪个表会具有额外的值。