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.
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
Patienttable 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 JOINbetween 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 JOINbetween 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.specialityandv.dateto 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, addingDISTINCTwill 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 withsemicolon.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 toINNER 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:
More appropriate script for you:
Output:
Create table and insert script: