Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:07:09+00:00 2026-06-03T02:07:09+00:00

I have a table called Visits : ID(PKey,Int) PatiendID(int), DoctorID(int), ExpectedDate(date), ActualDate(date), How can

  • 0

I have a table called Visits:

ID(PKey,Int)
PatiendID(int),
DoctorID(int),
ExpectedDate(date),
ActualDate(date),

How can I write a query that for each patient returns the numbers of repeat visits.

The visit is considered to be repeated if it is less then half of the year from previous visit.

In other words I need for each patient find number of visits between (last visit-6 months) and last visit excluding last visit. Is it possible to write a query without cursors?

And other question. I have Table Patients

ID(int, PKey),Name,Pasport,State

Is it possible for each client from here find the same information? Or it is better union the query below and all clients that are not in visits table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:07:11+00:00Added an answer on June 3, 2026 at 2:07 am

    In other words I need for each patient find number of visits between
    (last visit-6 months) and last visit excluding last visit.

    WITH CTE AS
    (
        SELECT PatientID, MAX(ActualDate) AS LastVisit
        FROM Visits
        GROUP BY PatientID
    )
    SELECT CTE.PatientID, COUNT(1)-1 AS RepeatVisits
    FROM CTE INNER JOIN Visits ON CTE.PatientID = Visits.PatientID
    WHERE ActualDate BETWEEN DATEADD(month, -6, LastVisit) AND LastVisit
    GROUP BY CTE.PatientId
    

    This will group, by patient, all visits in the six months before the last visit. It will give you the COUNT (total) minus one (excluding the last visit).

    Complete example: http://pastebin.com/vpCdASpB

    For the second part (Patients table), see here: http://pastebin.com/nKw0Wqvp

    WITH CTE AS
    (
        SELECT Patients.PatientID, Patients.Name, MAX(ActualDate) AS LastVisit
        FROM Patients LEFT JOIN Visits ON #patient.PatientID = Visits.PatientID
        GROUP BY Patients.PatientID, Patients.Name
    )
    SELECT CTE.PatientID, CTE.Name, COUNT(1)-1 AS RepeatVisits
    FROM CTE LEFT JOIN Visits ON CTE.PatientID = Visits.PatientID
                              AND Visits.ActualDate BETWEEN DATEADD(month, -6, LastVisit) AND LastVisit
    GROUP BY CTE.PatientId, CTE.Name
    ORDER BY PatientID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain me how below query will work.. i have table called employee
I have a table called 'MyTable' that looks like: ID | Item | Type
I have a table called 'survey_product' with the following structure: id int(11) order_id int(11)
I have a table called activities that looks like this id | related_id |
I have an int field in my product table called product_stat which is incremented
Assuming I have table called events with the columns INT id DATETIME start_time DATETIME
In my db I a have table called users. In that table I have
I have table name called Person with following column names P_Id(int), LastName(varchar), FirstName (varchar).
I have a table called visitors with the following values: id ip date time
I have table called location with a field area. This field can contain various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.