无法正确订购查询

时间:2019-02-19 14:06:15

标签: sql join db2 left-join

前一段时间,我请求帮助以特殊方式对LEFT JOIN过滤进行编码,以使结果在第一行中保留所需的值。

Need to retrieve table's last inserted/updated record with some exclusions

现在的问题是,有很多情况正在混合数据。场景是在同一张表上,我们需要在不同的列上组织两个值。 PO_ID是唯一的,但在其他表上可以具有1个或多个值,对于这种特殊情况,1个PO_ID具有3个SHIP_ID_CUS值。每行只需要1个PO_ID(无重复),这就是我们使用MAX()和GROUP BY的方式。

这是我认为会引起问题的一部分代码。

select
z.po_id,
max(scdc.ship_id) as ship_id_cdc,
max(lscdc.ship_evnt_cd) as last_event_cdc,
max(lscdc.ship_evnt_tms) as event_tms_cdc,
max(scus.SHIP_ID) as ship_id_cus, 
max(lscus.ship_evnt_cd) as last_event_cus,
max(lscus.ship_evnt_tms) as event_tms_cus

from TABLE.A z
left join (select distinct po_id, iltc.ship_id, s.ship_to_loc_code from TABLE.B iltc inner join TABLE.C s on iltc.ship_id=s.ship_id and iltc.ship_to_loc_code=s.ship_to_loc_code and s.ship_to_ctry<>'   ') AS A ON z.po_id = a.po_id
left JOIN TABLE.C scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
left JOIN TABLE.C scdc ON A.SHIP_ID = scdc.SHIP_ID AND A.SHIP_TO_LOC_CODE = scdc.SHIP_TO_LOC_CODE and scdc.loc_type = 'CDC' AND DAYS(scdc.shipment_tms)+10 >= DAYS(z.ship_tms)

left join 

(    select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
            ,        row_number() over(partition by ship_id order by updt_job_tms desc) as RN
            FROM     TABLE.D
         WHERE LEFT(ship_evnt_cd, 1) <> '9') lscus
ON        lscus.ship_id_856=scus.ship_id and scus.ship_to_loc_code=lscus.ship_to_loc_cd856 and lscus.rn = 1

left join 

(    select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
           ,        row_number() over(partition by ship_id order by updt_job_tms desc) as RN
            FROM     TABLE.D
         WHERE LEFT(ship_evnt_cd, 1) <> '9') lscdc
ON        lscdc.ship_id_856=scdc.ship_id and lscdc.ship_to_loc_cd856=scdc.ship_to_loc_code and lscdc.rn = 1

WHERE
z.po_id = 'T1DLDC'
GROUP BY z.po_id  

通过搜索该条件,我们得到以下结果

enter image description here

问题是,如果我们直接在TABLE.D上进行搜索,则我们需要的最后一个事件(最后更新记录为tms)是另一个事件(X1),并且日期不正确。

enter image description here

更奇怪的是,如果我们在原始查询中搜索ship_id_cus,我们将获得正确的代码,但日期仍然错误...

WHERE
--z.po_id = 'T1DLDC'
scus.ship_id = 'D30980'
GROUP BY z.po_id 

enter image description here

我尝试了其他逻辑更改,例如修改左联接以搜索子查询。

left JOIN ( select * from TABLE.C order by updt_job_tms desc) scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)

但这也通过po_id或ship_id_cus搜索提供了相同的准确结果

任何想法或评论将不胜感激。

谢谢

------------------------------------更新---------- -------------------------

使用row_partition()添加LEFT JOIN的结果,其中包括该po_id的所有ship_id_cus和带有tms的所有代码。这里没有匹配项。

enter image description here

基于所有这些,它应该是具有X1事件/ tms的最后一个ship_id_cus。如果我们也排除以9开头的数字,则会得到以下结果。

enter image description here

(我在这里不使用ship_id_cus的排序,在此之前我已经描述过,无论用哪种实现方式都无法使用)

1 个答案:

答案 0 :(得分:0)

如果您有表格:TBL1

ID     APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC    Y        JOE      2019-01-13
ABC    N        ZACK     2018-12-23
ABC    N        SUE      2019-02-23   

然后执行SQL:

SELECT ID, MAX(APPROVED) AS APPROVAL
,MAX(APPROVER) AS APPROVED_BY , MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID

您将得到结果:

ID     APPROVAL APPROVED_BY APPROVED_ON
====== ======== =========== =============
ABC    Y        ZACK        2019-02-23

对代码正确,但不是您想要的

尝试以下操作:

SELECT T1.ID, T1.APPROVED, T1.APPROVER, T1.DATE_APPROVED
FROM TBL1 AS T1
INNER JOIN (SELECT ID, MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
) AS T2
ON T1.ID =T2.ID
AND T1.DATE_APPROVED = T2.APPROVED_ON

结果:

ID     APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC    N        SUE      2019-02-23