1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
select c.Id_Groupe, Debut, Fin , sum(nbhab) as Nombre
from (select g1.Id_Groupe, a.date_validite as Debut, min(b.date_validite) as Fin
from dvp_COMMUNE a inner join dvp_GROUPE_COMMUNE g1
on g1.Id_commune = a.Id_Commune
inner join dvp_GROUPE_COMMUNE g2
on g1.Id_groupe = g2.Id_Groupe
left join dvp_COMMUNE b
on a.date_validite < b.date_validite
and g2.Id_Commune = b.Id_commune
group by g1.Id_Groupe, a.date_validite) c left join dvp_GROUPE_COMMUNE g
on c.Id_groupe = g.Id_Groupe
left join dvp_COMMUNE d
on d.Id_commune = g.Id_commune
and d.date_validite < coalesce(Fin, to_date('01/01/2999', 'DD/MM/YYYY'))
where not exists (select null
from dvp_COMMUNE e
where d.id_commune = e.id_commune
and d.date_validite < e.date_validite
and e.date_validite < coalesce(Fin, to_date('01/01/2999', 'DD/MM/YYYY')))
group by c.Id_Groupe, Debut, Fin ; |