1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| select c.Id_Groupe,
case when Debut < to_date('14/05/2006','DD/MM/YYYY')
then to_date('14/05/2006','DD/MM/YYYY')
else Debut
end as Debut,
case when coalesce(Fin, to_date('01/01/2999', 'DD/MM/YYYY')) > to_date('30/09/2006','DD/MM/YYYY')
then to_date('30/09/2006','DD/MM/YYYY')
else Fin
end as 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
where a.date_validite <= to_date('30/09/2006','DD/MM/YYYY')
group by g1.Id_Groupe, a.date_validite
having coalesce(min(b.date_validite), to_date('01/01/2999', 'DD/MM/YYYY')) > to_date('14/05/2006','DD/MM/YYYY')) 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; |
Partager