我有一张包含一百万条记录的表。这是带有一些示例数据点的表格结构 -
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