Catch last grouped Mysql record per year

Asked

Viewed 133 times

0

I have the following table

Tabela Exemplo

I need to return only the last record of the teaching stage ordered by academic year, using Laravel/Mysql

Example: 2002 - 15 / 2004 - 16

I’m trying to:

$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();

2 answers

1


SELECT ano_letivo, max(etapa_ensino_id)  FROM estudantes_carga_horaria GROUP BY ano_letivo ORDER BY  ano_letivo

1

If you want to take the biggest teaching step of each academic year 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.