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

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    	t_classes_sections.code AS section_code, 
    	t_anciennetes_annee.code as to_code, 
    	t_listes_tables.code as table_code, 
    	t_classes.sequence as classe_sequence 
    FROM 
    	rh.t_classes_sections_elements, 
    	rh.t_classes_sections, 
    	rh.t_classes, 
    	rh.t_listes_tables, 
    	rh.t_anciennetes_annee
    WHERE 
    	t_classes_sections_elements.section_id = t_classes_sections.oid
    	AND t_classes_sections.classe_id = t_classes.oid 
    	AND t_classes.is_cti = 1
    	AND table_id = t_listes_tables.oid
    	AND t_listes_tables.code = 'ANC'
    	AND t_classes_sections_elements.to_id = t_anciennetes_annee.oid
    ORDER BY 
    	t_classes.sequence, 
    	t_classes_sections.code,
    	t_anciennetes_annee.code
     LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    section_code,
    to_code,
    table_code,
    classe_sequence
)
VALUES
('S1', '00', 'ANC', 9),
('S1', '01', 'ANC', 9),
('S1', '02', 'ANC', 9),
('S1', '03', 'ANC', 9),
('S1', '04', 'ANC', 9),
('S1', '05', 'ANC', 9),
('S1', '06', 'ANC', 9),
('S1', '07', 'ANC', 9),
('S1', '08', 'ANC', 9),
('S1', '09', 'ANC', 9),
('S2', '10', 'ANC', 9),
('S2', '11', 'ANC', 9),
('S2', '12', 'ANC', 9),
('S2', '13', 'ANC', 9),
('S2', '14', 'ANC', 9),
('S2', '15', 'ANC', 9),
('S2', '16', 'ANC', 9),
('S2', '17', 'ANC', 9),
('S2', '18', 'ANC', 9),
('S2', '19', 'ANC', 9),
('S2', '20', 'ANC', 9),
('S3', '21', 'ANC', 9),
('S3', '22', 'ANC', 9),
('S3', '23', 'ANC', 9),
('S3', '24', 'ANC', 9),
('S3', '25', 'ANC', 9),
('S3', '26', 'ANC', 9),
('S3', '27', 'ANC', 9),
('S3', '28', 'ANC', 9),
('S3', '29', 'ANC', 9),
('S3', '30', 'ANC', 9),
('S4', '31', 'ANC', 9),
('S4', '32', 'ANC', 9),
('S4', '33', 'ANC', 9),
('S4', '34', 'ANC', 9),
('S4', '35', 'ANC', 9),
('S4', '36', 'ANC', 9),
('S4', '37', 'ANC', 9),
('S4', '38', 'ANC', 9),
('S4', '39', 'ANC', 9),
('S4', '40', 'ANC', 9),
('S4', '41', 'ANC', 9),
('S4', '42', 'ANC', 9),
('S4', '43', 'ANC', 9),
('S4', '44', 'ANC', 9),
('S4', '45', 'ANC', 9),
('S4', '46', 'ANC', 9),
('S4', '47', 'ANC', 9),
('S4', '48', 'ANC', 9),
('S4', '49', 'ANC', 9),
('S4', '50', 'ANC', 9)
;

-- 3/3 : Màj de la table iCTI
INSERT INTO rh.t_classes_sections_elements(section_id, to_id)     
	SELECT
		t_classes_sections.oid,
		t_anciennetes_annee.oid 
	FROM w_dbsetup
		JOIN rh.t_listes_tables ON t_listes_tables.code = w_dbsetup.table_code
		JOIN rh.t_classes ON 1=1
			AND t_classes.table_id = t_listes_tables.oid
			AND t_classes.sequence = w_dbsetup.classe_sequence
		JOIN rh.t_classes_sections ON 1=1
			AND t_classes_sections.classe_id = t_classes.oid
			AND t_classes_sections.code = w_dbsetup.section_code
		JOIN rh.t_anciennetes_annee ON t_anciennetes_annee.code = w_dbsetup.to_code
	WHERE t_classes_sections.oid::text || '-' || t_anciennetes_annee.oid::text NOT IN 
			(
			SELECT section_id::text || '-' || to_id::text 
			FROM rh.t_classes_sections_elements
			)
;