Solved

# member months in medical enrollment

Posted on 2006-04-19
Medium Priority
609 Views
Greeting experts
Back with another question. Thanks in advance for any help

For this question I have two table I am concerned with, called membe and member hp

Member looks like
Memberkey int (fk)
Membid varchar (15) (pk)
Lastnm varcahr  (50)
Firstnm varchar  varchar (50)

Membhp looks like

Memberkey  int  (pk)
Currhist char (1)  -- C for Current, H for history meaning the data is for the current
enrollment or the historical enrolment.
Opfromdt   -- Date they were enrolled
Opthrudt    -- date they were terminated

The member can have many entries in membhp as they can come on the plan a lot

What I do not know how to do is determine is  how many months a member has been on the plan, if there has been a break from their time on the plan.
So say I want to know how many months a member x.
Member x has been on the plan from  04/01/2004 to 03/01/2005 . They came back on the plan on 04/01/2005 and are set to terminate 01/01/2079.

How do I determine how many moths from 01/01/2005 to 12/31/2005 the member has been on the plan for.
If this is not clear ask me to clarify and I will, if I can.

Thanks in advance for any help
0
Question by:shayne23d
• 3
• 2

LVL 17

Expert Comment

ID: 16494933
Try this!

select Member.Memberkey, Member.Membid,Member.Lastnm,Member.Firstnm ,sum(datediff(month,Member.Opfromdt,Member.Opthrudt)) from Member
inner join Membhp on Membhp.Memberkey =Member.Memberkey
where Member.Membid='x'
group by Member.Memberkey, Member.Membid,Member.Lastnm,Member.Firstnm
0

LVL 17

Expert Comment

ID: 16494984
If you try to calculate months until today, try this

select Member.Memberkey, Member.Membid,Member.Lastnm,Member.Firstnm ,sum(datediff(month,Member.Opfromdt,Member.Opthrudt)) from Member
inner join
(select Memberkey,Currhist ,Opfromdt,
case when Opthrudt>getdate() then getdate()
else Opthrudt
end
as Opthrudt from Membhp where Opfromdt<getdate()) as Membhp
on Membhp.Memberkey =Member.Memberkey
where Member.Membid='x'
group by Member.Memberkey, Member.Membid,Member.Lastnm,Member.Firstnm
0

Author Comment

ID: 16502508
Close to what I am looking for.
The problem I have is that member x may have multiple times that he is enrolled and disenrolled.
So say I need to know how many months between 01/01/2005 and 12/31/2005.
lets say he has two different enrollment ftimes
opfromdt 7/01/2004  opthrudt 02/01/2005
opfromdt 03/01/2005 opthrudt  01/01/2079
now the way the above is written I would get 14 months enrolled.
When what I am looking for should be the 11 months that they were enrolled in 2005.
Does that make sense?

Thanks
0

LVL 17

Accepted Solution

HuyBD earned 2000 total points
ID: 16504066
OK, you need count in specifed duration
thy this.

select Member.Memberkey, Member.Membid,Member.Lastnm,Member.Firstnm ,sum(datediff(month,Member.Opfromdt,Member.Opthrudt)) from Member
inner join
(select Memberkey,Currhist ,
case when Opfromdt<'01/01/2005' then '01/01/2005'
else Opfromdt
end as Opfromdt
,
case when Opthrudt>'12/31/2005' then '12/31/2005'
else Opthrudt
end
as Opthrudt from Membhp where Opfromdt<'12/31/2005' and Opthrudt>'01/01/2005') as Membhp
on Membhp.Memberkey =Member.Memberkey
where Member.Membid='x'
group by Member.Memberkey, Member.Membid,Member.Lastnm,Member.Firstnm
0

Author Comment

ID: 16510862
Perfect.
Thank you for the help
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
###### Suggested Courses
Course of the Month13 days, 16 hours left to enroll