mSSQL,SQL视图,选择,百分比查询

时间:2017-07-06 00:04:51

标签: sql sql-server select percentage calculation

所以这是我需要满足的要求: 可持续发展教育中所有学校的汇总数据,按

分组
  

SchoolDistrict.SchoolDistrictID

(获取与上述学区情景相同的数据,然后按地区添加分组,过滤到

  

EducationServiceDistrict。 EducationServiceDistrictID

) 还计算通过,失败和未经测试的百分比

如何计算通过,失败和未经测试的百分比? 这是我到目前为止所写的查询。

CREATE VIEW district_map AS  
 SELECT * and SchoolDistrictID, 
        EducationServiceDistrict
   FROM SchoolDistrict_View  
         and SchoolDistrict, 
        EducationServiceDistrict
  GROUP BY EducationServiceDistrict.EducationServiceDistrictID  
  ORDER BYLeadWaterTestLocation.PassFail

1 个答案:

答案 0 :(得分:0)

这是如何解决这些问题的一般概念 - 如果您了解这个简化版本,您将能够解决您的问题。

    select d.districtName,
    s.studentCount,
    case when s.studentCount=0 then 0 else s.passed / s.studentCount * 100 end as PassedPct
    from district d 
    join (select districtId,
          sum(studentCount) as studentCount,
          sum(passed) as passed
          from schools
          group by districtId) as s
    on d.districtId = s.districtId
    order by d.districtName