Get last grouped MySql record per year

0

I have the following table

I need to return only the last record of the teaching step sorted by year_level, using Laravel / MySql

Example: 2002 - 15 / 2004 - 16

I'm trying:

$carga_horaria = DB::select(DB::raw('ch.*'))
->from(DB::raw('(SELECT * FROM estudantes_carga_horaria ORDER BY ano_letivo DESC) ch'))
->groupBy('ch.etapa_ensino_id')
->get();
    
asked by anonymous 17.08.2018 / 16:03

2 answers

1
SELECT ano_letivo, max(etapa_ensino_id)  FROM estudantes_carga_horaria GROUP BY ano_letivo ORDER BY  ano_letivo
    
17.08.2018 / 16:15
1

If you want to take the biggest step of teaching each year_letivo the query is this.

SELECT ano_letivo, MAX(etapa_ensino_id) etapa_ensino_id FROM estudantes_carga_horaria GROUP BY ano_letivo ORDER BY ano_letivo DESC

I hope I have helped.

    
17.08.2018 / 16:18