-- requete sur date naissance
insert into coregistration.export_auto_non_eligible (export_auto_non_eligible_affichage)
select coregistration_coregistration_affichage 
from coregistration 
where coregistration_campagne_annonceur = ID
and coregistration_champ = 12
and coregistration_eligible = 1
and EXTRACT(YEAR FROM age(now()::date, general.cast_to_date(coregistration_valeur))) < 18;
-- on passe les lignes traitees a exportable 3 pour ne plus les selectionner
update coregistration set coregistration_eligible = 3
where coregistration_coregistration_affichage in
(
select coregistration_coregistration_affichage
from coregistration
where coregistration_campagne_annonceur = ID
and coregistration_champ = 12
and coregistration_eligible = 1
and EXTRACT(YEAR FROM age(now()::date, general.cast_to_date(coregistration_valeur))) < 18
);
-- fin requete date naissance

-- requete sur ip
insert into coregistration.export_auto_non_eligible (export_auto_non_eligible_affichage)
select coregistration_coregistration_affichage
from coregistration
where coregistration_campagne_annonceur = ID
and coregistration_champ = 13
and coregistration_eligible = 1
and (select name from general.geoip_country(general.cast_to_inet(coregistration_valeur))) != 'France';
-- on passe les lignes traitees a exportable 3 pour ne plus les selectionner
update coregistration set coregistration_eligible = 3
where coregistration_coregistration_affichage in
(
select coregistration_coregistration_affichage
from coregistration
where coregistration_campagne_annonceur = ID
and coregistration_champ = 13
and coregistration_eligible = 1
and (select name from general.geoip_country(general.cast_to_inet(coregistration_valeur))) != 'France'
);
-- fin requete ip

-- requete sur cp
insert into coregistration.export_auto_non_eligible (export_auto_non_eligible_affichage)
select coregistration_coregistration_affichage
from coregistration
where coregistration_campagne_annonceur = ID
and coregistration_champ = 6
and coregistration_eligible = 1
and substring(coregistration_valeur from 1 for 2) not in ('69','75');
-- on passe les lignes traitees a exportable 3 pour ne plus les selectionner
update coregistration set coregistration_eligible = 3
where coregistration_coregistration_affichage in
(
select coregistration_coregistration_affichage
from coregistration
where coregistration_campagne_annonceur = ID
and coregistration_champ = 6
and coregistration_eligible = 1
and substring(coregistration_valeur from 1 for 2) not in ('69','75')
);
-- fin requete cp

-- retraitement des reponses en attente pour forcer le bon format date
update coregistration.coregistration_reponse
set coregistration_reponse_id_reponse = to_char(general.cast_to_date(coregistration_reponse_id_reponse), 'DD/MM/YYYY')
from coregistration.coregistration
where coregistration_coregistration_affichage = coregistration_reponse_affichage
and coregistration_reponse_id_question = ID_QUESTION
and coregistration_campagne_annonceur = ID_CAMPAGNE
and coregistration_champ = 1
and coregistration_eligible = 1;
-- requete sur sous question en fonction de la valeur de la reponse
insert into coregistration.export_auto_non_eligible (export_auto_non_eligible_affichage)
select coregistration_reponse_affichage
from coregistration.coregistration_reponse
inner join coregistration.coregistration on coregistration_coregistration_affichage = coregistration_reponse_affichage and coregistration_champ = 1 -- jointure sur coreg
where coregistration_reponse_id_question = ID_QUESTION
and coregistration_campagne_annonceur = ID_CAMPAGNE
and coregistration_reponse_id_reponse = '01/01/1900' 
and coregistration_eligible = 1;
-- on passe les lignes traités à exportable 3 pour ne plus les selectionner
update coregistration set coregistration_eligible = 3
where coregistration_coregistration_affichage in
(
select coregistration_reponse_affichage
from coregistration.coregistration_reponse
inner join coregistration.coregistration on coregistration_coregistration_affichage = coregistration_reponse_affichage and coregistration_champ = 1 -- jointure sur coreg
where coregistration_reponse_id_question = ID_QUESTION
and coregistration_campagne_annonceur = ID_CAMPAGNE
and coregistration_reponse_id_reponse = '01/01/1900' 
and coregistration_eligible = 1
);
-- fin requete sous question



-- requete sur sous question en fonction question_id / reponse_id et campagne_annonceur_id
insert into coregistration.export_auto_non_eligible (export_auto_non_eligible_affichage)
select coregistration_reponse_affichage
from coregistration.coregistration_reponse
inner join coregistration.coregistration on coregistration_coregistration_affichage = coregistration_reponse_affichage and coregistration_champ = 1 -- jointure sur coreg
inner join coregistration.association_coregistration_question on association_coregistration_question_id_question = coregistration_reponse_id_question
where coregistration_reponse_id_question = ID_QUESTION
and coregistration_reponse_id_reponse = 'ID_REPONSE'
and coregistration_campagne_annonceur = ID_CAMPAGNE
and coregistration_eligible = 1;
-- on passe les lignes traités à exportable 3 pour ne plus les selectionner
update coregistration set coregistration_eligible = 3
where coregistration_coregistration_affichage in
(
select coregistration_reponse_affichage
from coregistration.coregistration_reponse
inner join coregistration.coregistration on coregistration_coregistration_affichage = coregistration_reponse_affichage and coregistration_champ = 1 -- jointure sur coreg
inner join coregistration.association_coregistration_question on association_coregistration_question_id_question = coregistration_reponse_id_question
where coregistration_reponse_id_question = ID_QUESTION
and coregistration_reponse_id_reponse = 'ID_REPONSE'
and coregistration_campagne_annonceur = ID_CAMPAGNE
and coregistration_eligible = 1
); 
-- fin requete sous question



