Results 1 to 6 of 6
  1. #1
    gstullo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2011
    Posts
    3

    Number of Days between visits

    Good morning,



    I have a table listing all patient visits to a clinic. The data contains variables such as PatientID, VisitNumber, and VisitDate...

    I need a query which returns the number of days between each subsequent visit per patient (Number of days between Visit 1 and Visit 2, Visit 2 and Visit 3, etc.).

    I am sure that there is a simple solution but I and not thinking of it at the moment.

    Any ideas?

    Thanks,

    GST

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You will need a query that contains either a DLookup() function or a nested query to pull the date of the previous visit. Then once you have the date, you can use the datediff() function to get the number of days difference. Below is what the query with nest query would look like. The nested query is shown in red:

    SELECT tblPatientVisits.PatientID, tblPatientVisits.VisitDate, (Select top 1 Q1.VisitDate from tblPatientVisits as Q1 WHERE Q1.patientID= tblPatientVisits.PatientID AND tblPatientVisits.VisitDate>Q1.VisitDate ORDER BY Q1.PatientID, Q1.VisitDate Desc) as PreviousVisit, dateDiff("d",tblPatientVisits.VisitDate, PreviousVisit) as DaySinceLastVisit
    FROM tblPatientVisits;

  3. #3
    gstullo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2011
    Posts
    3
    Thank you for the feedback. Nested queries... This is new to me and is like a light-blub going off over my head. I have had many situations where I have had to create, in the QBE grid, queries which are imported into queries. The result has been many, many queries to display a large summary in one plate. For example, a unique query for each year which sums the number of visits for that year and a large query which pulls all those small queries together to display it in one place.

    It seems that the Nested would circumvent this...

    I assume that is must be written in SQL and cannot be represented in the QBE grid?

    -GST

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I assume that is must be written in SQL and cannot be represented in the QBE grid?
    Yes, the nested query will have to be written via the SQL view.
    You can build the basic parts of the query in the QBE grid and then switch over to SQL view.

  5. #5
    gstullo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2011
    Posts
    3
    Love it!

    Thank you!

  6. #6
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You're welcome

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access for recordig visits on a Daily Basis
    By phineas629 in forum Access
    Replies: 4
    Last Post: 05-13-2011, 11:18 PM
  2. number of days query
    By osuprog in forum Queries
    Replies: 15
    Last Post: 09-17-2010, 01:15 PM
  3. Select Dates between days and two days ago?
    By rstonehouse in forum Queries
    Replies: 4
    Last Post: 08-18-2010, 02:13 AM
  4. Replies: 4
    Last Post: 08-27-2009, 01:21 AM
  5. less than 180 days old
    By techexpressinc in forum Queries
    Replies: 2
    Last Post: 06-04-2009, 04:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums