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

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    	CASE WHEN t_etude_1.oid = 0 THEN t_etude_1.oid ELSE 1 END AS oid,
    	t_etude_1.code, 
    	t_etude_1.texte, 
    	t_etude_1.texte_court, 
    	0 AS taux_cible_2008, 
    	0 AS taux_cible_2009, 
    	0 AS taux_cible_2010, 
    	0 AS taux_cible_2011,  
    	t_ghm_1.code as ghm_1_code,
    	t_ghm_2.code as ghm_2_code,
    	t_ghm_3.code as ghm_3_code
    FROM 
    	pmsi.t_etude_1, 
    	pmsi.t_ghm t_ghm_1, 
    	pmsi.t_ghm t_ghm_2, 
    	pmsi.t_ghm t_ghm_3 
    WHERE 
    	t_ghm_1.oid = ghm_1_id 
    	AND t_ghm_2.oid = ghm_2_id 
    	AND t_ghm_3.oid = ghm_3_id
    ORDER BY t_etude_1.code LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    code,
    texte,
    texte_court,
    taux_cible_2008,
    taux_cible_2009,
    taux_cible_2010,
    taux_cible_2011,
    ghm_1_code,
    ghm_2_code,
    ghm_3_code
)
VALUES
(0, '**', 'TOTAL', 'TOTAL', 0, 0, 0, 0, '******', '******', '******'),
(1, '01', 'Libérations du canal carpien et d''autres nerfs superficiels', 'Canal carpien', 0, 0, 0, 0, '01C13Z', '24C54Z', '******'),
(1, '02', 'Interventions sur le cristallin', 'Cristallin', 0, 0, 0, 0, '02C05Z', '24C03Z', '******'),
(1, '03', 'Amygdalectomies et/ou adénoïdectomies', 'Amygdalectomies', 0, 0, 0, 0, '03C10Z', '24C05Z', '******'),
(1, '04', 'Ligatures de veines et éveinages', 'Ligatures de veines', 0, 0, 0, 0, '05C17V', '24C09Z', '******'),
(1, '05', 'Interventions réparatrices pour hernies', 'Hernies', 0, 0, 0, 0, '06C12V', '24C11Z', '******'),
(1, '06', 'Résections osseuses localisées et/ou ablations de matériel de fixation interne', 'Résections osseuses', 0, 0, 0, 0, '08C13Z', '08C14Z', '24C15Z'),
(1, '07', 'Interventions sur la main', 'Interventions sur la main', 0, 0, 0, 0, '08C17Z', '24C55Z', '******'),
(1, '08', 'Interventions sur la main', 'Interventions sur la main', 0, 0, 0, 0, '08C18V', '24C46Z', '******'),
(1, '09', 'Arthroscopies', 'Arthroscopies', 0, 0, 0, 0, '08C19Z', '24C17Z', '******'),
(1, '10', 'Interventions sur les testicules', 'Testicules', 0, 0, 0, 0, '12C06Z', '24C23Z', '******'),
(1, '11', 'Circoncisions', 'Circoncisions', 0, 0, 0, 0, '12C08Z', '24C24Z', '******'),
(1, '12', 'Interventions sur la vulve, le vagin ou le col utérin', 'Vulve, vagin, col utérin', 0, 0, 0, 0, '13C08V', '24C27Z', '******'),
(1, '13', 'Dilatations et curetages, conisations', 'Dilatations, curetages, conisations', 0, 0, 0, 0, '13C12Z', '24C28Z', '******'),
(1, '14', 'Ligature ou intervention système utéroannexiel', 'Ligatures système utéroannexiel', 0, 0, 0, 0, '13C10Z', '24C50Z', '******'),
(1, '15', 'Affections de la bouche et des dents avec certaines extractions, réparations et prothèses dentaires', 'Bouche, dents', 0, 0, 0, 0, '03K02Z', '24K38Z', '******')
;

-- 3/3 : Màj de la table iCTI
UPDATE pmsi.t_etude_1 SET 
	code = w_dbsetup.code
FROM w_dbsetup
WHERE 
	t_etude_1.oid = w_dbsetup.oid 
	AND w_dbsetup.oid = 0;


UPDATE pmsi.t_etude_1 SET 
	texte = w_dbsetup.texte, 
	texte_court = w_dbsetup.texte_court,
	ghm_1_id = t_ghm_1.oid,
	ghm_2_id = t_ghm_2.oid,
	ghm_3_id = t_ghm_3.oid
FROM 
	w_dbsetup, 
	pmsi.t_ghm t_ghm_1, 
	pmsi.t_ghm t_ghm_2, 
	pmsi.t_ghm t_ghm_3 
WHERE 
	t_etude_1.code = w_dbsetup.code
	AND t_ghm_1.code = ghm_1_code 
	AND t_ghm_2.code = ghm_2_code 
	AND t_ghm_3.code = ghm_3_code;


INSERT INTO pmsi.t_etude_1(oid, code, texte, texte_court)
SELECT oid, code, texte, texte_court 
FROM w_dbsetup 
WHERE 
	oid = 0 
	AND oid NOT IN (SELECT oid FROM pmsi.t_etude_1);


INSERT INTO pmsi.t_etude_1(
	code, texte, 
	texte_court, 
	ghm_1_id, 
	ghm_2_id, 
	ghm_3_id)
SELECT 
	w_dbsetup.code, 
	w_dbsetup.texte, 
	w_dbsetup.texte_court, 
	t_ghm_1.oid, 
	t_ghm_2.oid, 
	t_ghm_3.oid
FROM 
	w_dbsetup, 
	pmsi.t_ghm t_ghm_1, 
	pmsi.t_ghm t_ghm_2, 
	pmsi.t_ghm t_ghm_3  
WHERE 
	w_dbsetup.oid <> 0 
	AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_etude_1)
	AND t_ghm_1.code = ghm_1_code 
	AND t_ghm_2.code = ghm_2_code 
	AND t_ghm_3.code = ghm_3_code;