Record count

2

I have a table named cad_faltas , it has the fields:

id int(11)
id_cad_alunos int(11) 
falta varchar(8)
idcurso int(11)

The falta field receives the data AWAY and / or PRESENT. The question is, is there any possibility of doing a count of records that are as "AWAY" for every idcurso and also know the percentage of records with this information?

Example:

  

For the course of "id" 2 there are three registers like "ABSENT" that are   50% of all records for this "id" 2.

    
asked by anonymous 20.09.2017 / 16:07

2 answers

0

If what you need is the number of absences and percentages per course, here's a way to do it:

SELECT
  a.idcurso,
  nmfaltas,
  (nmfaltas * 100) / COUNT(*) percfaltas
FROM
  cad_faltas a
JOIN
  (SELECT idcurso, COUNT(*) nmfaltas FROM cad_faltas where falta = 'AUSENTE' GROUP BY idcurso) b on a.idcurso = b.idcurso
GROUP BY idcurso
  

SQLFiddle

    
20.09.2017 / 18:48
1

Because you need both the quantity of records with missing and present, a where clause becomes unfeasible. What it is possible to do is to calculate the sum of values, to define the number of absent, setting the value 1 when the record is absent or 0 when it is present. The sum will be the number of records missing. Already to calculate the portion that this value represents of the total, you can calculate the average value, also defining the value 1 when it is absent or 0 when it is present. Here's an example:

select 
  sum(case when falta = "AUSENTE" then 1.0 else 0.0 end) as ausentes, 
  100*avg(case when falta = "AUSENTE" then 1.0 else 0.0 end) as parcela,
  idcurso
from cad_faltas 
group by idcurso;
  

See working in SQLFiddle .

    
20.09.2017 / 16:38