【问题标题】:How would i get the average of the three rows. (SQL)我将如何获得三行的平均值。 (SQL)
【发布时间】:2020-05-09 20:30:11
【问题描述】:

我能够得到我需要的三个医院的结果。知道我试图弄清楚如何将三个结果平均在一起以获得整体组结果。

   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 tsql group-by average


【解决方案1】:

如果需要得到医院工作人员的整体表现

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

【讨论】:

    【解决方案2】:

    如果我正确地关注了你,你可以添加另一个级别的聚合:

    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
    

    请注意,这里的子查询不是绝对必要的;它只是避免了多次重复条件表达式。

    【讨论】:

    • 您好,谢谢您的帮助,我正在尝试从医院获得平均结果的输出,例如,中心医院的结果是 74.04% 76.15% 71.26% 我想将这些结果加在一起获得综合分数
    • @LamarReynolds:好的,我明白了。请参阅我的回答中的第二个查询。
    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 2012-03-18
    • 2022-08-14
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2015-09-11
    相关资源
    最近更新 更多