Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Calculate visits based on cutoff date

Calculate visits based on cutoff date

Calculate visits based on cutoff date

I have a group of patients participating in a special program. I need to compute the number of hospital visits for each patient before and after they joined this program.

Patient table looks like this:


PatientID      ActiveDate           InactiveDate          PreActiveVisits
111              7/1/2016           12/31/2016                   ?

Visits table looks like this:

PatientID           AdmitDate
111                 1/1/2016
111                 2/1/2016
111                 3/1/2016
111                 8/1/2016 

Need to populate the Patient table with a PreActiveVisits total of 3 but I'm not sure how to approach this without using a cursor.

RE: Calculate visits based on cutoff date

Would this query return the correct data?


Select  Visits.PatientId,
        Count(Visits.AdmitDate) As PreActiveVisits
From    Patient
        Inner Join Visits
          On Patient.PatientId = Visits.PatientId
          And Patient.ActiveDate <= Visits.AdmitDate
Group By Visits.PatientId 

Microsoft SQL Server MVP
My Blogs
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Calculate visits based on cutoff date

Try something like this:


WITH VisitCount (PatientID, VCount) AS
  (SELECT Visits.PatientID, COUNT(AdmitDate) 
     FROM Visits 
       JOIN Patient
         ON Visits.PatientID = Patient.PatientID
     WHERE Visits.AdmitDate < Patient.ActiveDate))

UPDATE Patient
  SET PreActiveVisits = VisitCount.VCount
  WHERE Patient.PatientID = VisitCount.PatientID 


RE: Calculate visits based on cutoff date

That did it - as always you make it look easy. For the update it would be easiest to use this as a cte? Thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close