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

青春壹個敷衍的年華 提交于 2019-12-11 16:54:49

问题


I am trying to retrieve the last record inserted/updated in the table SHIPMENT_EVENT. There are a couple of these events/codes that are not needed for instance all beginning with '9'. The issue is that these events are always the last to be sent but are the least important to me so I need to display the last event, but if this one is a 9X event, then it should not display it, instead it should continue looking for the last event which is not 9X until reaching a valid code to show.

Here is the code I have that thought was helping my case. This left join should check the event table and retrieve the last event. However, by testing deeply saw that it was showing NULL instead, when the last event was a 9X.

I've tried moving the NOT LIKE clause arround, outside the first block, but with the same result. I noticed it was wrong when removing directly the not like, I ended up with all results wit the 9X event.

left join 

(    select ship_id, ship_to_loc_cd856, ship_evnt_cd, updt_job_tms, 
carr_tracking_num, event_srv_lvl
         FROM
         (   select  ship_id, ship_to_loc_cd856, ship_evnt_cd, 
updt_job_tms, carr_tracking_num, event_srv_lvl
            ,        row_number() over(partition by ship_id order by 
updt_job_tms desc) as RN
          FROM     shipment_event
         ) s
         WHERE RN = 1  and ship_evnt_cd not like ('9%')
        )  AS lscus
ON        scus.ship_id=lscus.ship_id and 
scus.ship_to_loc_code=lscus.ship_to_loc_cd856

I would need the logic to check the most recent event and retrieve, but if it is 9X then continue looking for the next non 9x event.

Thanks


回答1:


Your problem is that you're filtering out undesired events after getting the most recent row:

WHERE RN = 1  and ship_evnt_cd not like ('9%')

You need to filter first, and get the most-recent row from those that are left:

LEFT JOIN (SELECT ship_id, ship_to_loc_cd856, ship_evnt_cd, 
                  updt_job_tms, carr_tracking_num, event_srv_lvl, 
                  ROW_NUMBER() OVER(PARTITION BY ship_id ORDER BY updt_job_tms DESC) AS rn
           FROM Shipment_Event
           -- This _might_ perform better than the NOT LIKE condition, you'll need to test
           WHERE LEFT(ship_evnt_cd, 1) <> '9') lscus
       ON lscus.ship_id = scus.ship_id 
          AND lscus.ship_to_loc_cd856 = scus.ship_to_loc_code 
          AND lscus.rn = 1



回答2:


If you have some ship_ids that only have 9% events, then you would need, e.g.

select  ship_id, ship_to_loc_cd856, ship_evnt_cd
,     updt_job_tms, carr_tracking_num, event_srv_lvl
,     row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM    shipment_event e
WHERE   ship_evnt_cd not like '9%'
    OR  NOT EXISTS (SELECT 1 from shipment_event s 
                    WHERE s.ship_id = e.ship_id 
                     AND ship_evnt_cd not like '9%')

as your sub-select



来源:https://stackoverflow.com/questions/54370611/need-to-retrieve-tables-last-inserted-updated-record-with-some-exclusions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!