首页 > 解决方案 > 用于循环记录的 SQL 查询

问题描述

我有一张包含一百万条记录的表。这是带有一些示例数据点的表格结构 -

patient   claim   thru_dt   cd   start     
322       65      20200201  42   20181008  
322       65      20200202  42             
322       95      20200203  52             
122       05      20200105  23             
122       05      20200115  42   20190102  
122       05      20200116  42            

我需要编写一个会产生此输出的查询 -

patient   claim   thru_dt   cd   start     
322       65      20200201  42   20181008    
322       65      20200202  42   20181008    
322       95      20200203  52   20181008    
122       05      20200105  23               
122       05      20200115  42   20190102    
122       05      20200416  42      

给出患者 322 的第二个声明的原因20181008是因为第一个和第二个具有相同的cd值。

患者 322 的第三次索赔也被赋予了20181008价值,即使它没有相同的cd价值,因为它是患者的最后一次索赔。

患者 122 的第一个声明仍然为 NULL 的原因是该声明的cd值不等于 42。

患者 122 的第三次索赔20190102即使具有相同的cd值也没有被赋予值的原因是因为他们先前索赔中的 thru_dt 相隔超过 30 天。

这是我迄今为止尝试过的 -

--This orders claims using row_number
DECLARE @min_record int;
DECLARE @max_record int;

select 
    @min_record = MIN(row_num), 
    @max_record = MAX(row_num) 
from 
(
    select *, 
    row_number() over(partition by patient order by thru_dt) as row_num
    from 
    table
)

while @min_record <= @max_record
begin 
    --Logic I need help with 
    SET @min_record = @min_record + 1
end 

标签: sqlsql-serverdatetimecaselateral-join

解决方案


我认为横向连接和条件表达式可以更简单地实现您想要的逻辑:

select t.*,
    case 
        when t.start is null and (
            s.cd = t.cd 
            or row_number() over(partition by t.patient order by t.thru_dt desc) = 1
        )
        then s.start
        else t.start
    end new_start
from mytable t
outer apply (
    select top (1) s.*
    from mytable s
    where 
        s.patient = t.patient 
        and s.start is not null
        and s.thru_dt >= dateadd(day, -30, t.thru_dt)
    order by s.thru_dt desc
) s
order by patient desc, thru_dt

DB Fiddle 上的演示

病人 | 索赔 | thru_dt | 光盘 | 开始 | 新的开始
------: | ----: | :--------- | -: | :--------- | :---------
    322 | 65 | 2020-02-01 | 42 | 2018-10-08 | 2018-10-08
    322 | 65 | 2020-02-02 | 42 |        | 2018-10-08
    322 | 95 | 2020-02-03 | 52 |        | 2018-10-08
    122 | 5 | 2020-01-05 | 23 |        | 无效的      
    122 | 5 | 2020-01-15 | 42 | 2019-01-02 | 2019-01-02
    122 | 5 | 2020-04-16 | 42 |        | 无效的      

推荐阅读