Question

Not sure the title is appropriate but couldn't think how else to phrase it. Here's the question.

I am trying to determine if a client used all of their benefits during a membership period. Here is what I have so far.

Select
       HospitalMasterID
      ,PatientID
      ,TxnCode
      ,TxnDate
      ,PlanDate
From Treatments
where PN = 1
Order By PatientID, TxnDate

Now what I need to do is assign a grouping based on the PlanDate. It is likely that a patient has renewed the plan. So from March 2012 - March 2013 they have used 5 separate benefits. Then repurchased the plan in March of 2013 so it starts again. I want to know how many and what benefits they are not using from 2012. In order to do this I need to find a way to categorize the groupings- I can't just use year as the plan can be purchased any time and it is active for a full year from purchase.

This has to be done for many patients at once- Using SQL 2008R2

Any suggestions would be appreciated.

Thanks,

Was it helpful?

Solution

Select
       HospitalMasterID
      ,PatientID
      ,TxnCode
      ,TxnDate
      ,PlanDate
      ,ROW_NUMBER() OVER (PARTITION BY PatientID, PlanDate ORDER BY TxnDate) AS GroupNumber
From Treatments
where PN = 1
Order By PatientID, TxnDate

or probably

SELECT PatientID, PlanDate, Max(GroupNumber) as BenefitCount
FROM (
  Select
       HospitalMasterID
      ,PatientID
      ,TxnCode
      ,TxnDate
      ,PlanDate
      ,ROW_NUMBER() OVER (PARTITION BY PatientID, PlanDate ORDER BY TxnDate) AS GroupNumber
  From Treatments
  where PN = 1
)
GROUP BY PatientID, PlanDate

OTHER TIPS

Thanks for the help but I think I will use a combination of PatientID, PlanDate to do this.

IN which case the original query works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top