Hello everyone, I'm beginner in SQL Server
I have three tables in Database hospital
- PatientFiles
- OtherServices
- PatientDeposit
Two queries to display my result
Query Number One. Display PatientFilesID, TotalOtherServices
SELECT pf.ID AS PatientFileID, SUM(os.Quantum * os.Price) AS TotalOtherServices FROM PatientsFiles pf INNER JOIN OtherServices os ON pf.ID = os.Patient_File_ID WHERE pf.ID = '14' GROUP BY pf.ID
It is true result
PatientFileID | TotalOtherServices
14 194.00
Query Number Two. Display PatientFilesID, TotalPatientDeposit
SELECT pd.Patient_File_ID AS PatientFileID, SUM(pd.Deposit) AS TotalPatientDeposit FROM PatientsDeposits pd WHERE pd.Patient_File_ID = '14' GROUP BY pd.Patient_File_ID
It is true result
PatientFileID | TotalPatientDeposit
14 450.00
My very tired to mix two queries
SELECT pf.ID AS PatientFileID, SUM(os.Quantum * os.Price) AS TotalOtherServices, SUM(pd.Deposit) AS TotalPatientDeposit FROM PatientsFiles pf INNER JOIN OtherServices os ON pf.ID = os.Patient_File_ID INNER JOIN PatientsDeposits pd ON pf.ID = pd.Patient_File_ID WHERE pf.ID = '14' GROUP BY pf.ID
It is false result
PatientFileID | TotalOtherServices | TotalPatientDeposit
14 582.00 1350.00
Thank you for help me in advance