×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*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.

# Calculate visits based on cutoff date

## Calculate visits based on cutoff date

(OP)
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:

#### CODE

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

Visits table looks like this:

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?

#### CODE

Select  Visits.PatientId,
From    Patient
Inner Join Visits
On Patient.PatientId = Visits.PatientId
Group By Visits.PatientId 

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"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:

#### CODE

WITH VisitCount (PatientID, VCount) AS
FROM Visits
JOIN Patient
ON Visits.PatientID = Patient.PatientID

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

Tamar

### RE: Calculate visits based on cutoff date

(OP)
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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!