SQL Server SQL Count of Dates and Most Recent Value

zf2sa74q  于 2024-01-05  发布在  其他
关注(0)|答案(1)|浏览(98)

I create a count of appointment dates with the most recent date's value. This will be a portion of a larger query for where to find all patients in the system who were first seen over a year ago, have 2 or more visits in the current calendar year, and what the insurance/payor was on their most recent visit. This query is just focusing on the 2+ visits in the calendar year and the insurance on the most recent visit part.

I have a query that is almost there, but due to the required grouping, it separates out the sum of appointments based on the payor:

  1. select
  2. tab.PrimaryMrn
  3. ,sum(tab.Appt_Count) ct
  4. ,tab.Payor
  5. from
  6. (
  7. select
  8. PatientDim.PrimaryMrn
  9. ,VisitFact.AppointmentDateKey
  10. ,(CASE
  11. WHEN CoverageDim.PayorFinancialClass IN ('Blue Cross Commercial','Commercial') THEN 'Private'
  12. WHEN CoverageDim.PayorFinancialClass IN ('Managed Care','Medicaid','Medicaid Replacement','Medicare','Medicare Replacement','Pending Medicaid','Self-Pay') THEN 'Medicaid'
  13. ELSE 'Other'
  14. END) as 'Payor'
  15. ,count(distinct VisitFact.EncounterKey) Appt_Count
  16. ,row_number() OVER (PARTITION BY PatientDim.PrimaryMrn ORDER BY VisitFact.AppointmentDateKey DESC) AS rn
  17. from VisitFact
  18. INNER JOIN PatientDim
  19. on VisitFact.PatientDurableKey = PatientDim.DurableKey and PatientDim.IsCurrent=1
  20. INNER JOIN CoverageDim
  21. ON VisitFact.CoverageKey = CoverageDim.CoverageKey
  22. where VisitFact.AppointmentDateKey between '20230101' and '20231231'
  23. group by PatientDim.PrimaryMrn ,VisitFact.AppointmentDateKey ,CoverageDim.PayorFinancialClass
  24. ) tab
  25. group by PrimaryMrn, tab.Payor
  26. order by PrimaryMrn

The inner query does what I expect:

PrimaryMrnAppointmentDateKeyPayorAppt_Countrn
123456720230724Medicaid11
123456720230427Private12
123456720230123Private13

The result I want:

PrimaryMrnPayorAppt_Count
1234567Medicaid3

'Medicaid' should be pulled in as the 'Payor' because it is associated with the most recent visit (2023-07-24), and the appointment counts should sum to the total of 3 since there were 3 visits. I will then filter this list to only include patients with 2 or more visits based on the Appt_Count field.

What my query gives:
| PrimaryMrn | Payor | Appt_Count |
| ------------ | ------------ | ------------ |
| 1234567 | Medicaid | 1 |
| 1234567 | Private | 2 |

I tried pulling by row number=1, but that removes the other visits I need to sum. I tried summing the visits first and then taking the max Payor, but that results in 'Private' since it chooses the max alphabetically.

How I can do what I'm trying to accomplish?

vxf3dgd4

vxf3dgd41#

You can try below query:

Here, I have added a new column AppointmentDateKeyPayor. It is a concatenation of AppointmentDateKey and Payor.

It is a trick to associate the payor on the AppointmentDate. Now, if we take max of AppointmentDateKeyPayor, we have max AppointmentDateKey alone with payor on that date.

Lastly, max AppointmentDateKey from AppointmentDateKeyPayor is removed using RELPACE function to only get the Payor we need.

This approach avoids adding one more subquery or OUTER APPLY.

  1. select
  2. tab.PrimaryMrn
  3. , REPLACE(max(AppointmentDateKeyPayor), max(AppointmentDateKey), '') Payor
  4. ,sum(tab.Appt_Count) ct
  5. from (
  6. select
  7. PatientDim.PrimaryMrn
  8. ,VisitFact.AppointmentDateKey
  9. ,(CASE
  10. WHEN CoverageDim.PayorFinancialClass IN ('Blue Cross Commercial','Commercial') THEN 'Private'
  11. WHEN CoverageDim.PayorFinancialClass IN ('Managed Care','Medicaid','Medicaid Replacement','Medicare','Medicare Replacement','Pending Medicaid','Self-Pay') THEN 'Medicaid'
  12. ELSE 'Other'
  13. END) as 'Payor'
  14. ,CAST(VisitFact.AppointmentDateKey as VARCHAR(10))
  15. +(CASE
  16. WHEN CoverageDim.PayorFinancialClass IN ('Blue Cross Commercial','Commercial') THEN 'Private'
  17. WHEN CoverageDim.PayorFinancialClass IN ('Managed Care','Medicaid','Medicaid Replacement','Medicare','Medicare Replacement','Pending Medicaid','Self-Pay') THEN 'Medicaid'
  18. ELSE 'Other'
  19. END) as 'AppointmentDateKeyPayor'
  20. ,count(distinct VisitFact.EncounterKey) Appt_Count
  21. from VisitFact
  22. INNER JOIN PatientDim
  23. on VisitFact.PatientDurableKey = PatientDim.DurableKey and
  24. PatientDim.IsCurrent=1
  25. INNER JOIN CoverageDim
  26. ON VisitFact.CoverageKey = CoverageDim.CoverageKey
  27. where VisitFact.AppointmentDateKey between '20230101' and '20231231'
  28. group by PatientDim.PrimaryMrn ,VisitFact.AppointmentDateKey
  29. ,CoverageDim.PayorFinancialClass
  30. ) tab
  31. group by PrimaryMrn
  32. order by PrimaryMrn
展开查看全部

相关问题