简体   繁体   中英

update statement based end date of previous row equaling start date of next row

I have identified overlapping dates from trainers that trained users between a start date and end date. I now need to identify those USER_IDs where the end date for one of their trainers is the same as the start date for their next trainer. I need to flag them 'Y' and 'N'.

I run into a problem when there are trainers who have the same start date and end date as the prior trainers end date for the USER_ID.

UPDATE #SERVICE A
SET DIS_ADM_MATCH = CASE WHEN B.FROM_DATE = A.TO_DATE OR
                              A.FROM_DATE  = B.TO_DATE
                         THEN 'Y' ELSE 'N' END
FROM #SERVICE A
INNER JOIN #SERVICE B
ON A.USER_ID = B.USER_ID
AND A.TRAINER_ID <> B.TRAINER_ID;

I think you want:

update service
    set DIS_ADM_MATCH = (case when exists (select 1
                                           from service s2
                                           where s2.user_id = service.user_id and
                                                 s2.from_date = service.to_date
                                          )
                              then 'Y' else 'N'
                         end);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM