Query database not using stored procedure - sql

i have a db diagram as shown above.
What I need to do is:
Select a table which is as follows:
PatientId, Emergency.Name or Doctor Names, Patient.Address.
Explanation; I need A query which will return patient details and, if Patient has an emergency also add an emergency, if not select all doctor.Name into one row.
So, example:
So, the first row was built because EmergencyId in table patient was null, while the second row had an emergency Id.
I need the query to simulate this. Using SSRS
Thanks a lot!

Thanks guys, can you at least explain
me how to return this data in separate
rows, so I can union later on?
I believe this will return the data you want broken out into separate rows, returning Emergency if it exists and the Drs if it does not. Good luck!
SELECT Distinct Coalesce(E.Name, D.Name) as VariableColumn,
P.PatientId,
P.Address
FROM Patient P
LEFT JOIN Emergency E
ON P.EmergencyId=E.EmergencyId
LEFT JOIN PatientDoctor PD
ON P.PatientID=PD.PatientId
LEFT JOIN Doctor D
ON PD.DoctorId=D.DoctorId

I can't really debug or work with your tables to check this, but here is a rough stab at something that should get you going:
SELECT P.PatientId, E.Name as 'EmergencyName', D.Name as 'DoctorName', P.Address
FROM Patient P
LEFT JOIN Emergency E ON P.EmergencyId=E.EmergencyId
LEFT JOIN PatientDoctor PD ON P.PatientID=PD.PatientId
LEFT JOIN Doctor D ON PD.DoctorId=D.DoctorId
ORDER BY P.PatientId, E.Name, D.Name, P.Address
Also, I'm no SQL Wizard. This should get the ball rolling, though.
Can you test this query against your data?
Does it return the desired results?
EDIT:
I just saw your comment about needing this layout with multiple values in one cell aligned vertically. That would require sub queries, and would be too much for me to write out.

Related

Trying to figure out a sql query to fetch the right data from multiple tables

So I have a database and I have 4 tables (admin, appointment, doctor, patient).
I only care about appointment, doctor and patient.
Diagram of these 3 tables:
What I want is based on a patients amka(int) to find all his appointments and show his name, surname and doctors name, surname.
I made this query:
(SELECT doctor.name, doctor.surname
FROM public.doctor
INNER JOIN public.appointment ON doctor.username = 'alouisot2')
union
(SELECT patient.name, patient.surname
FROM public.patient
INNER JOIN public.appointment ON patient.amka = '713783001');
The problem with this query is that it will only show appointments between this specific doctor and I just want to get all the doctors names and surnames.
Also I use postgresql.
I was trying to figure it out but I couldn't. I need to restructure the query but I cannot think how I would solve this problem.
If you have any idea on how to do achieve this I would really appreciate it.
Try this - properly join the three tables together (doctor to appointment based on the common username column, appointment to patient on the common amka column), and define the WHERE clause with your desired values:
SELECT
doctor.name, doctor.surname,
patient.name, patient.surname
FROM
public.doctor
INNER JOIN
public.appointment ON doctor.username = appointment.username
INNER JOIN
public.patient ON appointment.amka = patient.amka
WHERE
doctor.username = 'alouisot2'
AND patient.amka = '713783001';
this query just needs simple joins; you seem to have overcomplicated it a lot for some reason...
SELECT
patient.name,
patient.surname,
doctor.name,
doctor.surname
FROM public.patient
INNER JOIN public.appointment
ON patient.amka = appointment.amka
INNER JOIN public.doctor
ON appointment.username = doctor.username
WHERE patient.amka = '713783001'

Is there a SQL statement that will allow me to see results that match from 2 different tables?

I'm joining two tables (Employee and Matrix) and I'm wondering if there is a statement I can use to only show the names of employees that have a matrix attached to their names after joining the Employee and Matrix tables. I thought of possibly using 'Distinct', but I don't believe that would work as I'm trying to only see employees that have a matrix and this information would be coming from two different tables.
Any suggestion is appreciated. Thank you in advance.
I think exists does what you want:
select e.*
from employee e
where exists (select 1 from matrix m where m.employee_id = e.employee_id);
It would definitely work:
SELECT DISTINCT e.name
FROM employee e
INNER JOIN matrix m
ON e.id = m.employee_id

sql multiple attribute join

I'm creating a test database on medical practices. Here is a picture of my er diagram for reference. ER diagram
So basically my question is how to do joins properly. I'm trying to give a list of patients seen from a given practice (e.g practice id 1) but i also want to show the practice name and details. I also want to show a list of the patients and and gp's who work or have been to that practice (keep in mind i have only populated my tables with 10 rows of test data)
I have got so far
select patient.firstname, patient.surname
from patient
Join appointment on patient.patientid = appointment.patientid
where appointment.practiceid IN (1)
ORDER BY firstname;
`How would i also include the practice details and the gp details also who are associated with this practice. What's confusing me is how i add more select statements if they are not from the patient table. Any help would be great!
You can just add more join clauses:
SELECT patient.firstname, patient.surname, practice.name, practice.address
FROM patient
JOIN appointment ON patient.patientid = appointment.patientid
JOIN practice ON appointment.practiceid = practice.practiceid
WHERE appointment.practiceid IN (1)
ORDER BY firstname;
I like aliasing my SQL statements. Saves on typing.
SELECT p.firstname, p.surname, pr.Details
FROM patient p
JOIN appointment a ON p.patientid = a.patientid
JOIN Practice pr ON pr.PracticeId = a.PracticeId
WHERE a.practiceid IN (1)
ORDER BY p.firstname;
I've included how to show the Practice_Name in your query, if you use the same join logic approach for each of the other queries you mention, you will work it out. I think that's a better way to learn rather than solving every question above in one answer.
SELECT PR.Practice_Name, P.firstname, P.surname
FROM patient AS P
INNER JOIN appointment AS A ON P.patientid = A.patientid
INNER JOIN Practice AS PR ON A.practiceid = PR.practiceid
WHERE A.practiceid IN (1)
ORDER BY P.firstname;

PostgreSQL. how to make a query that shows pairs from the same column?

I have 3 tables
Students{sId, name}
Class{cId, cName} and
Enrollment{sId, cId, semester}
A student can enroll in multiple classes. I need to produce a query that displays all the pairs of students (their names and ids )who have completed one or more of the same classes in the same semester. I'm not really sure what I am to SELECT to display because I need to display in a format that shows the pairs
I started by doing the joins and came up with this
FROM Students s
INNER JOIN Enrollment e
ON s.sId = e.sId
INNER JOIN Class c
ON c.cId = e.sId
Then I am not sure what to do.. If someone could point me in the right direction it would be much appreciated. I am very new to SQL and am even struggling to find some keywords to google.
Thanks for any help

How to get the recors with count zero if there are no records

I have three tables like
I want to display the leave types with the count. For that I have written a query like
SELECT VM.vacation_id,
VM.vacation_desc,
isnull(sum(VR.total_hours_applied),0) AS totalCount
FROM EMPTYPE_VACATIONCONFIG VC
LEFT JOIN HR_Vacation_Master VM ON VC.VACATIONID=VM.vacation_id
INNER JOIN HR_Employee_Vacation_Request VR ON VR.vacation_id=VM.vacation_id
WHERE VR.employee_id=156
AND VC.BRANCHID=20
GROUP BY VM.vacation_desc,
VM.vacation_id
my query is working fine and giving results of existed vacationids only. like
I want third leave alos with zero total.
If the employee not applied any leave(in second table), that record not coming in list. I want that record also with the totalCount zero.
How can I do it
This is because of VR.employee_id=156 you are not allowing null row.
You can do that :
SELECT VM.vacation_id,
VM.vacation_desc,
isnull(sum(VR.total_hours_applied),0) AS totalCount
FROM EMPTYPE_VACATIONCONFIG VC
LEFT JOIN HR_Vacation_Master VM ON VC.VACATIONID=VM.vacation_id
LEFT JOIN HR_Employee_Vacation_Request VR
ON VR.vacation_id=VM.vacation_id AND VR.employee_id=156
WHERE VC.BRANCHID=20
GROUP BY VM.vacation_desc,
VM.vacation_id
Leave me a comment if this not works, I have some other ideas.
If one employee didn't apply any leave, there shouldn't have a record with his(or her) employee id in table HR_Employee_Vacation_Request , right? So I think you should use HR_Vacation_Master left outer join table HR_Employee_Vacation_Request .