CREATE VIEW votes_deputes AS WITH votants AS ((SELECT 'pour' AS "position", _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, NULL AS cause FROM scrutins.scrutin_groupe_pours_votant) UNION ALL ((((SELECT 'pour', _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, NULL FROM scrutins.scrutin_groupe_pour_votant) UNION ALL (SELECT 'contre', _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, NULL FROM scrutins.scrutin_groupe_contres_votant)) UNION ALL ((SELECT 'contre', _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, NULL FROM scrutins.scrutin_groupe_contre_votant) UNION ALL (SELECT 'abstention', _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, NULL FROM scrutins.scrutin_groupe_abstentions_votant))) UNION ALL ((SELECT 'abstention', _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, NULL FROM scrutins.scrutin_groupe_abstention_votant) UNION ALL (SELECT 'non-votant', _racine_id, _parent_id, acteur_ref, mandat_ref, par_delegation, cause_position_vote FROM scrutins.scrutin_groupe_non_votants_votant)))), mises_au_point AS ((SELECT 'pour' AS mise_au_point, _racine_id, acteur_ref FROM scrutins.scrutin_pours_votant) UNION ALL ((((SELECT 'pour', _racine_id, acteur_ref FROM scrutins.scrutin_mise_au_point_pour_votant) UNION ALL (SELECT 'pour', _racine_id, acteur_ref FROM scrutins.scrutin_dysfonctionnement_pour_votant)) UNION ALL ((SELECT 'contre', _racine_id, acteur_ref FROM scrutins.scrutin_contres_votant) UNION ALL (SELECT 'contre', _racine_id, acteur_ref FROM scrutins.scrutin_mise_au_point_contre_votant))) UNION ALL (((SELECT 'contre', _racine_id, acteur_ref FROM scrutins.scrutin_dysfonctionnement_contre_votant) UNION ALL (SELECT 'abstention', _racine_id, acteur_ref FROM scrutins.scrutin_abstentions_votant)) UNION ALL ((SELECT 'non-votant', _racine_id, acteur_ref FROM scrutins.scrutin_non_votants_votant) UNION ALL (SELECT 'non-votant', _racine_id, acteur_ref FROM scrutins.scrutin_non_votants_volontaires_votant))))), map1 AS (SELECT _racine_id, acteur_ref, "first"(mise_au_point) AS mise_au_point FROM mises_au_point GROUP BY ALL)SELECT s.uid AS scrutin_uid, s.legislature, s.numero, s.date_scrutin, COALESCE(v.acteur_ref, m.acteur_ref) AS acteur_uid, v.mandat_ref AS mandat_uid, g.organe_ref AS groupe_uid, v."position", v.par_delegation, v.cause AS cause_position, g.vote_position_majoritaire AS position_groupe, ((v."position" IN ('pour', 'contre', 'abstention')) AND (v."position" != g.vote_position_majoritaire)) AS contre_groupe, m.mise_au_point FROM votants AS v INNER JOIN scrutins.scrutin_groupe AS g ON ((g._id = v._parent_id)) FULL JOIN map1 AS m ON (((m._racine_id = v._racine_id) AND (m.acteur_ref = v.acteur_ref))) INNER JOIN scrutins.scrutin AS s ON ((s._id = COALESCE(v._racine_id, m._racine_id)));