-3
Create Table Doctor(
    doctorID varchar(50) Primary key,
    doctorFName varchar(50),
);

Create Table Appointment(
    appID varchar(50) Primary Key,
    doctorID varchar(50) Foreign Key References Doctor(doctorID),
);

Create Table Payment(
    paymentID varchar(50) Primary Key,
    paymentAmount int,
    appID varchar(50) Foreign Key References Appointment(appID),
);

paymentAmount is also known as payment for each appointment

How can I get the average payment amount of each doctor?

I tried:

SELECT d.doctorID, d.doctorFName, SUM(p.paymentAmount) as AverageDailySalary 
FROM Payment p JOIN Appointment a ON p.appID = a.appID JOIN Doctor d ON a.doctorID = d.doctorID 
ORDER BY d.doctorID 
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Niveth
  • 1
  • 2

1 Answers1

0

That's a simple JOIN..GROUP BY query :

SELECT d.doctorID,d.doctorFName,AVG(p.paymentAmount) as avg_pay
FROM Doctor d
JOIN Appoitment a
 ON(d.doctorID = a.doctorID)
JOIN payment p
 ON(a.appID = p.appID)
GROUP BY d.doctorID,d.doctorFName
sagi
  • 40,026
  • 6
  • 59
  • 84