Return Results if date range is 6 or 12 months from admit date

I have a need to create a report that shows that patients are due for a review based on their admission date. The users need to be able to enter a date range then the report needs to return anyone that is due within that date range for their review.

my table is called tblClients
The fields that need to be returned are tblClients.ClientID, tblClients.FirstName, tblClients.LastName, tblClients.AdmitDate.

so if the users enter a date range of 1/1/2011 and 1/31/2011 then I need all of the patients that are due for their reviews which would be anyone who was admitted in January and or July only

Any help would be greatly appreciated as I need this ASAP!
jonesy_33Asked:
Who is Participating?
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.

cyberkiwiCommented:
Something like this?

declare @startdate datetime
declare @enddate datetime

select @startdate = '20110101', @enddate = '20110131'

select tblClients.ClientID, tblClients.FirstName, tblClients.LastName, tblClients.AdmitDate
from tblClients
where (tblClients.AdmitDate between dateadd(m, -6, @startdate) and dateadd(m, -6, @enddate))
or (tblClients.AdmitDate between dateadd(m, -12, @startdate) and dateadd(m, -12, @enddate))
vinodchCommented:
select tblClients.ClientID, tblClients.FirstName, tblClients.LastName, tblClients.AdmitDate
from tblClients
where  (Month(tblClients.Admitdate) = Month('1/1/2011'))
            Or
            (Month(tblClients.Admitdate) = Month(DateAdd(m,6,'1/1/2011')))
jonesy_33Author Commented:
I have to apologize for the incomplete post. I completely forgot that I am no longer storing the admit date in tblClients I have moved it to tblClientDates since I need to retain a history of the patients admissions discharges and readmits. because of this I created the new table to track their admission history. so the admitdate would be stored in a separate table.

my tables are called tblClients and tblClientDates
The fields that need to be returned are tblClients.ClientID, tblClients.FirstName, tblClients.LastName, tblClientDates.Admitted but since there may be multiple entries I need the most recent which could be done by date or the primary (auto-numbered field) tblCleintDates.ID

FYI when I ran cyberkiwl's solution based on the created date (treating it as the admit date) I did not get a complete set of results. However, when I ran vinodch's solution I did get a complete set returned.

again sorry for the oversight.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

cyberkiwiCommented:
[FYI when I ran cyberkiwl's solution based on the created date (treating it as the admit date) I did not get a complete set of results. However, when I ran vinodch's solution I did get a complete set returned.]

That is because you mentioned a date range.  I wasn't really sure what you wanted to do with a RANGE.  If you only want to specify a month, then a single date will do from which to base the anchor month on, which is what vinodch implicitly assumed.

Try this

select ClientID, FirstName, LastName, Admitted
from
(
select c.ClientID, c.FirstName, c.LastName, d.Admitted,
      rn=row_number() over (partition by c.ClientID order by d.Admitted desc, d.ID desc)
from tblClients c
inner join tblClientDates d on c.clientID=d.clientID
) X
where Month(Admitted) = (Month('2010-01-01')+5)%12 + 1
cyberkiwiCommented:
Sorry, that returns the month Jan/Jul from ANY year. Please use this instead.

select ClientID, FirstName, LastName, Admitted
from
(
select c.ClientID, c.FirstName, c.LastName, d.Admitted,
      rn=row_number() over (partition by c.ClientID order by d.Admitted desc, d.ID desc)
from tblClients c
inner join tblClientDates d on c.clientID=d.clientID
) X
where datediff(month, Admitted, '2011-01-01') in (6,12)
jonesy_33Author Commented:
I am getting some duplicates but it is probably due to it being my development db and it has several entries on the same day that due to testing. is there a way to eliminate the duplicates and only get the one with the highest tblClientDates.ID?
cyberkiwiCommented:
select ClientID, FirstName, LastName, Admitted
from
(
select c.ClientID, c.FirstName, c.LastName, d.Admitted,
      rn=row_number() over (partition by c.ClientID order by d.Admitted desc, d.ID desc)
from tblClients c
inner join tblClientDates d on c.clientID=d.clientID
) X
where datediff(month, Admitted, '2011-01-01') in (6,12)
  and rn = 1   --- forgot to add this. This is what keeps just the latest entry

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
jonesy_33Author Commented:
Thank you for your help and quick responses!
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 2005

From novice to tech pro — start learning today.