左连接的复制问题只能通过添加一列来实现

时间:2015-12-15 23:24:27

标签: sql db2

所以我为代码之墙道歉,但是我对这个踢我屁股的小问题失去了理智。下面的查询工作得很好,除了DB.ACTIVITY_T中的一列之外,还给了我需要查看的所有内容。在我添加所述列的那一刻,我立即得到重复项,其中存在MNT.ARRIVEDTS。我似乎无法弄清楚为什么在我的select语句中再添加一列会导致这种重复。也许我对此深有洞察,看看我面前的情况如何?如果你看到可能导致错误的原因,请随时赐教。

    SELECT
    TRIM(B.REFNUMBER) AS "REFNUMBER"
    ,DATE(NUM_TSTAMP) AS "P/U DATE"
    ,CASE WHEN DATE(REPORT_TS) IS NULL THEN '' ELSE CHAR(DATE(REPORT_TS)) END AS "DELIVERY DATE"
    ,CASE WHEN REPORT_TS IS NULL THEN 'N' ELSE 'Y' END AS "DELIVERED"
    ,B.WEIGHT AS "WGT"
    ,B.PIECES
    ,B.WANT_DATE
    ,CEIL(B.CUBE) AS "SHPMNT CUBE"
    ,CASE WHEN B.DESTINATION = '5647' THEN 'DERPO'
          WHEN B.DESTINATION = '1234' THEN 'DERPB'
          WHEN B.DESTINATION = '9856' THEN 'DERPC'
          ELSE ''
    END AS "DERP POINT"
    ,CASE WHEN R.REFNO IS NULL THEN '' ELSE CHAR(R.REFNO) END AS "DERP #"
    ,CASE WHEN C.CITY = 'DERPSTON' THEN 'BLAH'
          WHEN C.CITY = 'DERPELVANIA' THEN 'HABD'
          WHEN C.CITY = 'DERPVILLE' THEN 'POIN'
          ELSE ''
    END AS "SEQUENCE"
    ,CASE WHEN E.EVENT_DESCRIPTION = 'ABCD' THEN 'Y' 
          ELSE 'N' 
    END AS "DERP DERP" 
    ,DATE(E.EVENT_TIMESTAMP) AS "DERP DATE"
    ,TIME(E.EVENT_TIMESTAMP) AS "DERP TIME"
    ,CASE WHEN I.DOC_TYPE = 'DERPI' THEN 'Y' 
          ELSE 'N'
    END AS "DERP
    ,CASE WHEN B.CUBE > '0.5' THEN 'Y' ELSE 'N' END AS "DERP CUBE”
    ,MNT."DERP ARRIVED"

    FROM PQ.MAIN B

    INNER JOIN PQ.MAIN_ALTERNATE A
    ON B.REFNUMBER = A.REFNUMBER
    AND B.CORRECTION = A.CORRECTION
    AND A.NUMBER_KEY = ''

    INNER JOIN PQ.MAIN_NAME C
    ON C.REFNUMBER = B.REFNUMBER
    AND C.CORRECTION = B.CORRECTION
    AND C.TYPE = 'C'
    AND C.NUMBER_KEY = ''
    AND C.NUM_CODE = 'ABCD'

    INNER JOIN PQ.MAIN_NAME S
    ON B.REFNUMBER = S.REFNUMBER
    AND B.CORRECTION = S.CORRECTION
    AND S.TYPE = 'S'
    AND S.COUNTRY = 'US'
    AND S.NUMBER_KEY = ''

    LEFT OUTER JOIN DB.ACTIVITY_NUM MN
    ON MN.REFNUMBER = B.REFNUMBER

    LEFT OUTER JOIN (SELECT MAX(DATE(A.ARRIVEDTS)) AS "DERP ARRIVED", MFSTNBR
    FROM AF.MANIFEST_T A WHERE A.DESTINATION IN ('1234','5647') GROUP BY MFSTNBR) AS MNT
    ON MNT.MFSTNBR = MN.MFSTNBR

    LEFT OUTER JOIN PQ.MAIN_EVENT E
    ON E.REFNUMBER = B.REFNUMBER
    AND E.EVENT_TYPE = 'R'
    AND E.EVENT_DESCRIPTION = 'RANDO'
    AND E.NUMBER_KEY = ''

    LEFT OUTER JOIN DB.DERP_INDEX I
    ON B.REFNUMBER = I.REFNUMBER
    AND I.DOC_TYPE = 'LIE'
    AND PAGE_NUM = '1'
    AND I.INDEX_DATE >= CURRENT DATE - 15 DAYS

    LEFT OUTER JOIN PQ.MAIN_REFNO R
    ON R.REFNUMBER = B.REFNUMBER
    AND R.CORRECTION = B.CORRECTION
    AND REFNO LIKE '%BOOG%'
    AND R.NUMBER_KEY = ''

    WHERE B.NUM_NUM_NUM = '123'
    AND B.DESTINATION IN ('1234','5647','9856')
    AND DATE(B.NUM_TSTAMP) >= CURRENT DATE - 10 DAYS

    GROUP BY 
    TRIM(B.REFNUMBER) 
    ,DATE(NUM_TSTAMP) 
    ,CASE WHEN DATE(REPORT_TS) IS NULL THEN '' ELSE CHAR(DATE(REPORT_TS)) END 
    ,CASE WHEN REPORT_TS IS NULL THEN 'N' ELSE 'Y' END
    ,B.WEIGHT
    ,B.PIECES
    ,B.WANT_DATE
    ,CEIL(B.CUBE)
    ,CASE WHEN B.DESTINATION = '5647' THEN 'DERPO'
          WHEN B.DESTINATION = '1234' THEN 'DERPB'
          WHEN B.DESTINATION = '9856' THEN 'DERPC'
          ELSE ''
    END
    ,CASE WHEN R.REFNO IS NULL THEN '' ELSE CHAR(R.REFNO)
    ,CASE WHEN C.CITY = 'DERPSTON' THEN 'BLAH'
          WHEN C.CITY = 'DERPELVANIA' THEN 'HABD'
          WHEN C.CITY = 'DERPVILLE' THEN 'POIN'
          ELSE ''
    END 
    ,CASE WHEN E.EVENT_DESCRIPTION = 'ABCD' THEN 'Y' 
          ELSE 'N' 
    END
    ,DATE(E.EVENT_TIMESTAMP) 
    ,TIME(E.EVENT_TIMESTAMP) 
    ,CASE WHEN I.DOC_TYPE = 'DERPI' THEN 'Y' 
          ELSE 'N'
    END
    ,CASE WHEN B.CUBE > '0.5' THEN 'Y' ELSE 'N' END
    ,MNT."DERP ARRIVED"

以下是您要求的结果。

   REFNUMBER   DERP ARRIVED    MFSTNBR
   123456789   [null]          [null]
   123456789   [null]          [null]
   123456789   2015-12-15      32304587
   987654321   [null]          [null]
   987654321   [null]          [null]
   987654321   2015-12-13      49304483

每个null实际上都有一个与之绑定的不同MFSTNBR,但是它返回一个空值,因为它不符合“1234”和“1234”。或' 5647'标准。如果我删除了目标条件,则将使用类似但不同的MFSTNBR替换空值。

1 个答案:

答案 0 :(得分:1)

我的猜测是,当你只想要一个......时,你会从MNT获得多条记录......

可能具有2个不同值的MNT.Destination将导致最多2条记录,而不是一条记录。

替换

LEFT OUTER JOIN DB.ACTIVITY_T MNT

(SELECT MAX(DATE(MNT.ARRIVEDTS)) as "DERP ARRIVED", MFSTNBR
FROM DB.Activity_T
WHERE MNT.DESTINATION IN ('1234','5647')
GROUP BY MFSTNBR) as MNT

并消除连接标记;

ND MNT.DESTINATION IN ('1234','5647') as it's taken care of by the inline view.

哦,修改选择只是在最顶层的选择中寻找“DERP ARRIVED”。

只是我猜...

SELECT
TRIM(B.REFNUMBER) AS "REFNUMBER"
,DATE(NUM_TSTAMP) AS "P/U DATE"
,CASE WHEN DATE(REPORT_TS) IS NULL THEN '' ELSE CHAR(DATE(REPORT_TS)) END AS "DELIVERY DATE"
,CASE WHEN REPORT_TS IS NULL THEN 'N' ELSE 'Y' END AS "DELIVERED"
,B.WEIGHT AS "WGT"
,B.PIECES
,B.WANT_DATE
,CEIL(B.CUBE) AS "SHPMNT CUBE"
,CASE WHEN B.DESTINATION = '5647' THEN 'DERPO'
      WHEN B.DESTINATION = '1234' THEN 'DERPB'
      WHEN B.DESTINATION = '9856' THEN 'DERPC'
      ELSE ''
END AS "DERP POINT"
,CASE WHEN R.REFNO IS NULL THEN '' ELSE CHAR(R.REFNO) END AS "DERP #"
,CASE WHEN C.CITY = 'DERPSTON' THEN 'BLAH'
      WHEN C.CITY = 'DERPELVANIA' THEN 'HABD'
      WHEN C.CITY = 'DERPVILLE' THEN 'POIN'
      ELSE ''
END AS "SEQUENCE"
,CASE WHEN E.EVENT_DESCRIPTION = 'ABCD' THEN 'Y' 
      ELSE 'N' 
END AS "DERP DERP" 
,DATE(E.EVENT_TIMESTAMP) AS "DERP DATE"
,TIME(E.EVENT_TIMESTAMP) AS "DERP TIME"
,CASE WHEN I.DOC_TYPE = 'DERPI' THEN 'Y' 
      ELSE 'N'
END AS "DERP
,CASE WHEN B.CUBE > '0.5' THEN 'Y' ELSE 'N' END AS "DERP CUBE”
-------Modified the next line--------------
,MNT."DERP ARRIVED"

FROM PQ.MAIN B

INNER JOIN PQ.MAIN_ALTERNATE A
ON B.REFNUMBER = A.REFNUMBER
AND B.CORRECTION = A.CORRECTION
AND A.NUMBER_KEY = ''

INNER JOIN PQ.MAIN_NAME C
ON C.REFNUMBER = B.REFNUMBER
AND C.CORRECTION = B.CORRECTION
AND C.TYPE = 'C'
AND C.NUMBER_KEY = ''
AND C.NUM_CODE = 'ABCD'

INNER JOIN PQ.MAIN_NAME S
ON B.REFNUMBER = S.REFNUMBER
AND B.CORRECTION = S.CORRECTION
AND S.TYPE = 'S'
AND S.COUNTRY = 'US'
AND S.NUMBER_KEY = ''

LEFT OUTER JOIN DB.ACTIVITY_NUM MN
ON MN.REFNUMBER = B.REFNUMBER

-------Modified the next line--------------
LEFT OUTER JOIN     (SELECT MAX(DATE(A.ARRIVEDTS)) as "DERP ARRIVED", MFSTNBR
    FROM DB.Activity_T A
    WHERE A.DESTINATION IN ('1234','5647')
    GROUP BY MFSTNBR) as MNT
ON MNT.MFSTNBR = MN.MFSTNBR
-------Removed this line. the next line--------------
---and MNT.DESTINATION IN ('1234','5647')    -------

LEFT OUTER JOIN PQ.MAIN_EVENT E
ON E.REFNUMBER = B.REFNUMBER
AND E.EVENT_TYPE = 'R'
AND E.EVENT_DESCRIPTION = 'RANDO'
AND E.NUMBER_KEY = ''

LEFT OUTER JOIN DB.DERP_INDEX I
ON B.REFNUMBER = I.REFNUMBER
AND I.DOC_TYPE = 'LIE'
AND PAGE_NUM = '1'
AND I.INDEX_DATE >= CURRENT DATE - 15 DAYS

LEFT OUTER JOIN PQ.MAIN_REFNO R
ON R.REFNUMBER = B.REFNUMBER
AND R.CORRECTION = B.CORRECTION
AND REFNO LIKE '%BOOG%'
AND R.NUMBER_KEY = ''

WHERE B.NUM_NUM_NUM = '123'
AND B.DESTINATION IN ('1234','5647','9856')
AND DATE(B.NUM_TSTAMP) >= CURRENT DATE - 10 DAYS

GROUP BY 
TRIM(B.REFNUMBER)
,DATE(NUM_TSTAMP)
,REPORT_TS
,REPORT_TS
,B.WEIGHT 
,B.PIECES
,B.WANT_DATE
,CEIL(B.CUBE)
,B.DESTINATION
,R.REFNO
,C.CITY
,E.EVENT_DESCRIPTION
,DATE(E.EVENT_TIMESTAMP)
,TIME(E.EVENT_TIMESTAMP)
,I.DOC_TYPE
,B.CUBE
 --- Added this line----
,MNT."DERP ARRIVED"