我正在处理代表人群索赔数据的数据提取。最终目标是能够将特定的主张与独特的遭遇相关联。我们定义遭遇的方式是一连串连续的或在相同范围内的声明。意思是说,迄今为止的索赔要求后面紧跟着一个索赔要求的起始日期,或者与另一项索赔要求的&起始日期相同。但是,由于没有键可以真正表明一组声明代表特定的遭遇,因此我们必须实现为我们执行此操作的逻辑。
在此示例中,我们已经按“患者与设施”对数据进行了分组。
CREATE TABLE #BillingData (
ClaimID VARCHAR(MAX)
,Patient VARCHAR(MAX)
,Facility VARCHAR(MAX)
,ClaimFromDate DATE
,ClaimToDate DATE
,GroupID VARCHAR(MAX)
)
INSERT INTO #BillingData
VALUES
('Claim1','JOHN DOE','NURSING HOME','2000-01-01','2000-01-01','1')
,('Claim2','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim3','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim4','JOHN DOE','NURSING HOME','2000-01-01','2000-01-31','1')
,('Claim5','JOHN DOE','NURSING HOME','2000-02-01','2000-02-29','1')
,('Claim6','JOHN DOE','NURSING HOME','2000-03-01','2000-03-31','1')
,('Claim7','JOHN DOE','NURSING HOME','2000-04-01','2000-04-30','1')
,('Claim8','JOHN DOE','NURSING HOME','2000-08-01','2000-08-31','1')
,('Claim9','JOHN DOE','NURSING HOME','2000-09-01','2000-09-30','1')
,('Claim10','JANE DOE','HOME HEALTH','2000-02-01','2000-02-29','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')
,('Claim11','JANE DOE','HOME HEALTH','2000-03-01','2000-03-31','2')
我想最终看到的是一个遭遇ID。这是将各个声明放入遇到的ID中的方法:
第1,2,3,4,5,6,7项针对Encounter 1.1; 索赔8,9在遭遇1.2中; 索赔10,11在遭遇2.1中
任何指导将不胜感激!
答案 0 :(得分:1)
您需要找到差距,即当前的开始日期大于任何以前的结束日期(加上一天)
with gaps as
(
select *,
case
when max(ClaimToDate) -- max previous end date
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows between unbounded preceding and 1 preceding)
< dateadd(day,-1,ClaimFromDate) -- compare to current start date
then 1 -- gap
else 0 -- overlapping ranges
end as flag
from #BillingData
)
select *,
sum(flag) -- cumulative sum over 0/1 to create encounter number
over (partition by Patient, Facility
order by ClaimFromDate, ClaimID
rows unbounded preceding) +1 as encounter
from gaps
order by GroupID, encounter, ClaimFromDate
请参见fiddle