-5

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
Mellad Qarizada
  • 129
  • 1
  • 5
  • 16
  • can i plz have the reason of down vote to prevent in future question the mistake – Mellad Qarizada Feb 07 '17 at 06:59
  • http://stackoverflow.com/questions/3061655/getting-percentage-of-count-to-the-number-of-all-items-in-group-by . I think your question has been already resolve in this – DevD Feb 07 '17 at 07:00
  • I think it is not easy to know what the table means. There is no date information. How should I interpret this? How are the rows updated? Isn't the table with many columns for different days and with fixed number of names better for your purpose? – ghchoi Feb 07 '17 at 07:04
  • select sum(Attendance = 0) / sum(Attendance = 1) * 100 from students – Mellad Qarizada Feb 07 '17 at 07:10
  • something like above – Mellad Qarizada Feb 07 '17 at 07:10

1 Answers1

0

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

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215