SQL查询-医生和医院 是否显示医生ID、dname、治疗过多个患者的医生收到的总费用 显示hospitalid、hname、H与之相关的医生人数最多的医院类型
桌子 表1-患者 表2-医院 表3-医生 表4-账单SQL查询-医生和医院 是否显示医生ID、dname、治疗过多个患者的医生收到的总费用 显示hospitalid、hname、H与之相关的医生人数最多的医院类型,sql,Sql,桌子 表1-患者 表2-医院 表3-医生 表4-账单 到目前为止,这就是我所拥有的: 挑选 billing.doctorid, 合计(费用)作为总费用, 博士 从…起 账单,医生 哪里 doctor.doctorid=billing.doctorid 分组 billing.doctorid, 博士 有 min(billing.patientid)max(billing.patientid)我会帮你回答第一个问题,第二个问题留给你 是否显示医生ID、dname、治疗过多个患者的医生收到的总费用
到目前为止,这就是我所拥有的: 挑选 billing.doctorid, 合计(费用)作为总费用, 博士 从…起 账单,医生 哪里 doctor.doctorid=billing.doctorid 分组 billing.doctorid, 博士 有
min(billing.patientid)max(billing.patientid)我会帮你回答第一个问题,第二个问题留给你
账单中。因此:
select doctorId, count(patientId) as patientCount
from (select distinct doctorId, patientId from billing) as a
group by doctorId
having count(patientId)>1;
此查询将仅返回具有多个患者的医生的ID。请注意,我正在使用子查询对医患元组进行重复数据消除
现在让我们来讨论这个问题的另一部分:每位医生的总费用。同样,该信息在表账单中:
select doctorId, sum(fees) as totalFees
from billing
group by doctorId;
最后,让我们把所有内容放在一起,并将医生信息包括在表医生中:
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;
希望这有帮助
你需要学习和(或)记住的事情:
您需要了解如何关联存储在不同表中的数据。学习如何使用内部联接
(以及左联接
和右联接
)
您需要了解分组方式如何工作,以及如何使用聚合函数(sum()
,count()
,等等)
您知道如何编写子查询。现在,请尝试将它们不仅用于where
条件,而且用作数据源(包括在from
语句中)
随身携带一份RDBMS参考手册。另外,一本关于SQL的好书也可以帮助你(去书店或图书馆找一本你喜欢的书)
看起来你已经得到了答案,但自从我写了
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
如果你的家庭作业有问题,那么展示你尝试了什么,以及你在哪里遇到了困难。好的。着手给我们展示一下。我们会等的。不着急。欢迎来到Stack Overflow。请尽快阅读这篇文章。无论是家庭作业还是非家庭作业,请展示你尝试过的内容,以及你遇到的困难。如果你表现出你已经尝试过,人们会帮助你,但是当你没有表现出你已经尝试过任何事情时,他们不愿意仅仅编写代码来解决你的问题。此外,对于DBMS问题,它通常有助于指定您正在使用的DBMS。尽管这些查询可以用标准SQL来回答,但也存在诸如“您的DBMS是否支持OLAP功能”之类的问题(尽管您可能还没有学过这些功能)。@ShanthaPrasath告诉我们您尝试过什么。发布您的代码和预期结果的示例。我们可以帮助您,但我们需要一个地方来启动您编写此文档的关系数据库管理系统(即SQL Server、Oracle等)?找到了一个更简单的答案选择billing.doctorid,sum(fees)as totalfees,doctor.dname from billing,doctor where.doctorid=billing.doctorid按billing.doctorid分组,doctor.dname有min(billing.patientid)max(billing.patientid)SQL的好书是什么。。。我真的很需要一本给我讲一本好书sql@ShanthaPrasath您正在使用哪些RDBMS(MySQL、Oracle、Access)?对于MySQL,在线参考手册可以覆盖您的背部。如果您需要学习资源,请使用谷歌:查找在线教程并使用它们无法解决下一个问题:请从关键字缺失中获得帮助错误:选择*从hospital where hospitalid=(按hospitalid have count(doctorid)=(选择最大值(doctoramt)从(选择count doctorid)中选择医生组中的hospitalid)作为医生组的医生(由hospitalid)作为tbltemp);
select doctorId, count(patientId) as patientCount
from (select distinct doctorId, patientId from billing) as a
group by doctorId
having count(patientId)>1;
select doctorId, sum(fees) as totalFees
from billing
group by doctorId;
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;
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