查找支付给每位医生的平均金额

时间:2016-09-06 06:30:37

标签: mysql sql multiple-tables

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也称为每次约会的付款

如何获得每位医生的平均付款金额?

我试过了:

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 

1 个答案:

答案 0 :(得分:0)

这是一个简单的JOIN..GROUP BY查询:

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