Familiarize with the data that you have:
The first key thing is to understand what data you have. Here in this case, you have four tables
- InsuranceCompanies
- Patient
- Doctors
- Visits
Your goal:
Find the list of all the patients who visited all orthopedists (specialty) associated with their Insurance Companies.
Let’s take a step back and analyze it in smaller pieces:
Generally, the requirements might be a bit overwhelming when you look at them on the whole. Let’s split the requirements into smaller components to understand what you need to do.
- Part a: You need to find the list of doctors, whose speciality is ‘Orthopedist’
- Part b: Find the list of patients who visited doctors identified in #1.
- Part c: Filter the result #2 to find the list of patients and doctors who share the same insurance company.
- Part d: Find out that the patients who visited each one of those Orthopedists who belong to the same insurance company as the patient do.
How to approach:
-
You need to identify your main goal, here in this case to identify the list of patients. So, query the Patient table first.
-
You have the patients, actually all of them but we need to find which of these patients visited the doctors. Let’s not worry about whether the doctor is an Orthopedist or not. We just need the list of patients and the doctors they have visited. There is no mapping between Patient and Doctors table. To find out this information,
Join the Patient table with Visits table on the correct key field.
Then join the output with the Doctors table on the correct key field.
-
If you have done the join correctly, you should now have the list of all the patients and the doctors that they have visited. If you used
LEFT OUTER JOIN
, you will find even the patients who had never visited a doctor. If you usedRIGHT OUTER JOIN
, you will find only the patients who visited a doctor. -
Now, you have all the patients and the doctors whom they have visited. However, the requirement is to find only the doctors who are Orthopedists. So, apply the condition to filter the result to give only the desired result.
-
You have now achieved the requirements as split into smaller components in part a and part b. You still need to filter it by the insurance companies. Here is the tricky part, the requirement doesn’t say that you need to display the insurance company, so we don’t have to use the table InsuranceCompanies. Your next question will
'How am I going to filter the results?'
. Valid point. Find out if any of the three tablesPatient
,Doctor
andVisits
contain the insurance company information.Patient
andDoctors
have a common field. Join that common field to filter the result. -
Find the count of unique Orthopedists that each patient has visited.
-
Here is the part that can be done in many ways, one of the way of doing this would be to add a sub query that would be your fourth column in the output. This sub query would query the table Doctors and filter by speciality = ‘Orthopedist’. In addition to that filter, you also have to filter by matching the insurance company on the inner table with the insurance company id on the Patients table that is on the main query. This subquery will return the count of all the Orthopedists for insurance company id that matches the patient’s data.
-
You should now have the fields
patient id
,patient name
,patients visits count
and thetotal number of Orthopedists in same insurance company
from the sub query. You can then add an outer join that will filter the results from this derived table on the fields wherepatients visits count
matches withtotal number of Orthopedists in same insurance company
. I am not saying this is the best approach. This is one approach that I can think of. -
If you follow the above logic, you should have this.
List of patients who have visited all the doctors
Filtered by only doctors, whose are Orthopedists
Filtered by patients and doctors sharing the same insurance company information.
Again, the whole output is then filtered by the two count fields found inside the derived table output.
The ball is in your court:
- Try it step by step and once you find the answer. Post it here as a separate answer. I will upvote it to compensate for all the downvotes that you got on this question.
I am confident that you can do this easily.
If you stumble…
Don’t hesitate to post your questions as comments to this answer
, Others and I will be glad to assist you.
Disclaimer
I have provided one of the many ways how this logic can be implemented. I am sure that there are many ways to implement this in a far better manner.
Outcome:
Please refer @Ofek Ron’s answer for the correct query that produces the desired output. I didn’t write any part of the query. It was all OP’s effort.
4
solved How can I improve this query to avoid using nested views? [closed]