A)我有一张医生表。每位医生都有一个独特的用户名,以及评级和专业。还有一张手术表。它包含(除其他外) 具有医生唯一用户名的属性。它不是唯一的。
我正在尝试创建一个显示每个专业的表格。数据:
Speciality | Avg. Rating for Each Speciality | # of surgeries performed by Speciality
(第一栏只是专业名称)
到目前为止我最好的:
SELECT D.Speciality,t1.Avg, t0.CPT FROM DOCTOR as D
INNER JOIN(
SELECT AVG(AvgRating) as Avg, Speciality
FROM Doctors as D1
GROUP BY D1.Speciality
) as t1 ON D.Speciality=t1.Speciality
INNER JOIN(
SELECT COUNT(CPT) as CPT, Speciality
FROM PERFORM_SURGERY AS P
GROUP BY P.Speciality
)t0 ON t0.SPECIALITY=D.SPECIALITY
;
B) doctor 表包含唯一的用户名和非唯一的名称。还有一个处方表。它包含医生的用户名和患者的用户名。 现在还有3个我们需要的表。一个是 Req_surgery ,手术和访问。 Req_surgery 包含Doctor的用户名和CPT(CPT是唯一的)。 手术表有一个CPT的主键,它还包含手术费用。 访问包含医生的用户名和患者的用户名以及访问费用。
我想要一张表格:
Doctor Name | No of Patients Seen | No of Prescriptions written | Total Billing
列出每个医生的姓名和所需信息。
到目前为止,我有:
SELECT t1.name, t3.Visits, t2.Prescriptions, t4.money
FROM DOCTOR as D
INNER JOIN(
SELECT (FName + LName) as name, Username
FROM DOCTOR
) t1 ON t1.userName=D.username
INNER JOIN(
SELECT DUsername, COUNT(DateVisit) AS Prescriptions
FROM PRESCRIPTION
WHERE MONTH(DateVisit) = $month
AND YEAR(DateVisit)=$year
)t2 ON D.Username = t2.DUsername
INNER JOIN(
SELECT COUNT(DATEVISIT) as Visits, DUsername
FROM VISIT as V
WHERE MONTH(DateVisit) = $month
AND YEAR(DateVisit)=$year
)t3 ON D.Username=t3=DUsername
我甚至没有完成,因为(a)我知道我的错误,我不知道如何解决它,(b)我不知道如何计算每位医生的收入。 / p>
Doctor拥有Username的主键。 手术是CPT的主要关键。 Req_Surgery具有引用Doctor(用户名)的Doctor's Username(以下称DU)的外键。
答案 0 :(得分:0)
SELECT (FName + LName) as name, t3.Visits, t2.Prescriptions, t4.money
FROM DOCTOR as D
INNER JOIN(
SELECT DUsername, COUNT(DateVisit) AS Prescriptions
FROM PRESCRIPTION
WHERE MONTH(DateVisit) = $month
AND YEAR(DateVisit)=$year
GROUP BY DUsername
)t2 ON D.Username = t2.DUsername
INNER JOIN(
SELECT COUNT(DATEVISIT) as Visits, DUsername
FROM VISIT as V
WHERE MONTH(DateVisit) = $month
AND YEAR(DateVisit)=$year
GROUP BY DUsername
)t3 ON D.Username=t3.DUsername
我添加了DUsername分组以获取每位医生的数字。我也删除了t1,因为实际上它与D
相同