member months in medical enrollment

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
shayne23dAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuyBDCommented:
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
HuyBDCommented:
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
shayne23dAuthor Commented:
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
HuyBDCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shayne23dAuthor Commented:
Perfect.
Thank you for the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.