我怎样才能得到这三行的平均数。(SQL)

问题描述 投票:0回答:1

我在网上查到了我需要的三家医院的结果。知道我在想怎么把这三个结果平均在一起,得到一个整体的群体结果。

   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%
sql average
1个回答
0
投票

如果需要得到医院员工的整体成绩。

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

0
投票

如果我没听错的话,你可以再加一级聚合。

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

请注意,子查询在这里并不是严格意义上的必要,它只是避免了重复多次的条件表达式。

© www.soinside.com 2019 - 2024. All rights reserved.