I have a table with a million records. This is the structure of the table with some example data points -
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
I need to write a query that would produce this output -
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
The reason the second claim of patient 322 was given 20181008
is because both the first and the second one have the same cd
value.
The reason the third claim of patient 322 was also given 20181008
value even though it doesn't have the same cd
value is because it is the last claim for the patient.
The reason the first claim of patient 122 is still a NULL is because that claim's cd
value is not equal to 42.
The reason the third claim of patient 122 was NOT given a value of 20190102
even though it has the same cd
value is because the thru_dt in their prior claim is more than 30 days apart.
This is what I have tried so far -
--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
I think a lateral join and conditional expressions make it simpler to implement the logic you want:
Demo on DB Fiddle: