mSSQL、SQL查看、select、百分比查询
mSSQL, SQL view, select, percentage query
所以这是我需要满足的要求:
ESD 中所有学校的汇总数据,按
分组
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
这是解决这些问题的一般思路 - 如果您理解这个简化版本,您将能够解决您的问题。
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
所以这是我需要满足的要求: ESD 中所有学校的汇总数据,按
分组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
这是解决这些问题的一般思路 - 如果您理解这个简化版本,您将能够解决您的问题。
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