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 7884457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:48:18+00:00 2026-06-03T04:48:18+00:00

Find patients who visited two different doctors of the same specialty in the same

  • 0

Find patients who visited two different doctors of the same specialty in the same day.

Example database: Click here to view the sample data script in SQL Fiddle.

CREATE VIEW DistinctVisits AS
SELECT v.vid,v.pid,d.speciality,v.date
FROM Visits v ,Doctors d
WHERE d.did=v.did
GROUP BY v.pid,v.did,v.date;

CREATE VIEW DistinctVisits2 AS
SELECT dv.pid,dv.speciality,dv.date, COUNT(dv.vid) as countv
FROM DistinctVisits dv
GROUP BY dv.pid,dv.speciality,dv.date;

SELECT dv2.pid,dv2.speciality
FROM DistinctVisits2 dv2
WHERE dv2.countv=2;

DROP VIEW DistinctVisits;
DROP VIEW DistinctVisits2;

how do i repeat the same idea but on just one big query?
another solutions would be nice as well, but try to help me improve this one first.

  • 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-03T04:48:19+00:00Added an answer on June 3, 2026 at 4:48 am

    Explanation:

    • You need to find the list of patients who had visited two different doctors of the same speciality on a given day.

    • In this requirement, your Patient table becomes the main table. Let’s query that table first.

    • Now we have the list of patients. We need to get the list of doctors they visited. We cannot simply join the Patients table with Doctors table because there is no column to map the data. We have to use Visits as the intermediate table

    • Add a LEFT OUTER JOIN between Patient and Visits table and join by pid column.

    • We have the patients and the list of their visits but now we need to get the doctors information. So, Add another LEFT OUTER JOIN between Visits and Doctors table and join by did column.

    • We have the patients and doctor visits information. However, we need only the patient’s name, the speciality of the doctor they visited and the date when they visited. So, we will add the columns p.pname, d.speciality and v.date to the SELECT clause and also in the GROUP BY clause. In addition to this we, need all the visits count but there is a catch. We need only the DISTINCT count, in other words we need the count of all the unique doctors that they visited. So, if the patient visited the same doctor twice on a give day, it should be counted as 1. So, adding DISTINCT will help here. Also, the key is to use the correct column name, in this case d.did represents the doctor.

    • We have all the data we need but we need filter only the patients who visited two different doctors on the same day. To do that, HAVING clause comes to our rescue. HAVING is appropriate when you apply GROUP BY. We will use the same COUNT(DISTINCT d.did) to check if the count matches only the value of 2. You can see the results in the output.

    Suggestion:

    • You don’t have to specify the INSERT INTO statement for every value being inserted into a table. You can group them together within parentheses and separate them by commas. The last statement should end with semicolon.

    • The query uses LEFT OUTER JOIN. I used this join to find out all the doctor visits for each patient even if they had never visited a doctor. I just wanted to see the output as I formed the query. You could change this to INNER JOIN, which I think is more appropriate in your scenario.

    • If you don’t want to display the visits count, you can remove it from the SELECT clause.

    Demo:

    Click here to view the demo in SQL Fiddle.

    Script used in explanation:

    SELECT          p.pname
                ,   d.speciality 
                ,   v.date
                ,   COUNT(DISTINCT d.did) AS visitcount
    FROM            Patient p
    LEFT OUTER JOIN Visits v
    ON              v.pid = p.pid
    LEFT OUTER JOIN Doctors d
    ON              d.did = v.did
    GROUP BY        p.pname
                ,   d.speciality
                ,   v.date
    HAVING          COUNT(DISTINCT d.did) = 2
    

    More appropriate script for you:

    SELECT      p.pname
            ,   d.speciality 
            ,   v.date
    FROM        Patient p
    INNER JOIN Visits v
    ON          v.pid = p.pid
    INNER JOIN Doctors d
    ON          d.did = v.did
    GROUP BY    p.pname
            ,   d.speciality
            ,   v.date
    HAVING      COUNT(DISTINCT d.did) = 2
    

    Output:

    PNAME      SPECIALITY    DATE       VISITCOUNT
    ---------  ------------  ---------  -----------
    Loch Ness  Assholes      17/9/2012      2
    Loch Ness  Orthopedist   13/1/2011      2
    

    Create table and insert script:

    create table InsuranceCompanies  (
        cid         int,
        cname       varchar(20),
        primary key (cid)
    );
    
    create table Patient (
        pid         int,
        pname       varchar(20),
        age         int,
        cid         int,
        gender      char,
        primary     key (pid),
        constraint foreign key (cid) 
            references InsuranceCompanies (cid) 
    );
    
    create table Doctors (
        did         int ,
        dname       varchar(20),
        speciality  varchar(20),
        age         int,
        cid         int,
        primary key (did),
        constraint foreign key (cid) 
            references InsuranceCompanies (cid) 
    );
    
    create table Visits(
        vid         int,
        pid         int,
        did         int,
        date        varchar(20),
        primary key (vid),
        constraint foreign key (pid) 
            references Patient (pid) ,
        constraint foreign key (did) 
            references Doctors (did)
    );
    
    INSERT INTO InsuranceCompanies(cid, cname) VALUES
        ( 1111, 'Harel Inc' ),
        ( 2222, 'Clalit Inc' );
    
    INSERT INTO Doctors ( did, dname, speciality, age, cid) VALUES 
        ( 100, 'Jhonny Depp',       'Heart',        42, 1111 ),
        ( 101, 'Tom Tolan',         'Assholes',     62, 1111 ),
        ( 105, 'Yom Tov',           'Assholes',     52, 1111 ),
        ( 102, 'Lauren Jaime',      'Throat',       27, 2222 ),
        ( 103, 'Gomez Flaurence',   'Legs',         37, 2222 ),
        ( 106, 'David Harpaz',      'Orthopedist',  37, 2222 ),
        ( 107, 'David Schwimmer',   'Orthopedist',  37, 2222 ),
        ( 108, 'Sammy Salut',       'Orthopedist',  37, 1111 );
    
    INSERT INTO Patient ( pid, pname, age, cid,gender) VALUES 
        ( 200, 'Jon Gilmour',       25, 2222, 'm' ),
        ( 206, 'Bon Gilmour',       30, 2222, 'm' ),
        ( 205, 'Jon Gilmour',       22, 2222, 'm' ),
        ( 201, 'Bon Jovy',          21, 2222, 'm' ),
        ( 202, 'Loch Ness',         17, 2222, 'f' ),
        ( 203, 'Lilach Sonin',      12, 1111, 'f' ),
        ( 209, 'Lilach Dba',        34, 1111, 'f' ),
        ( 210, 'Paulina Daf',       32, 1111, 'f' ),
        ( 204, 'Gerry Jalor',       23, 1111, 'm' ),
        ( 208, 'Jerrushalem Jalor', 23, 1111, 'm' );
    
    INSERT INTO Visits ( vid, pid, did, date) VALUES 
        ( 300, 204, 100,    '12/12/2012' ),
        ( 301, 204, 101,    '12/12/2012' ),
        ( 302, 204, 101,    '02/01/2012' ),
        ( 303, 202, 101,    '17/09/2012' ),
        ( 311, 202, 105,    '17/09/2012' ),
        ( 304, 203, 102,    '12/12/2011' ),
        ( 312, 202, 106,    '13/06/2012' ),
        ( 314, 202, 107,    '13/01/2011' ),
        ( 313, 202, 108,    '13/01/2011' ),
        ( 305, 204, 102,    '10/10/2011' ),
        ( 306, 201, 100,    '12/01/2012' ),
        ( 316, 204, 108,    '18/05/2012' ),
        ( 307, 202, 100,    '12/07/2012' ),
        ( 315, 203, 108,    '12/07/2012' ),
        ( 310, 204, 103,    '10/04/2012' ),
        ( 308, 203, 102,    '12/12/2011' ),
        ( 309, 200, 101,    '12/12/2012' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Find patients who visited all orthopedists (specialty) associated with their insurance companies. Database: Click
More specifically i have 3 entities(doctors, patients, prescripts). The prescripts entity has two foreign
I have a database with patients, doctors, cabinets and visits. Patients ( id_pac ,
Find Insurance Company with the maximal number of members (patients). Thats the Database and
I find the following example mildly surprising: >>> class Foo: def blah(self): pass >>>
I've checked the various tutorials, and can't find my use case covered. Here is
The following query is designed to find the number of people who went to
I have a query that is designed to find the number of people who
Well I'm creating a search bar to find some patients in my school project,
In the Rails documentation , we find the following example: class Physician < ActiveRecord::Base

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.