-- 1/3 : Création de la table temporaire
DROP TABLE IF EXISTS w_dbsetup
;

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    t_dashboard_filter.oid,
    t_dashboard.code as dashboard_code,
    t_indicateur_condition.external_code2
		FROM rh.t_dashboard_filter
		LEFT JOIN rh.t_dashboard ON t_dashboard.oid = dashboard_id
		LEFT JOIN rh.t_indicateur_condition ON t_indicateur_condition.oid = condition_id
		ORDER BY 2, 3 LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    dashboard_code,
    external_code2
)
VALUES
(1, 'CTI_EGAPRO', 'CTI_AGE#LS#AGE:CTI_AGE:S1'),
(1, 'CTI_EGAPRO', 'CTI_AGE#LS#AGE:CTI_AGE:S3'),
(1, 'CTI_EGAPRO', 'CTI_AGE#LS#AGE:CTI_AGE:S6'),
(1, 'CTI_EGAPRO', 'CTI_SEXE#DT#1'),
(1, 'CTI_EGAPRO', 'CTI_SEXE#DT#2')
;

-- 3/3 : Màj de la table iCTI
TRUNCATE rh.t_dashboard_filter;

INSERT INTO rh.t_dashboard_filter(dashboard_id,condition_id)
	SELECT
		t_dashboard.oid,
		t_indicateur_condition.oid
	FROM w_dbsetup
	LEFT JOIN rh.t_dashboard ON t_dashboard.code = w_dbsetup.dashboard_code
	LEFT JOIN rh.t_indicateur_condition ON t_indicateur_condition.external_code2 = w_dbsetup.external_code2
;