makeitwork09
Technical User
I'm bit confused with the query below.
Here is an example of the data set.
PURGE_ACTION LOAN CLOSEOUT_STATUS PREVIOUSLOANNUM
PURGE 3610878 REFINANCED
PURGE 3610887 REFINANCED
KEEP 3728285 REFINANCED 3610878 3610887
Note, the above example does not have all the columns. Also not that the query works as expected when only one value is in the PREVIOUSLOANNUM column.
The result I want is to return the first two rows above because the loan number is in the KEEP record's PREVIOUSLOANNUM.
The issue is the code is only returning the second row for loan 3610887. Is my code incorrect or is this because of the way SQL reads records?
Here is an example of the data set.
PURGE_ACTION LOAN CLOSEOUT_STATUS PREVIOUSLOANNUM
PURGE 3610878 REFINANCED
PURGE 3610887 REFINANCED
KEEP 3728285 REFINANCED 3610878 3610887
Note, the above example does not have all the columns. Also not that the query works as expected when only one value is in the PREVIOUSLOANNUM column.
The result I want is to return the first two rows above because the loan number is in the KEEP record's PREVIOUSLOANNUM.
The issue is the code is only returning the second row for loan 3610887. Is my code incorrect or is this because of the way SQL reads records?
SQL:
select *
from ##base b
join (
select b2.PREVIOUSLOANNUM
from ##base b2
where b2.PURGE_ACTION = 'KEEP'
and b2.CLOSEOUT_STATUS = 'REFINANCED') as k on
k.PREVIOUSLOANNUM like '%' + b.loan + '%'
where b.PURGE_ACTION = 'PURGE'
and b.CLOSEOUT_STATUS = 'REFINANCED'