1

我有一张包含索赔历史的表格。基本上我正在查看状态变化和日期。每当有人更新声明时,新行就会加载到我在下面显示的表格中。我想要获取的是列“c_sta_clm”的所有状态更改,我希望能够捕获日期“row_begin_dt”以及状态更改(PC 到 AC)和(AC 到 TE)。

非常感谢任何有关如何使这个简单的指导。我正在考虑制作两个易失性表并加入 C_CLM,获取最小状态日期并进行比较等...

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
4

3 回答 3

2

这应该为您提供 2009-10-24 和 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
于 2012-12-05T04:06:21.820 回答
1

一种通用的方法是使用相关子查询:

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

在许多数据库中,您可以使用lagorlead函数做同样的事情,但并非所有数据库都支持它们。

于 2012-12-04T17:43:09.257 回答
0

就像是

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'

将 PC 转换为 AC,不知道用户列是否重要,但将其插入应该是微不足道的。看到你没有说哪个DBMS,sql可能需要一个旋转。

此外,这将拾取诸如 PC 到 XX 到 AC 之类的东西,你没有说什么。

于 2012-12-04T17:50:50.703 回答