提问人:Shantha Prasath 提问时间:5/16/2013 最后编辑:Shantha Prasath 更新时间:5/16/2013 访问量:11963
SQL 查询 - 医生和医院 [已关闭]
SQL querying - Doctors and Hospitals [closed]
问:
- 显示治疗过一名以上患者的医生所收取的 doctorid、dname、总费用?
- 显示医院 ID、hname、htype 与它们关联的医生数量最多的医院。
表
表 1 - 患者
patientid
pname
address
amount
ptype
表2 - 医院
hospitalid
hname
htype
表3 - 医生
doctorid
dname
specialization
hospitalid
status
表4 - 计费
billingid
patientid
doctorid
fees
billdate
到目前为止,这就是我所拥有的:
选择 billing.doctorid, sum (fees) as totalfees, doctor.d名称 从 Billing, 医生 哪里 doctor.doctorid = 计费.doctorid 分组依据 billing.doctorid, doctor.d名称 有 最小值 ( billing.patientID ) <> 最大值 ( billing.patientid )
答:
1赞
Barranka
5/16/2013
#1
我会帮助你解决你的第一个问题,第二个问题我会留给你。
- 显示治疗过一名以上患者的医生所收取的 doctorid、dname、总费用?
让我们把这个问题分成几部分:
因此,您首先需要知道哪些医生治疗了不止一名患者。该信息在表中。所以:billing
select doctorId, count(patientId) as patientCount
from (select distinct doctorId, patientId from billing) as a
group by doctorId
having count(patientId)>1;
此查询将仅返回具有多个患者的医生的 ID。请注意,我正在使用子查询来删除 doctor-patient 元组的重复数据。
现在让我们来攻击这个问题的另一部分:每个医生的总费用。同样,该信息在表中:billing
select doctorId, sum(fees) as totalFees
from billing
group by doctorId;
最后,让我们把它们放在一起,并包括医生的信息,这些信息在表格中:doctor
select
d.doctorId, d.doctorName, a.totalFees
from
doctor as d
inner join (
select doctorId, sum(fees) as totalFees
from billing
group by doctorId
) as a on d.doctorId = a.doctorId
inner join (
select doctorId, count(patientId) as patientCount
from (select distinct doctorId, patientId from billing) as a
group by doctorId
having count(patientId)>1;
) as b on d.doctorId = b.doctorId;
希望这会有所帮助
您需要学习和(或)牢记的事项:
- 您需要了解如何关联存储在不同表中的数据。研究如何使用(以及和
INNER JOIN
LEFT JOIN
RIGHT JOIN
) - 您需要了解其工作原理,以及如何使用聚合函数(、、等)。
GROUP BY
sum()
count()
- 你知道如何编写子查询。现在,尝试不仅将它们用于条件,而且将它们用作数据源(包括它们在语句中)
where
from
- 随身携带一份RDBMS参考手册的副本。一本关于SQL的好书也可以帮助你(去书店或图书馆找一本你喜欢的书)。
评论
0赞
Shantha Prasath
5/16/2013
找到一个更简单的答案 select billing.doctorid, sum (fees) as totalfees, doctor.dname from billing, doctor where doctor.doctorid = billing.doctorid group by billing.doctorid, doctor.dname has min ( billing.patientid ) <> max ( billing.patientid )
0赞
Shantha Prasath
5/16/2013
什么是一本关于SQL的好书......我非常需要一个
0赞
Shantha Prasath
5/16/2013
告诉我一本关于sql的好书
0赞
Barranka
5/16/2013
@ShanthaPrasath 您使用的是哪些 RDBMS(MySQL、Oracle、Access)?对于 MySQL,在线参考手册可以覆盖您的背部。如果您需要学习资源,请使用 Google:查找在线教程并使用它们
0赞
Shantha Prasath
5/16/2013
无法解决下一个问题:请帮助 From 关键字缺少错误:select * from hospital where hospitalid = (select hospitalid from doctor group by hospitalid having count ( doctorid ) = (select max ( doctoramt ) from (select count doctorid) as doctoramt from doctor group by hospitalid) as tbltemp));
0赞
Eric J. Price
5/16/2013
#2
看起来你已经得到了答案,但既然我写了......
Select d.doctorID,
d.dName,
Sum(b.fees) [total fees received]
From doctor d
Join billing b
On d.doctorID = b.doctorID
Group By d.doctorID,
d.dName
Having Count(Distinct patientID) > 1
With CTE As
(
Select Rank() Over (Order By Count(d.doctorID) Desc) As priCount,
h.hospitalID,
h.hName,
h.hType,
Count(d.doctorID) As doctors
From hospital h
Join doctor d
On h.hospitalID = d.hospitalID
Group By h.hospitalID,
h.hName,
h.hType
)
Select hosptitalID,
hName,
hType
From CTE
Where priCount = 1
评论