Comparing two files dates to assign an occurrence number

I have a list of patients (VisitID) who have been restrained.  Some of the patients have multiple eprisodes they are restrained.  The episodes are designated by different order dates and OrderID.

The seconde list are the mandatory 2 hour assessments done by the staff to review the well being of the restrained patients (VisitID) and ActivityDateTime  

What I need to do is compare the Assessment list and assign them to the correct restraint order if the assessment was done (ActivityDateTime) on or after the order date and before a subsequent order date for the same paitent  (VisitID).

I have attached two files of data:  Patients and Assessments.

What is the syntax to create this query?

Thanks

glen
Assessments.xlsx
Patients.xlsx
GPSPOWAsked:
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.

Ioannis ParaskevopoulosCommented:
Hi,

I would love some more data with different assessments and orders, but here is a quick thought:

;WITH PatientsCTE AS
(
  SELECT  *,
          ISNULL((SELECT  MIN(OrderDateTime) 
                  FROM    Patients 
                  WHERE   VisitID = P.VisitID 
                          AND StartEp > P.StartEp)
                  ,StopOrdDate) AS NextOrderDateTime
  FROM    Patients P
)
SELECT  A.*,
        P.OrderID
FROM    PatientsCTE P
        JOIN Assessments A
          ON P.VisitID = A.VisitID
          AND A.ActivityDateTime >= P.OrderDateTime 
          AND A.ActivityDateTime < P.NextOrderDateTime

Open in new window


You may check the results on SQLFiddle.

In the results, the very first assessment is not being returned as it is before the start of the first order. I do not know if it should be returned or not.

Giannis
0

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
GPSPOWAuthor Commented:
Thank you

I was able to modify your suggestion to meet my table criteria.

Glen
0
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 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.