diff --git a/src/libs/routes/location.js b/src/libs/routes/location.js index 39b5b6d981a489433f87138c88a904543fb56974..a7d86f5b356b0818e12c5840b74d5504f132dd1e 100644 --- a/src/libs/routes/location.js +++ b/src/libs/routes/location.js @@ -833,7 +833,67 @@ locationApp.get('/educational/city/:id', (req, res, next) => { .group('escola.tipo_localizacao') .group('escola.ano_censo'); - const queries = [totalSchoolsQry, schoolsPerLocationQry].map((qry) => { + const schoolClassYearQry = squel.select() + .field('MAX(turma.ano_censo)') + .from('turma') + .toString(); + + const enrollmentsQry = squel.select() + .field('municipio.nome', 'name') + .field('COALESCE(SUM(turma.num_matriculas), 0)', 'total') + .field('turma.ano_censo', 'census_year') + .field('\'Total\'', 'adm_dependency') + .from('turma') + .from('municipio') + .where('turma.fk_municipio_id = municipio.pk_cod_ibge') + .where(`turma.fk_municipio_id = ${cityId}`) + .where(`turma.ano_censo IN (${schoolClassYearQry})`) + .where('turma.fk_tipo_turma_id <= 3') + .group('turma.ano_censo') + .group('municipio.nome'); + + const enrollmentsPerAdmDepQry = squel.select() + .field('municipio.nome', 'name') + .field('COALESCE(SUM(turma.num_matriculas), 0)', 'total') + .field('turma.ano_censo', 'census_year') + .field('dependencia_adm.nome', 'adm_dependency') + .from('turma') + .from('dependencia_adm') + .from('municipio') + .where('turma.fk_municipio_id = municipio.pk_cod_ibge') + .where(`turma.fk_municipio_id = ${cityId}`) + .where('turma.fk_dependencia_adm_id = dependencia_adm.pk_dependencia_adm_id') + .where('turma.fk_tipo_turma_id <= 3') + .where(`turma.ano_censo IN (${schoolClassYearQry})`) + .group('turma.ano_censo') + .group('dependencia_adm.nome') + .group('municipio.nome'); + + const enrollmentsPerSchoolLevelQry = squel.select() + .field('municipio.nome', 'name') + .field('COALESCE(SUM(turma.num_matriculas), 0)', 'total') + .field('turma.ano_censo', 'census_year') + .field('dependencia_adm.nome', 'adm_dependency') + .field('etapa_ensino.desc_etapa', 'school_level') + .from('turma') + .from('dependencia_adm') + .from('etapa_ensino') + .from('municipio') + .where('turma.fk_municipio_id = municipio.pk_cod_ibge') + .where(`turma.fk_municipio_id = ${cityId}`) + .where('turma.fk_dependencia_adm_id = dependencia_adm.pk_dependencia_adm_id') + .where('turma.fk_etapa_ensino_id = etapa_ensino.pk_etapa_ensino_id') + .where('turma.fk_tipo_turma_id <= 3') + .where(`turma.ano_censo IN (${schoolClassYearQry})`) + .group('turma.ano_censo') + .group('dependencia_adm.nome') + .group('etapa_ensino.desc_etapa') + .group('municipio.nome') + .order('municipio.nome') + .order('dependencia_adm.nome') + .order('etapa_ensino.desc_etapa'); + + const queries = [totalSchoolsQry, schoolsPerLocationQry, enrollmentsQry, enrollmentsPerAdmDepQry, enrollmentsPerSchoolLevelQry].map((qry) => { return sqlQuery(qry.toString()); });