Insurance Info Per Patient, Grouped by Service Year?
Posted: Mon Mar 19, 2018 2:01 pm
I am trying to output a table with the following columns to match Dentrix info:
plan_id
patient_id
current_year_benefits
patient_count
family_deductible_met
family_preventive_deductible_met
family_benefits_last_year
family_deductible_last_year
family_preventive_deductible_last_year
max_family_coverage
max_person_coverage
secondary_plan_id
secondary_current_year_benefits
secondary_patient_count
secondary_family_deductible_met
secondary_family_preventive_deductible_met
secondary_family_benefits_last_year
secondary_family_deductible_last_year
secondary_family_preventive_deductible_last_year
secondary_max_family_coverage
secondary_max_person_coverage
So far I have the following query, which is kind of a catchall of everything I can make sense of, hoping to then whittle it down:
Here is the data I am trying to match it up to:
http://www.sqlfiddle.com/#!9/219cf7/1/0
I am trying to figure out how to get the current insurance usage from each patient for the current year and the previous year. The query I have will only return patients that have had work done, not those who have not used insurance at all. We also need to take into consideration the renew month. I am not sure yet what to do about grouping them by guarantor. Any help would be appreciated.
plan_id
patient_id
current_year_benefits
patient_count
family_deductible_met
family_preventive_deductible_met
family_benefits_last_year
family_deductible_last_year
family_preventive_deductible_last_year
max_family_coverage
max_person_coverage
secondary_plan_id
secondary_current_year_benefits
secondary_patient_count
secondary_family_deductible_met
secondary_family_preventive_deductible_met
secondary_family_benefits_last_year
secondary_family_deductible_last_year
secondary_family_preventive_deductible_last_year
secondary_max_family_coverage
secondary_max_person_coverage
So far I have the following query, which is kind of a catchall of everything I can make sense of, hoping to then whittle it down:
Code: Select all
SELECT * FROM (SELECT
claim.ClaimNum,
patient.patnum,
patient.Guarantor,
benefit.percent,
CASE
WHEN benefit.TimePeriod = '0' THEN
'None'
WHEN benefit.TimePeriod = '1' THEN
'ServiceYear'
WHEN benefit.TimePeriod = '2' THEN
'CalendarYear'
WHEN benefit.TimePeriod = '3' THEN
'Lifetime'
WHEN benefit.TimePeriod = '4' THEN
'Years'
ELSE
'Undefined'
END AS benefit_time_period,
CASE
WHEN benefit.CoverageLevel = '0' THEN
'None specified - Typical for percentages and copayments.'
WHEN benefit.CoverageLevel = '1' THEN
'Individual'
WHEN benefit.CoverageLevel = '2' THEN
'Family'
ELSE
'Undefined'
END AS benefit_coverage_level,
benefit_cat.description,
benefit_codes.fromcode,
benefit_codes.tocode,
benefit_cat.defaultpercent,
CASE
WHEN YEAR (claim.DateService) = YEAR (now()) THEN
"current"
ELSE
YEAR (claim.DateService)
END AS year_service,
claim.ClaimStatus,
claim.PlanNum,
claim.PlanNum2,
claim.claimfee,
claim.inspayamt,
claim.WriteOff,
(ClaimFee - InsPayAmt - WriteOff) as balance_due,
plan.GroupName,
plan2.GroupName AS GroupName2,
plan.monthrenew,
plan2.monthrenew AS monthrenew2,
b2.monetaryamt AS monetaryamt2,
benefit.monetaryamt AS plan_benefit_limit
FROM
patient
LEFT JOIN claim AS claim ON claim.PatNum = patient.PatNum
LEFT JOIN insplan AS plan ON plan.plannum = claim.plannum
LEFT JOIN insplan AS plan2 ON plan2.plannum = claim.PlanNum2
AND claim.PlanNum2 != "0"
LEFT JOIN benefit AS benefit ON benefit.plannum = claim.PlanNum
LEFT JOIN benefit AS b2 ON b2.plannum = claim.PlanNum2
AND claim.PlanNum2 != "0"
LEFT JOIN covcat AS benefit_cat ON benefit_cat.covcatnum = benefit.covcatnum
LEFT JOIN covspan AS benefit_codes ON benefit_codes.covcatnum = benefit.covcatnum
)as results
where results.year_service > "2016"
group by ClaimNum
http://www.sqlfiddle.com/#!9/219cf7/1/0
I am trying to figure out how to get the current insurance usage from each patient for the current year and the previous year. The query I have will only return patients that have had work done, not those who have not used insurance at all. We also need to take into consideration the renew month. I am not sure yet what to do about grouping them by guarantor. Any help would be appreciated.