SQL查询列的百分比

时间:2017-02-07 06:55:55

标签: sql

如何查询下表中的学生出勤率百分比

Name    Attendance
A   1
B   0
B   1
B   1
B   1
B   1
B   1
A   0
A   0
A   0

1 个答案:

答案 0 :(得分:0)

我建议Sum(Attendance) / Count(Attendance)为百分比:

  select Name,
         Sum(Attendance) / Count(Attendance) * 100 as Percentage
    from MyTable
group by Name

因此,对于A学生,我们有Sum(Attendance) == 1 + 0 + 0 + 0 = 1Count(Attemdance) == 4,且百分比为1 / 4 * 100 = 25