Mellad Qarizada Mellad Qarizada - 4 years ago 155
SQL Question

SQL Query percentage of a column

how to query percentage of students attendance in table below

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

Answer Source

I suggest Sum(Attendance) / Count(Attendance) as a percentage:

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

So, in case of A student we have Sum(Attendance) == 1 + 0 + 0 + 0 = 1 and Count(Attemdance) == 4 and the percentage is 1 / 4 * 100 = 25

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download