I have a table that contains history of a claim. Basically I'm looking at status changes and dates. Whenever someone updates a claim, the new row is loaded into the table I'm showing below. What I'm trying to obtain is all of the status changes for the column "c_sta_clm" I want to be able to capture the date "row_begin_dt" and both status change (PC to AC) & (AC to TE).
Any guidance on how to make this simple is hugely appreciated. I was thinking of making two volatile tables and joining on C_CLM, taking min status dates and comparing etc...
row_begin_dt user c_clm c_sta_clm
2009-10-08 ? C5S2M 09050012 PC
2009-10-24 ? C5S2M 09050012 AC
2009-10-28 ? C1CMH 09050012 AC
2010-10-30 ? C1CMH 09050012 AC
2011-05-19 ? A9709 09050012 AC
2011-06-09 ? C6JEC 09050012 AC
2011-10-07 ? DAJ07 09050012 TE
2011-11-04 ? DAJ07 0905001 TE
This should get you the records for 2009-10-24 and 2011-10-07.
select
row_begin_dt,
user,
c_clm,
c_sta_clm,
-- Find the c_sta_clm of the previous row
max(c_sta_clm) over (partition by c_clm order by row_begin_dt rows between 1 preceding and 1 preceding) as prev_c_sta_clm
from claims
-- Include only records which have a c_sta_clm different to that of the previous row
qualify c_sta_clm <> prev_c_sta_clm
One general way to do this is using correlated subqueries:
select
from (select c.*,
(select top 1 from claims c2 where c2.c_clm = c.c_clm a and c2.row_begin_dt > c.row_begin_dt order by row_begin_dt
) as next_sta_clm
from claim c2
) c
where next_sta_clm <> c_sta_clm
In many databases, you can do the same thing with the lag
or lead
function, but no all databases support them.
Something like
Select ToRecord.row_begin_dt From ClaimHistory FromRecord
inner join ClaimHistory ToRecord On ToRecord.c_clm = FromRecord.c_clm and ToRecord.row_begin_dt > FromRecord.row_begin_dt and ToRecord.c_sta_claim = 'AC'
Where FromRecord.c_sta_claim = 'PC'
would get PC to AC, didnlt know whether user column was significant, but bolting it in should be trivial. Seeing as you didn't say which DBMS, sql may require a twiddle.
Also this would pick up something like PC to XX to AC, which you didnt say anything about.
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.