【问题标题】:SQL to compare records (status change) in tableSQL比较表中的记录(状态变化)
【发布时间】:2012-11-22 10:03:19
【问题描述】:

我有一张包含索赔历史记录的表格。基本上我正在查看状态变化和日期。每当有人更新声明时,新行都会加载到我在下面显示的表格中。我想要获取的是列“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

【问题讨论】:

  • SQL 是语言。这是什么 DBMS?
  • 第一次,可能也是最后一次,我会在问题中添加“Teradata”标签。
  • 我建议看看这个question的答案。您应该能够使用具有适当窗口(ROWS BETWEEN)的窗口聚合函数来完成此操作。发帖 what you have tried 我会从那里帮助你。

标签: sql teradata


【解决方案1】:

这应该会为您提供 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

【讨论】:

    【解决方案2】:

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

    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
    

    在许多数据库中,您可以使用 laglead 函数执行相同的操作,但并非所有数据库都支持它们。

    【讨论】:

    • 我已经尝试过使用实际的表名,但它与语句中的“额外”有点混淆... select * from (select c.*, (select top 1 from Pearl_p.TLTC900_CLM_PRSST c2其中 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 Pearl_p.TLTC900_CLM_PRSST c2 ) c where next_sta_clm c_sta_clm
    【解决方案3】:

    类似

    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 之类的东西,你什么都没说。

    【讨论】:

    • 这是 Teradata SQL。我认为可能还有一个 OLAP 功能。我正在尝试以正确的顺序捕获状态变化,例如 PC 到 AC,然后 AC 到 TE 对于这个数据样本只有两个。
    • 所以你只想要PC directky 后跟AC?即没有中间状态?
    • 这将是第一个 AC 的第一台 PC 和第一个 TE 的最后一个 AC
    • 这可不好笑。如果您要经常运行此程序,或者在大量数据上不那么频繁地运行,如果我是您,我会认真考虑对某种 StatusChangeHistory 表使用触发器。
    猜你喜欢
    • 2016-12-08
    • 1970-01-01
    • 2018-04-12
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 2022-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多