我将如何获得三行的平均值。 (SQL)
How would i get the average of the three rows. (SQL)
我能够得到我需要的三个医院的结果。知道我正在尝试弄清楚如何将三个结果平均在一起以获得整体组结果。
SELECT H.[Hospital Name],
Format(1.0* Avg (Case When s.[PhysicianQuestion]>3 Then 1.0 Else 0 End),'P2') As [Physician
Top Box],
Format(1.0*Avg (Case When s.[NurseQuestion]=4 Then 1.0 Else 0 End),'P2') As [Nurse Top Box],
Format( 1.0*Avg ( Case When s.[FacilityQuestion]=4 Then 1.0 Else 0 end),'P2') As [Facility Top Box]
From surveyresponses as S
Join Visits as V on v.AccountNumber=S.AccountNumber
join Hospitals as H on H.HospitalID=V.HospitalID
Group By [Hospital Name]
Order By [Hospital Name]
Results
Central Hospital 74.04% 76.15% 71.26%
Desert Flats Hospital 67.79% 68.99% 73.96%
Mercy Valley Hospital 74.93% 76.45% 73.88%
如果我没听错,你可以添加另一个聚合级别:
select
format(avg([Physician Top Box]), 'P2') [Avg Physician Top Box],
format(avg([Nurse Top Box]), 'P2') [Avg Nurse Top Box],
format(avg([Facility Top Box]), 'P2') [Avg Facility Top Box]
from (
select
h.[Hospital Name],
avg(case when s.PhysicianQuestion > 3 then 1.0 else 0 end) As [Physician Top Box],
avg(case when s.NurseQuestion = 4 then 1.0 else 0 end) As [Nurse Top Box],
avg(case when s.FacilityQuestion = 4 then 1.0 else 0 end) As [Facility Top Box]
from surveyresponses as s
inner join Visits as v on v.AccountNumber = s.AccountNumber
inner join Hospitals as h on h.HospitalID = v.HospitalID
group by h.[Hospital Name]
) t
请注意,在所有算术计算之后,我将格式移动到外部查询。
另一方面,如果您想要查询返回的每一行的三个列值的平均值,您可以这样做:
select
[Hospital Name],
format([Physician Top Box], 'P2') [Physician Top Box],
format([Nurse Top Box], 'P2') [Nurse Top Box],
format([Facility Top Box], 'P2') [Facility Top Box],
([Physician Top Box] + [Nurse Top Box] + [Facility Top Box]) / 3 [Avg Top Box]
from (
select
h.[Hospital Name],
avg(case when s.PhysicianQuestion > 3 then 1.0 else 0 end) As [Physician Top Box],
avg(case when s.NurseQuestion = 4 then 1.0 else 0 end) As [Nurse Top Box],
avg(case when s.FacilityQuestion = 4 then 1.0 else 0 end) As [Facility Top Box]
from surveyresponses as s
inner join Visits as v on v.AccountNumber = s.AccountNumber
inner join Hospitals as h on h.HospitalID = v.HospitalID
group by h.[Hospital Name]
) t
注意这里子查询不是绝对必要的;它只是避免多次重复条件表达式。
如果需要得到医院工作人员的整体绩效
select [Hospital Name],
format( (([Physician Top Box] + [Nurse Top Box] + [Facility Top Box] )/3), 'P2') AS
[Overall Hospital Performance]
from (
select
h.[Hospital Name],
avg(case when s.PhysicianQuestion > 3 then 1.0 else 0 end) As [Physician Top Box],
avg(case when s.NurseQuestion = 4 then 1.0 else 0 end) As [Nurse Top Box],
avg(case when s.FacilityQuestion = 4 then 1.0 else 0 end) As [Facility Top Box]
from surveyresponses as s
inner join Visits as v on v.AccountNumber = s.AccountNumber
inner join Hospitals as h on h.HospitalID = v.HospitalID
group by h.[Hospital Name]
) t
我能够得到我需要的三个医院的结果。知道我正在尝试弄清楚如何将三个结果平均在一起以获得整体组结果。
SELECT H.[Hospital Name],
Format(1.0* Avg (Case When s.[PhysicianQuestion]>3 Then 1.0 Else 0 End),'P2') As [Physician
Top Box],
Format(1.0*Avg (Case When s.[NurseQuestion]=4 Then 1.0 Else 0 End),'P2') As [Nurse Top Box],
Format( 1.0*Avg ( Case When s.[FacilityQuestion]=4 Then 1.0 Else 0 end),'P2') As [Facility Top Box]
From surveyresponses as S
Join Visits as V on v.AccountNumber=S.AccountNumber
join Hospitals as H on H.HospitalID=V.HospitalID
Group By [Hospital Name]
Order By [Hospital Name]
Results
Central Hospital 74.04% 76.15% 71.26%
Desert Flats Hospital 67.79% 68.99% 73.96%
Mercy Valley Hospital 74.93% 76.45% 73.88%
如果我没听错,你可以添加另一个聚合级别:
select
format(avg([Physician Top Box]), 'P2') [Avg Physician Top Box],
format(avg([Nurse Top Box]), 'P2') [Avg Nurse Top Box],
format(avg([Facility Top Box]), 'P2') [Avg Facility Top Box]
from (
select
h.[Hospital Name],
avg(case when s.PhysicianQuestion > 3 then 1.0 else 0 end) As [Physician Top Box],
avg(case when s.NurseQuestion = 4 then 1.0 else 0 end) As [Nurse Top Box],
avg(case when s.FacilityQuestion = 4 then 1.0 else 0 end) As [Facility Top Box]
from surveyresponses as s
inner join Visits as v on v.AccountNumber = s.AccountNumber
inner join Hospitals as h on h.HospitalID = v.HospitalID
group by h.[Hospital Name]
) t
请注意,在所有算术计算之后,我将格式移动到外部查询。
另一方面,如果您想要查询返回的每一行的三个列值的平均值,您可以这样做:
select
[Hospital Name],
format([Physician Top Box], 'P2') [Physician Top Box],
format([Nurse Top Box], 'P2') [Nurse Top Box],
format([Facility Top Box], 'P2') [Facility Top Box],
([Physician Top Box] + [Nurse Top Box] + [Facility Top Box]) / 3 [Avg Top Box]
from (
select
h.[Hospital Name],
avg(case when s.PhysicianQuestion > 3 then 1.0 else 0 end) As [Physician Top Box],
avg(case when s.NurseQuestion = 4 then 1.0 else 0 end) As [Nurse Top Box],
avg(case when s.FacilityQuestion = 4 then 1.0 else 0 end) As [Facility Top Box]
from surveyresponses as s
inner join Visits as v on v.AccountNumber = s.AccountNumber
inner join Hospitals as h on h.HospitalID = v.HospitalID
group by h.[Hospital Name]
) t
注意这里子查询不是绝对必要的;它只是避免多次重复条件表达式。
如果需要得到医院工作人员的整体绩效
select [Hospital Name],
format( (([Physician Top Box] + [Nurse Top Box] + [Facility Top Box] )/3), 'P2') AS
[Overall Hospital Performance]
from (
select
h.[Hospital Name],
avg(case when s.PhysicianQuestion > 3 then 1.0 else 0 end) As [Physician Top Box],
avg(case when s.NurseQuestion = 4 then 1.0 else 0 end) As [Nurse Top Box],
avg(case when s.FacilityQuestion = 4 then 1.0 else 0 end) As [Facility Top Box]
from surveyresponses as s
inner join Visits as v on v.AccountNumber = s.AccountNumber
inner join Hospitals as h on h.HospitalID = v.HospitalID
group by h.[Hospital Name]
) t