Como puedo sacar el promedio de una consulta Mysql para poder usarla en Laravel
SELECT start as fecha,
COUNT(IF(active = 0,1,NULL)) as totalir,
COUNT(start) as totalit,
round(COUNT(IF(active = 0,1,NULL)) / COUNT(start) * 100, 2) as porcentaje
FROM incidences
WHERE MONTH(start) = MONTH(CURRENT_DATE())
AND YEAR(start) = YEAR(CURRENT_DATE())
GROUP BY start
Creo que debería ser algo así, aun me estoy iniciando en laravel y no sé como hacer la consulta:
$tri = DB::select('SELECT start as fecha,
COUNT(IF(active = 0,1,NULL)) as totalir,
COUNT(start) as totalit, round(COUNT(IF(active = 0,1,NULL)) / COUNT(start) * 100, 2) as porcentaje
FROM incidences
WHERE MONTH(start) = MONTH(CURRENT_DATE())
AND YEAR(start) = YEAR(CURRENT_DATE())
GROUP BY start');
Esta es la vista, por ahora solo estoy mostrando el porcentaje. Lo que me gustaría es poder mostrar el promedio de la columna porcentaje
Gracias de antemano!
$tri = DB::table->('incidences') ->select('start as date', 'COUNT(IF(active = 0,1,NULL)) as totalir', 'COUNT(start) as totalit', 'round(AVG(COUNT(IF(active = 0,1,NULL)) / COUNT(start) * 100), 2) as porcentaje') ->where(function ($query) { $query->where('MONTH(start)', '=', 'MONTH(CURRENT_DATE())') ->where('YEAR(start)', '=', 'YEAR(CURRENT_DATE())'); })->groupBy('start')->get();