<?xml version="1.0" encoding="ISO-8859-15"?>
<DBSETUPDATA softCode="iCTI_pmsi" schema="pmsi">
	<!-- SCHEMA "BASE" -->
	<TABLE name="p_calendrier" label="Calendrier" schema="base" />
	<TABLE name="p_calendrier_mois" label="Calendrier (mois)" schema="base" />
	<TABLE name="t_departements" label="Départements" schema="base" />
	<TABLE name="t_territoires_sante" label="Territoires de santé" schema="base" />
	<TABLE name="t_regions" label="Régions" schema="base" />
	<TABLE name="t_finess" label="Finess" schema="base" />
	<TABLE name="t_codes_geographiques_pmsi" label="Codes géographiques PMSI" schema="base" />
	<TABLE name="t_codes_postaux" label="Codes postaux"	schema="base"/>
	<TABLE name="t_listes_tables_base" label="Tables pour les listes" schema="base" />
	<TABLE name="t_structure_code_ccam" label="Structure code CCAM" schema="base" />
	<TABLE name="t_ipa" label="I.P.A." schema="base" />
	<TABLE name="t_cas" label="Catégories d'actitivés de soins" schema="base" />
	<TABLE name="t_exoneration_tm" label="Exonération ticket modérateur" schema="base" />
	<TABLE name="t_facturation_18euros" label="Facturation 18 euros" schema="base" />
	<TABLE name="t_grands_regimes" label="Grands régimes" schema="base" />
	<TABLE name="t_motif_non_facturation" label="Motifs de non facturation à l'AMO" schema="base" />
	<TABLE name="t_nature_assurance" label="Nature d'assurance" schema="base" />
	<TABLE name="t_pec" label="Séjour facturable à l'AMO" schema="base" />
	<TABLE name="t_pec_fj" label="Prise en charge du forfait journalier" schema="base" />
	<TABLE name="t_type_contrat_oc" label="Type de contrat organisme complémentaire" schema="base" />
	<TABLE name="t_durees_sejour" label="Durées de séjour" schema="base" />
	<TABLE name="t_ages" label="Ages" schema="base" />



	<!-- SCHEMA "PMSI" -->

	<!-- t_ages deprecated, remonté dans base
	<TABLE name="t_ages" label="Ages">
		<selectCmd><![CDATA[
			
			SELECT 
				oid AS keepoid, 
				code, 
				texte
				FROM pmsi.t_ages
				ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[	
			
			UPDATE pmsi.t_ages SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE t_ages.oid = w_dbsetup.keepoid;
			
			
			INSERT INTO pmsi.t_ages(oid, code, texte)
				SELECT keepoid, code, texte 
				FROM w_dbsetup 
				WHERE keepoid NOT IN (SELECT oid FROM pmsi.t_ages);
			
		]]></updateCmd>
	</TABLE>
	-->

	<!-- t_durees_sejour deprecated, remonté dans base
	<TABLE name="t_durees_sejour" label="t_durees_sejour">
		<selectCmd><![CDATA[
			
			SELECT oid as keepoid, code, texte
			FROM pmsi.t_durees_sejour
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			UPDATE pmsi.t_durees_sejour SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE t_durees_sejour.oid = w_dbsetup.keepoid;
			
			
			INSERT INTO pmsi.t_durees_sejour(oid, code, texte)
				SELECT keepoid, code, texte 
				FROM w_dbsetup 
				WHERE keepoid NOT IN (SELECT oid FROM pmsi.t_durees_sejour);
			
		]]></updateCmd>
	</TABLE>
	-->

	<TABLE name="t_gestes_marqueurs" label="Gestes marqueurs">
		<selectCmd><![CDATA[ 
			
			SELECT 
				age_max,
				age_min,
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				texte_court
			FROM pmsi.t_gestes_marqueurs
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[		
			
			UPDATE pmsi.t_gestes_marqueurs SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_gestes_marqueurs.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_gestes_marqueurs SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				age_min = w_dbsetup.age_min,
				age_max = w_dbsetup.age_max
			FROM w_dbsetup
			WHERE t_gestes_marqueurs.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_gestes_marqueurs(oid, code, texte, texte_court, age_min, age_max)
				SELECT oid, code, texte, texte_court, age_min, age_max 
				FROM w_dbsetup 
				WHERE oid = 0 AND oid NOT IN (SELECT oid FROM pmsi.t_gestes_marqueurs);
			
			
			INSERT INTO pmsi.t_gestes_marqueurs(code, texte, texte_court, age_min, age_max)
				SELECT code, texte, texte_court, age_min, age_max 
				FROM w_dbsetup 
				WHERE oid <> 0 AND code NOT IN (SELECT code FROM pmsi.t_gestes_marqueurs);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ccam_regroupements" label="Regroupements CCAM">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				texte_court
			FROM pmsi.t_ccam_regroupements
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_ccam_regroupements SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_ccam_regroupements.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_ccam_regroupements SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_ccam_regroupements.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_ccam_regroupements(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_ccam_regroupements);
			
			
			INSERT INTO pmsi.t_ccam_regroupements(code, texte, texte_court)
				SELECT code, texte, texte_court 
				FROM w_dbsetup 
				WHERE 
					oid <> 0 
					AND code NOT IN (SELECT code FROM pmsi.t_ccam_regroupements);

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_modificateurs_ccam" label="Filtre  CCAM">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				texte_court
			FROM pmsi.t_modificateurs_ccam
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_modificateurs_ccam SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_modificateurs_ccam.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_modificateurs_ccam SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_modificateurs_ccam.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_modificateurs_ccam(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_modificateurs_ccam);
			
			
			INSERT INTO pmsi.t_modificateurs_ccam(code, texte, texte_court)
				SELECT code, texte, texte_court 
				FROM w_dbsetup 
				WHERE 
					oid <> 0 
					AND code NOT IN (SELECT code FROM pmsi.t_modificateurs_ccam);

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_association_non_prevue" label="Filtre  ANP">
		<selectCmd><![CDATA[ 
			
			SELECT 
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte
			FROM pmsi.t_association_non_prevue
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_association_non_prevue SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_association_non_prevue.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_association_non_prevue SET 
				texte = w_dbsetup.texte 
			FROM w_dbsetup
			WHERE t_association_non_prevue.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_association_non_prevue(oid, code, texte)
				SELECT oid, code, texte
				FROM w_dbsetup 
				WHERE 
					oid = 0 
					AND oid NOT IN (SELECT oid FROM pmsi.t_association_non_prevue);
			
			
			INSERT INTO pmsi.t_association_non_prevue(code, texte)
				SELECT code, texte 
				FROM w_dbsetup 
				WHERE 
					oid <> 0 
					AND code NOT IN (SELECT code FROM pmsi.t_association_non_prevue);

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_actes" label="Actes">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_actes.oid = 0 THEN t_actes.oid ELSE 1 END AS oid,
				t_actes.code, 
				t_actes.texte, 
				t_actes.texte_court, 
				t_actes.nomenclature, 
				t_actes."type", 
				t_actes.sexe, 
				t_actes.classant, 
				t_actes.marqueur, 
				t_actes.icr, 
				t_actes.icr_1,
				t_actes.icr_4, 
				t_actes.icr_prive_atih_1, 
				t_actes.icr_prive_atih_4, 
				t_actes.icr_p_prive_atih_1, 
				t_actes.icr_public_atih_1, 
				t_actes.icr_public_atih_4,
				t_actes.icr_p_public_atih_1,
				t_actes.tarif_1,
				t_actes.tarif_4, 
				t_actes.tarif_5, 
				t_ccam_regroupements_1.code as ccam_regroupement_code_1, 
				t_ccam_regroupements_4.code as ccam_regroupement_code_4, 
				t_gestes_marqueurs.code as geste_marqueur_code,
				t_actes.chapitre_1_id AS chapitre_1_toid,
				t_actes.chapitre_1_code,
				t_actes.chapitre_1_texte,
				t_actes.chapitre_2_id AS chapitre_2_toid,
				t_actes.chapitre_2_code,
				t_actes.chapitre_2_texte,
				t_actes.chapitre_3_id AS chapitre_3_toid,
				t_actes.chapitre_3_code,
				t_actes.chapitre_3_texte,
				t_actes.chapitre_4_id AS chapitre_4_toid,
				t_actes.chapitre_4_code,
				t_actes.chapitre_4_texte,
				t_actes.type_id AS type_toid,
				t_actes.type_code,
				t_actes.type_texte,
				t_actes.date_debut_validite,
				t_actes.date_fin_validite
			FROM 
				pmsi.t_actes, 
				pmsi.t_ccam_regroupements t_ccam_regroupements_1,
				pmsi.t_ccam_regroupements t_ccam_regroupements_4, 
				pmsi.t_gestes_marqueurs
			WHERE 
				ccam_regroupement_id_1 = t_ccam_regroupements_1.oid
				AND ccam_regroupement_id_4 = t_ccam_regroupements_4.oid
				AND geste_marqueur_id = t_gestes_marqueurs.oid
				AND t_actes.nomenclature <> 'CDAM'
			ORDER BY t_actes.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			-- Des actes de la ccam descriptive ont été dans un premier temps livrés avec le code 'ATIH' ceci 
			-- génére un problème car entre temps le code a été changé en 'CCAM' -> problème d'installation
			DELETE FROM pmsi.t_actes WHERE nomenclature = 'ATIH';
			
			UPDATE pmsi.t_actes SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_actes.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;	 
			
			
			UPDATE pmsi.t_actes SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				"type" = w_dbsetup."type",
				sexe = w_dbsetup.sexe,
				classant = w_dbsetup.classant,
				marqueur = w_dbsetup.marqueur,
				icr = w_dbsetup.icr,
				icr_1 = w_dbsetup.icr_1,
				icr_4 = w_dbsetup.icr_4,
				icr_prive_atih_1 = w_dbsetup.icr_prive_atih_1,
				icr_prive_atih_4 = w_dbsetup.icr_prive_atih_4,
				icr_p_prive_atih_1 = w_dbsetup.icr_p_prive_atih_1,
				icr_public_atih_1 = w_dbsetup.icr_public_atih_1,
				icr_public_atih_4 = w_dbsetup.icr_public_atih_4,
				icr_p_public_atih_1 = w_dbsetup.icr_p_public_atih_1,
				tarif_1 = w_dbsetup.tarif_1,
				tarif_4 = w_dbsetup.tarif_4,
				tarif_5 = w_dbsetup.tarif_5,
				ccam_regroupement_id_1 = t_ccam_regroupements_1.oid,
				ccam_regroupement_id_4 = t_ccam_regroupements_4.oid,
				geste_marqueur_id = t_gestes_marqueurs.oid,
				chapitre_1_id = w_dbsetup.chapitre_1_toid,
				chapitre_1_code = w_dbsetup.chapitre_1_code,
				chapitre_1_texte = w_dbsetup.chapitre_1_texte,
				chapitre_2_id = w_dbsetup.chapitre_2_toid,
				chapitre_2_code = w_dbsetup.chapitre_2_code,
				chapitre_2_texte = w_dbsetup.chapitre_2_texte,
				chapitre_3_id = w_dbsetup.chapitre_3_toid,
				chapitre_3_code = w_dbsetup.chapitre_3_code,
				chapitre_3_texte = w_dbsetup.chapitre_3_texte,
				chapitre_4_id = w_dbsetup.chapitre_4_toid,
				chapitre_4_code = w_dbsetup.chapitre_4_code,
				chapitre_4_texte = w_dbsetup.chapitre_4_texte,
				type_id = w_dbsetup.type_toid,
				type_code = w_dbsetup.type_code,
				type_texte = w_dbsetup.type_texte,
				date_debut_validite = w_dbsetup.date_debut_validite,
				date_fin_validite = w_dbsetup.date_fin_validite,
                nomenclature = w_dbsetup.nomenclature
			FROM 
				w_dbsetup, 
				pmsi.t_ccam_regroupements t_ccam_regroupements_1, 
				pmsi.t_ccam_regroupements t_ccam_regroupements_4, 
				pmsi.t_gestes_marqueurs
			WHERE 
				t_actes.code = w_dbsetup.code 
				AND w_dbsetup.ccam_regroupement_code_1 = t_ccam_regroupements_1.code
				AND w_dbsetup.ccam_regroupement_code_4 = t_ccam_regroupements_4.code
				AND w_dbsetup.geste_marqueur_code = t_gestes_marqueurs.code
				AND (
					t_actes.texte IS DISTINCT FROM w_dbsetup.texte OR	
					t_actes.texte_court IS DISTINCT FROM w_dbsetup.texte_court OR		
					t_actes."type" IS DISTINCT FROM w_dbsetup."type" OR		
					t_actes.sexe IS DISTINCT FROM w_dbsetup.sexe OR	
					t_actes.classant IS DISTINCT FROM w_dbsetup.classant OR		
					t_actes.marqueur IS DISTINCT FROM w_dbsetup.marqueur OR		
					t_actes.icr IS DISTINCT FROM w_dbsetup.icr OR		
					t_actes.icr_1 IS DISTINCT FROM w_dbsetup.icr_1 OR		
					t_actes.icr_4 IS DISTINCT FROM w_dbsetup.icr_4 OR	
					t_actes.icr_prive_atih_1 IS DISTINCT FROM w_dbsetup.icr_prive_atih_1 OR		
					t_actes.icr_prive_atih_4 IS DISTINCT FROM w_dbsetup.icr_prive_atih_4 OR		
					t_actes.icr_p_prive_atih_1 IS DISTINCT FROM w_dbsetup.icr_p_prive_atih_1 OR	
					t_actes.icr_public_atih_1 IS DISTINCT FROM w_dbsetup.icr_public_atih_1 OR		
					t_actes.icr_public_atih_4 IS DISTINCT FROM w_dbsetup.icr_public_atih_4 OR	
					t_actes.icr_p_public_atih_1 IS DISTINCT FROM w_dbsetup.icr_p_public_atih_1 OR
					t_actes.tarif_1 IS DISTINCT FROM w_dbsetup.tarif_1 OR		
					t_actes.tarif_4 IS DISTINCT FROM w_dbsetup.tarif_4 OR	
					t_actes.tarif_5 IS DISTINCT FROM w_dbsetup.tarif_5 OR	
					t_actes.ccam_regroupement_id_1 IS DISTINCT FROM t_ccam_regroupements_1.oid OR		
					t_actes.ccam_regroupement_id_4 IS DISTINCT FROM t_ccam_regroupements_4.oid OR		
					t_actes.geste_marqueur_id IS DISTINCT FROM t_gestes_marqueurs.oid OR
					t_actes.chapitre_1_id IS DISTINCT FROM w_dbsetup.chapitre_1_toid OR 
					t_actes.chapitre_1_code IS DISTINCT FROM w_dbsetup.chapitre_1_code OR 
					t_actes.chapitre_1_texte IS DISTINCT FROM w_dbsetup.chapitre_1_texte OR 
					t_actes.chapitre_2_id IS DISTINCT FROM w_dbsetup.chapitre_2_toid OR 
					t_actes.chapitre_2_code IS DISTINCT FROM w_dbsetup.chapitre_2_code OR 
					t_actes.chapitre_2_texte IS DISTINCT FROM w_dbsetup.chapitre_2_texte OR 
					t_actes.chapitre_3_id IS DISTINCT FROM w_dbsetup.chapitre_3_toid OR 
					t_actes.chapitre_3_code IS DISTINCT FROM w_dbsetup.chapitre_3_code OR 
					t_actes.chapitre_3_texte IS DISTINCT FROM w_dbsetup.chapitre_3_texte OR 
					t_actes.chapitre_4_id IS DISTINCT FROM w_dbsetup.chapitre_4_toid OR 
					t_actes.chapitre_4_code IS DISTINCT FROM w_dbsetup.chapitre_4_code OR 
					t_actes.chapitre_4_texte IS DISTINCT FROM w_dbsetup.chapitre_4_texte OR 
					t_actes.type_id IS DISTINCT FROM w_dbsetup.type_toid OR 
					t_actes.type_code IS DISTINCT FROM w_dbsetup.type_code OR 
					t_actes.type_texte IS DISTINCT FROM w_dbsetup.type_texte OR 
					t_actes.date_debut_validite IS DISTINCT FROM w_dbsetup.date_debut_validite OR 
					t_actes.date_fin_validite IS DISTINCT FROM w_dbsetup.date_fin_validite OR 
					t_actes.nomenclature IS DISTINCT FROM w_dbsetup.nomenclature
				);	 
			
			
			INSERT INTO 
				pmsi.t_actes(
					oid, 
					code, 
					texte, 
					texte_court, 
					nomenclature, 
					"type", 
					sexe, 
					classant, 
					marqueur, 
					icr, 
					icr_1, 
					icr_4, 
					tarif_1, 
					tarif_4, 
					tarif_5, 
					icr_prive_atih_1, 
					icr_prive_atih_4, 
					icr_p_prive_atih_1, 
					icr_public_atih_1, 
					icr_public_atih_4, 
					icr_p_public_atih_1, 
					ccam_regroupement_id_1, 
					ccam_regroupement_id_4, 
					geste_marqueur_id,
					chapitre_1_id, 
					chapitre_1_code,
					chapitre_1_texte, 
					chapitre_2_id, 
					chapitre_2_code, 
					chapitre_2_texte,
					chapitre_3_id,
					chapitre_3_code,
					chapitre_3_texte,
					chapitre_4_id,
					chapitre_4_code,
					chapitre_4_texte,
					type_id,
					type_code,
					type_texte,
                    date_debut_validite,
                    date_fin_validite)
				SELECT 
					w_dbsetup.oid, 
					w_dbsetup.code,
					w_dbsetup.texte, 
					w_dbsetup.texte_court,
					w_dbsetup.nomenclature, 
					"type", 
					sexe, 
					classant, 
					marqueur, 
					icr, 
					icr_1, 
					icr_4, 
					tarif_1, 
					tarif_4, 
					tarif_5, 
					icr_prive_atih_1, 
					icr_prive_atih_4, 
					icr_p_prive_atih_1, 
					icr_public_atih_1, 
					icr_public_atih_4,
					icr_p_public_atih_1,
					0,
					0, 
					0, 
					0, 
					'0',
					'',
					0, 
					'0',
					'',
					0, 
					'0', 
					'',
					0, 
					'0', 
					'', 
					0, 
					'0', 
					'',
                    '00010101'::date as date_debut_validite,
                    '20991231'::date as date_fin_validite
				FROM 
					w_dbsetup
				WHERE 
					w_dbsetup.oid = 0 
					AND w_dbsetup.oid NOT IN (SELECT oid FROM pmsi.t_actes)
				;
			
			
			INSERT INTO pmsi.t_actes(
				code, 
				texte, 
				texte_court, 
				nomenclature, 
				"type", 
				sexe, 
				classant, 
				marqueur, 
				icr, 
				icr_1, 
				icr_4, 
				tarif_1, 
				tarif_4, 
				tarif_5, 
				icr_prive_atih_1, 
				icr_prive_atih_4, 
				icr_p_prive_atih_1, 
				icr_public_atih_1, 
				icr_public_atih_4, 
				icr_p_public_atih_1, 
				ccam_regroupement_id_1, 
				ccam_regroupement_id_4, 
				geste_marqueur_id,
				chapitre_1_id, 
				chapitre_1_code,
				chapitre_1_texte,
				chapitre_2_id,
				chapitre_2_code,
				chapitre_2_texte,
				chapitre_3_id,
				chapitre_3_code,
				chapitre_3_texte,
				chapitre_4_id,
				chapitre_4_code,
				chapitre_4_texte,
				type_id,
				type_code,
				type_texte,
                date_debut_validite,
                date_fin_validite)
				SELECT 
					w_dbsetup.code,
					w_dbsetup.texte, 
					w_dbsetup.texte_court, 
					w_dbsetup.nomenclature, 
					w_dbsetup."type", 
					w_dbsetup.sexe,
					w_dbsetup.classant,
					w_dbsetup.marqueur, 
					w_dbsetup.icr, 
					w_dbsetup.icr_1, 
					w_dbsetup.icr_4, 
					w_dbsetup.tarif_1, 
					w_dbsetup.tarif_4, 
					w_dbsetup.tarif_5, 
					w_dbsetup.icr_prive_atih_1, 
					w_dbsetup.icr_prive_atih_4, 
					w_dbsetup.icr_p_prive_atih_1, 
					w_dbsetup.icr_public_atih_1, 
					w_dbsetup.icr_public_atih_4,
					w_dbsetup.icr_p_public_atih_1,
					t_ccam_regroupements_1.oid,
					t_ccam_regroupements_4.oid, 
					t_gestes_marqueurs.oid, 
					w_dbsetup.chapitre_1_toid, 
					w_dbsetup.chapitre_1_code,
					w_dbsetup.chapitre_1_texte,
					w_dbsetup.chapitre_2_toid,
					w_dbsetup.chapitre_2_code,
					w_dbsetup.chapitre_2_texte,
					w_dbsetup.chapitre_3_toid,
					w_dbsetup.chapitre_3_code,
					w_dbsetup.chapitre_3_texte,
					w_dbsetup.chapitre_4_toid,
					w_dbsetup.chapitre_4_code,
					w_dbsetup.chapitre_4_texte,
					w_dbsetup.type_toid, 
                    w_dbsetup.type_code,
					w_dbsetup.type_texte,
                    w_dbsetup.date_debut_validite,
                    w_dbsetup.date_fin_validite
				FROM 
					w_dbsetup, 
					pmsi.t_ccam_regroupements t_ccam_regroupements_1, 
					pmsi.t_ccam_regroupements t_ccam_regroupements_4, 
					pmsi.t_gestes_marqueurs
				WHERE 
					w_dbsetup.oid <> 0 
					AND w_dbsetup.code || w_dbsetup.nomenclature NOT IN (SELECT code || nomenclature FROM pmsi.t_actes)
					AND ccam_regroupement_code_1 = t_ccam_regroupements_1.code
					AND ccam_regroupement_code_4 = t_ccam_regroupements_4.code
					AND geste_marqueur_code = t_gestes_marqueurs.code;
			
                UPDATE pmsi.t_actes SET
                    racine_id = subview.racine_id,
                    racine_code = subview.racine_code,
                    racine_texte = subview.racine_texte
                FROM
                    (SELECT 
                        t_actes.oid, 
                        CASE WHEN t_actes_racine.oid IS NOT NULL THEN t_actes_racine.oid ELSE t_actes.oid END as racine_id, 
                        CASE WHEN t_actes_racine.code IS NOT NULL THEN t_actes_racine.code ELSE t_actes.code END as racine_code, 
                        CASE WHEN t_actes_racine.texte IS NOT NULL THEN t_actes_racine.texte ELSE t_actes.code END as racine_texte
                    FROM 
                        pmsi.t_actes
                        LEFT JOIN pmsi.t_actes t_actes_racine ON (substr(t_actes.code, 1, 7) = t_actes_racine.code)) subview
                WHERE 
                    t_actes.oid = subview.oid
                    AND (
                        t_actes.racine_id IS NULL OR t_actes.racine_id <> subview.racine_id OR 
                        t_actes.racine_code IS NULL OR t_actes.racine_code <> subview.racine_code OR 
                        t_actes.racine_texte IS NULL OR t_actes.racine_texte <> subview.racine_texte 
                    );
			
			
			SELECT pmsi.cti_update_structure_code_ccam();
			
			SELECT pmsi.cti_reorganize_actes_c();
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_diagnostics" label="Diagnostics">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code, 
				texte, 
				niveau, 
				liste_cma, 
				severite_fg11_simulation_code, 
				severite_fg11_code, 
				severite_fg12_code, 
				severite_fg13_code, 
				severite_fg1311d_code, 
				severite_fg1311e_code, 
				severite_fg1311f_code,
				severite_fg1311g_code,
				severite_fg2016_code,
				severite_fg2017_code,
				severite_fg2018_code,
				severite_fg2019_code,
				severite_fg2020_code,
				severite_fg2021_code,
				severite_fg2022_code,
				severite_fg2023_code
			FROM 
				pmsi.t_diagnostics 
            WHERE
                t_diagnostics.code IS DISTINCT FROM t_diagnostics.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_diagnostics SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_diagnostics.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;
				
				
			UPDATE pmsi.t_diagnostics SET 
				texte = w_dbsetup.texte, 
				niveau = w_dbsetup.niveau,
				liste_cma = w_dbsetup.liste_cma,
				severite_fg11_simulation_code = w_dbsetup.severite_fg11_simulation_code,
				severite_fg11_code = w_dbsetup.severite_fg11_code,
				severite_fg12_code = w_dbsetup.severite_fg12_code,
				severite_fg13_code = w_dbsetup.severite_fg13_code,
				severite_fg1311d_code = w_dbsetup.severite_fg1311d_code,
				severite_fg1311e_code = w_dbsetup.severite_fg1311e_code,
				severite_fg1311f_code = w_dbsetup.severite_fg1311f_code,
				severite_fg1311g_code = w_dbsetup.severite_fg1311g_code,
				severite_fg2016_code = w_dbsetup.severite_fg2016_code	,
				severite_fg2017_code = w_dbsetup.severite_fg2017_code	,
				severite_fg2018_code = w_dbsetup.severite_fg2018_code,
				severite_fg2019_code = w_dbsetup.severite_fg2019_code,
				severite_fg2020_code = w_dbsetup.severite_fg2020_code,
				severite_fg2021_code = w_dbsetup.severite_fg2021_code,
				severite_fg2022_code = w_dbsetup.severite_fg2022_code,
				severite_fg2023_code = w_dbsetup.severite_fg2023_code
			FROM w_dbsetup
			WHERE t_diagnostics.code = w_dbsetup.code
				 AND (
					t_diagnostics.texte IS DISTINCT FROM w_dbsetup.texte OR	
					t_diagnostics.niveau IS DISTINCT FROM w_dbsetup.niveau OR 	
					t_diagnostics.liste_cma IS DISTINCT FROM w_dbsetup.liste_cma OR
					t_diagnostics.severite_fg11_simulation_code IS DISTINCT FROM w_dbsetup.severite_fg11_simulation_code OR
					t_diagnostics.severite_fg11_code IS DISTINCT FROM w_dbsetup.severite_fg11_code OR
					t_diagnostics.severite_fg12_code IS DISTINCT FROM w_dbsetup.severite_fg12_code OR
					t_diagnostics.severite_fg13_code IS DISTINCT FROM w_dbsetup.severite_fg13_code OR
					t_diagnostics.severite_fg1311d_code IS DISTINCT FROM w_dbsetup.severite_fg1311d_code OR
					t_diagnostics.severite_fg1311e_code IS DISTINCT FROM w_dbsetup.severite_fg1311e_code OR
					t_diagnostics.severite_fg1311f_code IS DISTINCT FROM w_dbsetup.severite_fg1311f_code OR
					t_diagnostics.severite_fg1311g_code IS DISTINCT FROM w_dbsetup.severite_fg1311g_code OR
					t_diagnostics.severite_fg2016_code IS DISTINCT FROM w_dbsetup.severite_fg2016_code OR
					t_diagnostics.severite_fg2017_code IS DISTINCT FROM w_dbsetup.severite_fg2017_code OR
					t_diagnostics.severite_fg2018_code IS DISTINCT FROM w_dbsetup.severite_fg2018_code OR
					t_diagnostics.severite_fg2019_code IS DISTINCT FROM w_dbsetup.severite_fg2019_code OR
					t_diagnostics.severite_fg2020_code IS DISTINCT FROM w_dbsetup.severite_fg2020_code OR
					t_diagnostics.severite_fg2021_code IS DISTINCT FROM w_dbsetup.severite_fg2021_code OR
					t_diagnostics.severite_fg2022_code IS DISTINCT FROM w_dbsetup.severite_fg2022_code OR
					t_diagnostics.severite_fg2023_code IS DISTINCT FROM w_dbsetup.severite_fg2023_code
					);
			
			
			INSERT INTO pmsi.t_diagnostics(
				oid, 
				code, 
				texte, 
				niveau,
				liste_cma, 
				severite_fg11_simulation_code, 
				severite_fg11_code, 
				severite_fg12_code, 
				severite_fg13_code, 
				severite_fg1311d_code, 
				severite_fg1311e_code, 
				severite_fg1311f_code,
				severite_fg1311g_code,
				severite_fg2016_code,
				severite_fg2017_code,
				severite_fg2018_code,
				severite_fg2019_code,
				severite_fg2020_code,
				severite_fg2021_code,
				severite_fg2022_code,
				severite_fg2023_code
				)
			SELECT 
				oid, 
				code, 
				texte,
				niveau, 
				liste_cma, 
				severite_fg11_simulation_code, 
				severite_fg11_code, 
				severite_fg12_code, 
				severite_fg13_code, 
				severite_fg1311d_code, 
				severite_fg1311e_code, 
				severite_fg1311f_code, 
				severite_fg1311g_code,
				severite_fg2016_code,
				severite_fg2017_code,
				severite_fg2018_code,
				severite_fg2019_code,
				severite_fg2020_code,
				severite_fg2021_code,
				severite_fg2022_code,
				severite_fg2023_code
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_diagnostics);
				
				
			INSERT INTO pmsi.t_diagnostics(
				code, 
				texte, 
				niveau, 
				liste_cma, 
				severite_fg11_simulation_code, 
				severite_fg11_code, 
				severite_fg12_code, 
				severite_fg13_code, 
				severite_fg1311d_code, 
				severite_fg1311e_code, 
				severite_fg1311f_code, 
				severite_fg1311g_code,
				severite_fg2016_code,
				severite_fg2017_code,
				severite_fg2018_code,
				severite_fg2019_code,
				severite_fg2020_code,
				severite_fg2021_code,
				severite_fg2022_code,
				severite_fg2023_code
				)
				SELECT 
					code, 
					texte, 
					niveau, 
					liste_cma, 
					severite_fg11_simulation_code, 
					severite_fg11_code, 
					severite_fg12_code, 
					severite_fg13_code, 
					severite_fg1311d_code, 
					severite_fg1311e_code, 
					severite_fg1311f_code, 
					severite_fg1311g_code,
					severite_fg2016_code,
					severite_fg2017_code,
					severite_fg2018_code,
					severite_fg2019_code,
					severite_fg2020_code,
					severite_fg2021_code,
					severite_fg2022_code,
					severite_fg2023_code
				FROM w_dbsetup 
				WHERE 
					oid <> 0 
					AND code NOT IN (SELECT code FROM pmsi.t_diagnostics);
			
			
			UPDATE pmsi.t_diagnostics SET
				racine_id = subview.racine_id,
				racine_code = subview.racine_code || '*',
				racine_texte = subview.racine_texte
			FROM
				(SELECT 
					t_diagnostics.oid, 
					CASE WHEN t_diagnostics_racine.oid IS NOT NULL THEN t_diagnostics_racine.oid ELSE t_diagnostics.oid END as racine_id, 
					CASE WHEN t_diagnostics_racine.code IS NOT NULL THEN t_diagnostics_racine.code ELSE t_diagnostics.code END as racine_code, 
					CASE WHEN t_diagnostics_racine.texte IS NOT NULL THEN t_diagnostics_racine.texte ELSE t_diagnostics.code END as racine_texte
				FROM 
					pmsi.t_diagnostics
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_racine ON (substr(t_diagnostics.code, 1, 3) = t_diagnostics_racine.code)) subview
			WHERE t_diagnostics.oid = subview.oid
				AND (
					t_diagnostics.racine_id IS NULL OR t_diagnostics.racine_id <> subview.racine_id OR 
					t_diagnostics.racine_code IS NULL OR t_diagnostics.racine_code <> (subview.racine_code || '*') OR 
					t_diagnostics.racine_texte IS NULL OR t_diagnostics.racine_texte <> subview.racine_texte 
				);
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_cmd" label="CMD">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				texte_court
			FROM pmsi.t_cmd
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_cmd SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_cmd.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_cmd SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_cmd.code = w_dbsetup.code
			AND (
				t_cmd.texte <> w_dbsetup.texte OR
				t_cmd.texte_court <> w_dbsetup.texte_court OR
				t_cmd.texte IS NULL OR
				t_cmd.texte_court IS NULL
			);
			
			
			INSERT INTO pmsi.t_cmd(
				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_cmd);
			
			
			INSERT INTO pmsi.t_cmd(
				code, 
				texte, 
				texte_court)
			SELECT 
				code, 
				texte, 
				texte_court 
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_cmd);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_mco" label="Aso">
		<selectCmd><![CDATA[ 
			
			SELECT code, texte FROM pmsi.t_mco ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_mco SET 
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE t_mco.code = w_dbsetup.code
			AND (
				t_mco.texte <> w_dbsetup.texte OR
				t_mco.texte IS NULL
			);
			
			
			INSERT INTO pmsi.t_mco(code, texte)
			SELECT 
				code, texte 
			FROM w_dbsetup 
			WHERE code NOT IN (SELECT code FROM pmsi.t_mco);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_severites_ghm" label="Complexités des GHM">
		<selectCmd><![CDATA[ 
			
			SELECT code, texte FROM pmsi.t_severites_ghm ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_severites_ghm SET 
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_severites_ghm.code = w_dbsetup.code
				AND (
					t_severites_ghm.texte <> w_dbsetup.texte OR
					t_severites_ghm.texte IS NULL
				);
				
				
			INSERT INTO pmsi.t_severites_ghm(code, texte)
			SELECT code, texte 
			FROM w_dbsetup 
			WHERE code NOT IN (SELECT code FROM pmsi.t_severites_ghm);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_codes_retour_groupeur" label="Codes retour groupeur">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				type,
				code,
				texte,
				bloquante
			FROM pmsi.t_codes_retour_groupeur 
			ORDER BY type, code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_codes_retour_groupeur SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_codes_retour_groupeur.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_codes_retour_groupeur SET 
				texte = w_dbsetup.texte, 
				type = w_dbsetup.type,
				bloquante = w_dbsetup.bloquante
			FROM w_dbsetup
			WHERE 
				t_codes_retour_groupeur.type || t_codes_retour_groupeur.code = w_dbsetup.type || w_dbsetup.code
				AND (
					t_codes_retour_groupeur.texte IS DISTINCT FROM w_dbsetup.texte OR
					t_codes_retour_groupeur.bloquante IS DISTINCT FROM w_dbsetup.bloquante OR
					t_codes_retour_groupeur.type IS DISTINCT FROM w_dbsetup.type
				);
			
			
			INSERT INTO pmsi.t_codes_retour_groupeur(
				oid,type,code,texte,bloquante)
			SELECT 
				oid,type,code,texte,bloquante
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_codes_retour_groupeur);
			
			
			INSERT INTO pmsi.t_codes_retour_groupeur(type,code,texte,bloquante)
			SELECT 
				type,code,texte,bloquante
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND type || code NOT IN (SELECT type || code FROM pmsi.t_codes_retour_groupeur);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_poles_oap" label="Poles OAP">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court 
			FROM pmsi.t_poles_oap 
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_poles_oap SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_poles_oap.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_poles_oap SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 
				t_poles_oap.code = w_dbsetup.code
				AND (
					t_poles_oap.texte <> w_dbsetup.texte OR
					t_poles_oap.texte_court <> w_dbsetup.texte_court OR
					t_poles_oap.texte IS NULL OR
					t_poles_oap.texte_court IS NULL
				);
				
				
			INSERT INTO pmsi.t_poles_oap(
				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_poles_oap);
			
			
			INSERT INTO pmsi.t_poles_oap(
				code, 
				texte, 
				texte_court)
			SELECT 
				code, 
				texte, 
				texte_court
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_poles_oap);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_lignes_oap" label="Lignes OAP">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_lignes_oap.oid = 0 THEN t_lignes_oap.oid ELSE 1 END AS oid,
				t_lignes_oap.code,
				t_lignes_oap.texte,
				t_lignes_oap.mco,
				t_lignes_oap.lourdeur,
				t_poles_oap.code AS pole_oap_code 
			FROM 
				pmsi.t_lignes_oap, 
				pmsi.t_poles_oap 
			WHERE 
				t_lignes_oap.pole_oap_id = t_poles_oap.oid 
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_lignes_oap SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_lignes_oap.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_lignes_oap SET 
				texte = w_dbsetup.texte, 
				mco = w_dbsetup.mco,
				lourdeur = w_dbsetup.lourdeur,
				pole_oap_id = t_poles_oap.oid
			FROM 
				w_dbsetup, 
				pmsi.t_poles_oap 
			WHERE 
				t_lignes_oap.code = w_dbsetup.code	
				AND w_dbsetup.pole_oap_code = t_poles_oap.code;	
			
			
			INSERT INTO pmsi.t_lignes_oap(
				oid, 
				code, 
				texte, 
				mco, 
				lourdeur, 
				pole_oap_id)
			SELECT 
				w_dbsetup.oid, 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.mco, 
				w_dbsetup.lourdeur, 
				t_poles_oap.oid
			FROM 
				w_dbsetup, 
				pmsi.t_poles_oap 
			WHERE 
				w_dbsetup.oid = 0 
				AND w_dbsetup.pole_oap_code = t_poles_oap.code 
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_lignes_oap);
			
			
			INSERT INTO pmsi.t_lignes_oap(
				code, 
				texte, 
				mco, 
				lourdeur, 
				pole_oap_id)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.mco, 
				w_dbsetup.lourdeur, 
				t_poles_oap.oid
			FROM 
				w_dbsetup, 
				pmsi.t_poles_oap 
			WHERE
				w_dbsetup.oid <> 0 
				AND w_dbsetup.pole_oap_code = t_poles_oap.code 
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_lignes_oap);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_groupes_activite" label="Groupes d'activité">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				mco,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				texte_court
			FROM pmsi.t_groupes_activite
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_groupes_activite SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_groupes_activite.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_groupes_activite SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				mco = w_dbsetup.mco
			FROM w_dbsetup
			WHERE 
				t_groupes_activite.code = w_dbsetup.code
				AND (
					t_groupes_activite.texte <> w_dbsetup.texte OR
					t_groupes_activite.texte_court <> w_dbsetup.texte_court OR
					t_groupes_activite.mco <> w_dbsetup.mco OR
					t_groupes_activite.texte IS NULL OR
					t_groupes_activite.texte_court IS NULL OR
					t_groupes_activite.mco IS NULL
				);
			
			
			INSERT INTO pmsi.t_groupes_activite(
				oid, 
				code, 
				texte, 
				texte_court, 
				mco)
			SELECT
				oid, 
				code, 
				texte, 
				texte_court, 
				mco 
			FROM 
				w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_groupes_activite);
			
			
			INSERT INTO pmsi.t_groupes_activite(
				code, 
				texte, 
				texte_court, 
				mco)
			SELECT 
				code, 
				texte, 
				texte_court, 
				mco 
			FROM 
				w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_groupes_activite);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ghm5" label="GHM (5)">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				severite,
				da_code,
				gp_code,
				ga_code,
				da_gp_code,
				da_gp_ga_code
			FROM pmsi.t_ghm5
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_ghm5 SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_ghm5.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;
			
			UPDATE pmsi.t_ghm5 SET 
				texte = w_dbsetup.texte, 
				severite = w_dbsetup.severite,
				da_code = w_dbsetup.da_code,
				gp_code = w_dbsetup.gp_code,
				ga_code = w_dbsetup.ga_code,
				da_gp_code = w_dbsetup.da_gp_code,
				da_gp_ga_code = w_dbsetup.da_gp_ga_code
			FROM w_dbsetup
			WHERE 
				t_ghm5.code = w_dbsetup.code
				AND (
					t_ghm5.texte IS DISTINCT FROM w_dbsetup.texte OR
					t_ghm5.severite IS DISTINCT FROM w_dbsetup.severite OR
					t_ghm5.da_code IS DISTINCT FROM w_dbsetup.da_code OR
					t_ghm5.gp_code IS DISTINCT FROM w_dbsetup.gp_code OR
					t_ghm5.ga_code IS DISTINCT FROM w_dbsetup.ga_code OR
					t_ghm5.da_gp_code IS DISTINCT FROM w_dbsetup.da_gp_code OR
					t_ghm5.da_gp_ga_code IS DISTINCT FROM w_dbsetup.da_gp_ga_code
				);
			
			
			INSERT INTO pmsi.t_ghm5(oid, code, texte, severite, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code)
			SELECT oid, code, texte, severite,da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_ghm5);
			
			
			INSERT INTO pmsi.t_ghm5(code, texte, severite, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code)
			SELECT code, texte, severite, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_ghm5);
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_ghm_racines" label="Racine de GHM">
		<selectCmd><![CDATA[

			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM pmsi.t_ghm_racines
			ORDER BY code
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_ghm_racines SET
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE
				t_ghm_racines.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghm_racines SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE
				t_ghm_racines.code = w_dbsetup.code
				AND (
					t_ghm_racines.texte IS DISTINCT FROM w_dbsetup.texte
				);


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


			INSERT INTO pmsi.t_ghm_racines(code, texte)
			SELECT code, texte
			FROM w_dbsetup
			WHERE
				oid <> 0
				AND code NOT IN (SELECT code FROM pmsi.t_ghm_racines);

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ghm_da" label="GHM DA">
		<selectCmd><![CDATA[

			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM pmsi.t_ghm_da
			ORDER BY code
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_ghm_da SET
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE
				t_ghm_da.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghm_da SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE
				t_ghm_da.code = w_dbsetup.code
				AND (
					t_ghm_da.texte IS DISTINCT FROM w_dbsetup.texte
				);


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


			INSERT INTO pmsi.t_ghm_da(code, texte)
			SELECT code, texte
			FROM w_dbsetup
			WHERE
				oid <> 0
				AND code NOT IN (SELECT code FROM pmsi.t_ghm_da);

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_ghm_ga" label="GHM GA">
		<selectCmd><![CDATA[

			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM pmsi.t_ghm_ga
			ORDER BY code
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_ghm_ga SET
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE
				t_ghm_ga.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghm_ga SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE
				t_ghm_ga.code = w_dbsetup.code
				AND (
					t_ghm_ga.texte IS DISTINCT FROM w_dbsetup.texte
				);


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


			INSERT INTO pmsi.t_ghm_ga(code, texte)
			SELECT code, texte
			FROM w_dbsetup
			WHERE
				oid <> 0
				AND code NOT IN (SELECT code FROM pmsi.t_ghm_ga);

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_ghm_gp" label="GHM GP">
		<selectCmd><![CDATA[

			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM pmsi.t_ghm_gp
			ORDER BY code
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_ghm_gp SET
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE
				t_ghm_gp.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghm_gp SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE
				t_ghm_gp.code = w_dbsetup.code
				AND (
					t_ghm_gp.texte IS DISTINCT FROM w_dbsetup.texte
				);


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


			INSERT INTO pmsi.t_ghm_gp(code, texte)
			SELECT code, texte
			FROM w_dbsetup
			WHERE
				oid <> 0
				AND code NOT IN (SELECT code FROM pmsi.t_ghm_gp);

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ghm" label="GHM">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_ghm.oid = 0 THEN t_ghm.oid ELSE 1 END AS oid,
				t_ghm.code, 
				t_ghm.texte, 
				t_ghm.version_groupage,
				t_ghm.ghm3, 
				t_ghm.mco, 
				t_ghm.aso, 
				t_ghm.severite, 
				t_ghm.statut, 
				t_ghm.taux_cma_national, 
				t_ghm.code_fg11, 
				t_ghm.ht_fg11, 
				0 AS dms_1, 
				t_ghm.dms_2, 
				t_ghm.dms_3,
				t_ghm.dms_hospidiag,
				t_cmd.code AS cmd_code, 
				t_groupes_activite.code AS groupe_activite_code, 
				t_ghm5.code as ghm5_code, 
				t_lignes_oap.code as ligne_oap_code,
				t_cas.code as cas_code
			FROM 
				pmsi.t_ghm
				JOIN pmsi.t_cmd ON t_ghm.cmd_id = t_cmd.oid
				JOIN pmsi.t_groupes_activite ON t_ghm.groupe_activite_id = t_groupes_activite.oid
				JOIN pmsi.t_ghm5 ON t_ghm.ghm5_id = t_ghm5.oid
				JOIN pmsi.t_lignes_oap ON t_ghm.ligne_oap_id = t_lignes_oap.oid
				JOIN base.t_cas ON t_ghm.cas_id = t_cas.oid
			ORDER BY t_ghm.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_ghm
				SET code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				w_dbsetup.oid = t_ghm.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_ghm
				SET texte = w_dbsetup.texte , 
				version_groupage = w_dbsetup.version_groupage, 
				ghm3 = w_dbsetup.ghm3, 
				mco = w_dbsetup.mco, 
				aso = w_dbsetup.aso, 
				severite = w_dbsetup.severite, 
				cmd_id = t_cmd.oid, 
				groupe_activite_id = t_groupes_activite.oid, 
				ghm5_id = t_ghm5.oid, 
				ligne_oap_id = t_lignes_oap.oid, 
				statut = w_dbsetup.statut,
				taux_cma_national = w_dbsetup.taux_cma_national,
				code_fg11 = w_dbsetup.code_fg11,
				ht_fg11 = w_dbsetup.ht_fg11,
				cas_id = t_cas.oid,
				dms_2 = w_dbsetup.dms_2,
				dms_3 = w_dbsetup.dms_3,
				dms_hospidiag = w_dbsetup.dms_hospidiag
			FROM
				w_dbsetup
				JOIN pmsi.t_cmd ON w_dbsetup.cmd_code = t_cmd.code
				JOIN pmsi.t_ghm5 ON w_dbsetup.ghm5_code = t_ghm5.code
				JOIN pmsi.t_groupes_activite ON w_dbsetup.groupe_activite_code = t_groupes_activite.code
				JOIN pmsi.t_lignes_oap ON w_dbsetup.ligne_oap_code = t_lignes_oap.code
				JOIN base.t_cas ON w_dbsetup.cas_code = t_cas.code
			WHERE 
				w_dbsetup.code = t_ghm.code
			;
			
			
			INSERT INTO pmsi.t_ghm(
				oid, 
				code, 
				texte, 
				version_groupage, 
				ghm3, 
				mco, 
				aso, 
				severite, 
				cmd_id, 
				groupe_activite_id, 
				ghm5_id, 
				ligne_oap_id, 
				statut, 
				taux_cma_national, 
				code_fg11, 
				ht_fg11, 
				cas_id,
				dms_2,
				dms_3,
				dms_hospidiag)
			SELECT 
				w_dbsetup.oid, 
				w_dbsetup.code, 
				w_dbsetup.texte,
				w_dbsetup.version_groupage, 
				w_dbsetup.ghm3,
				w_dbsetup.mco, 
				w_dbsetup.aso, 
				w_dbsetup.severite,
				t_cmd.oid, 
				t_groupes_activite.oid, 
				t_ghm5.oid, 
				t_lignes_oap.oid, 
				statut, 
				taux_cma_national, 
				w_dbsetup.code_fg11,
				w_dbsetup.ht_fg11, 
				t_cas.oid,
				w_dbsetup.dms_2,
				w_dbsetup.dms_3,
				w_dbsetup.dms_hospidiag
			FROM 
				w_dbsetup
				JOIN pmsi.t_cmd ON w_dbsetup.cmd_code = t_cmd.code
				JOIN pmsi.t_ghm5 ON w_dbsetup.ghm5_code = t_ghm5.code
				JOIN pmsi.t_groupes_activite ON w_dbsetup.groupe_activite_code = t_groupes_activite.code
				JOIN pmsi.t_lignes_oap ON w_dbsetup.ligne_oap_code = t_lignes_oap.code
				JOIN base.t_cas ON w_dbsetup.cas_code = t_cas.code
			WHERE 
				w_dbsetup.oid = 0
				AND NOT EXISTS (
					SELECT 1
					FROM pmsi.t_ghm
					WHERE w_dbsetup.code = t_ghm.code
					)
			;
			
			
			INSERT INTO pmsi.t_ghm(
				code, 
				texte,
				version_groupage,
				ghm3, 
				mco, 
				aso, 
				severite, 
				cmd_id, 
				groupe_activite_id, 
				ghm5_id, 
				ligne_oap_id,
				statut, 
				taux_cma_national, 
				code_fg11, 
				ht_fg11, 
				cas_id,
				dms_2,
				dms_3,
				dms_hospidiag)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte,
				w_dbsetup.version_groupage, 
				w_dbsetup.ghm3, 
				w_dbsetup.mco, 
				w_dbsetup.aso, 
				w_dbsetup.severite, 
				t_cmd.oid, 
				t_groupes_activite.oid, 
				t_ghm5.oid, 
				t_lignes_oap.oid, 
				statut, 
				taux_cma_national, 
				w_dbsetup.code_fg11, 
				w_dbsetup.ht_fg11, 
				t_cas.oid,
				w_dbsetup.dms_2,
				w_dbsetup.dms_3,
				w_dbsetup.dms_hospidiag
			FROM 
				w_dbsetup
				JOIN pmsi.t_cmd ON w_dbsetup.cmd_code = t_cmd.code
				JOIN pmsi.t_ghm5 ON w_dbsetup.ghm5_code = t_ghm5.code
				JOIN pmsi.t_groupes_activite ON w_dbsetup.groupe_activite_code = t_groupes_activite.code
				JOIN pmsi.t_lignes_oap ON w_dbsetup.ligne_oap_code = t_lignes_oap.code
				JOIN base.t_cas ON w_dbsetup.cas_code = t_cas.code
			WHERE 
				w_dbsetup.oid <> 0 
			    AND NOT EXISTS (
					SELECT 1
					FROM pmsi.t_ghm
					WHERE w_dbsetup.code = t_ghm.code
				)
			;
			
			SELECT * FROM pmsi.cti_reorganize_ghm_c();
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ghm_racines_regroupements" label="Table de jointure des regroupement de racine de GHM">
		<selectCmd><![CDATA[

			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				ghm_racine_code,
				da_code,
				gp_code,
				ga_code,
				da_gp_code,
				da_gp_ga_code,
				annee_pmsi
			FROM pmsi.t_ghm_racines_regroupements
			ORDER BY ghm_racine_code, annee_pmsi DESC, da_gp_ga_code
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_ghm_racines_regroupements SET
				ghm_racine_code = w_dbsetup.ghm_racine_code
			FROM w_dbsetup
			WHERE
				t_ghm_racines_regroupements.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghm_racines_regroupements SET
				da_code = w_dbsetup.da_code,
				gp_code = w_dbsetup.gp_code,
				ga_code = w_dbsetup.ga_code,
				da_gp_code = w_dbsetup.da_gp_code,
				da_gp_ga_code = w_dbsetup.da_gp_ga_code
			FROM w_dbsetup
			WHERE
				t_ghm_racines_regroupements.ghm_racine_code = w_dbsetup.ghm_racine_code
				AND t_ghm_racines_regroupements.annee_pmsi = w_dbsetup.annee_pmsi
				AND (
					t_ghm_racines_regroupements.da_code IS DISTINCT FROM w_dbsetup.da_code OR
					t_ghm_racines_regroupements.gp_code IS DISTINCT FROM w_dbsetup.gp_code OR
					t_ghm_racines_regroupements.ga_code IS DISTINCT FROM w_dbsetup.ga_code OR
					t_ghm_racines_regroupements.da_gp_code IS DISTINCT FROM w_dbsetup.da_gp_code OR
					t_ghm_racines_regroupements.da_gp_ga_code IS DISTINCT FROM w_dbsetup.da_gp_ga_code
				);


			INSERT INTO pmsi.t_ghm_racines_regroupements(oid, ghm_racine_code, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code, annee_pmsi)
			SELECT oid, ghm_racine_code, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code, annee_pmsi
			FROM w_dbsetup
			WHERE
				oid = 0
				AND oid NOT IN (SELECT oid FROM pmsi.t_ghm_racines_regroupements);


		    WITH w_annee_pmsi AS (
        	SELECT DISTINCT annee_pmsi FROM pmsi.t_ghm_racines_regroupements
				)
				INSERT INTO pmsi.t_ghm_racines_regroupements (
					ghm_racine_code,
					da_code,
					gp_code,
					ga_code,
					da_gp_code,
					da_gp_ga_code,
					annee_pmsi
					)
					SELECT
						ghm_racine_code,
						da_code,
						gp_code,
						ga_code,
						da_gp_code,
						da_gp_ga_code,
						annee_pmsi
					FROM w_dbsetup
					WHERE oid <> 0
						AND (ghm_racine_code NOT IN (SELECT ghm_racine_code FROM pmsi.t_ghm_racines_regroupements)
						OR annee_pmsi NOT IN (SELECT annee_pmsi FROM w_annee_pmsi))

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_ghm_regroupements" label="Table de jointure des regroupement de racine de GHM">
		<selectCmd><![CDATA[

			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				ghm_code,
				da_code,
				gp_code,
				ga_code,
				da_gp_code,
				da_gp_ga_code,
				annee_pmsi
			FROM pmsi.t_ghm_regroupements
			ORDER BY ghm_code, annee_pmsi DESC
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_ghm_regroupements SET
				ghm_code = w_dbsetup.ghm_code
			FROM w_dbsetup
			WHERE
				t_ghm_regroupements.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghm_regroupements SET
				da_code = w_dbsetup.da_code,
				gp_code = w_dbsetup.gp_code,
				ga_code = w_dbsetup.ga_code,
				da_gp_code = w_dbsetup.da_gp_code,
				da_gp_ga_code = w_dbsetup.da_gp_ga_code
			FROM w_dbsetup
			WHERE
				t_ghm_regroupements.ghm_code = w_dbsetup.ghm_code
				AND t_ghm_regroupements.annee_pmsi = w_dbsetup.annee_pmsi
				AND (
					t_ghm_regroupements.da_code IS DISTINCT FROM w_dbsetup.da_code OR
					t_ghm_regroupements.gp_code IS DISTINCT FROM w_dbsetup.gp_code OR
					t_ghm_regroupements.ga_code IS DISTINCT FROM w_dbsetup.ga_code OR
					t_ghm_regroupements.da_gp_code IS DISTINCT FROM w_dbsetup.da_gp_code OR
					t_ghm_regroupements.da_gp_ga_code IS DISTINCT FROM w_dbsetup.da_gp_ga_code
				);


			INSERT INTO pmsi.t_ghm_regroupements(oid, ghm_code, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code, annee_pmsi)
			SELECT oid, ghm_code, da_code, gp_code, ga_code, da_gp_code, da_gp_ga_code, annee_pmsi
			FROM w_dbsetup
			WHERE
				oid = 0
				AND oid NOT IN (SELECT oid FROM pmsi.t_ghm_regroupements);


			INSERT INTO pmsi.t_ghm_regroupements (
				ghm_code,
				da_code,
				gp_code,
				ga_code,
				da_gp_code,
				da_gp_ga_code,
				annee_pmsi
				)
				SELECT
					ghm_code,
					da_code,
					gp_code,
					ga_code,
					da_gp_code,
					da_gp_ga_code,
					annee_pmsi
				FROM w_dbsetup
				WHERE oid <> 0
				AND NOT EXISTS (
					SELECT 1
					FROM pmsi.t_ghm_regroupements
					WHERE t_ghm_regroupements.ghm_code = w_dbsetup.ghm_code
					AND t_ghm_regroupements.annee_pmsi = w_dbsetup.annee_pmsi
				)
				;


		]]></updateCmd>
	</TABLE>

	<TABLE name="t_divers" label="Divers">
		<selectCmd><![CDATA[ 
            SELECT 
                t_divers.code, 
                t_divers.description,
                t_divers.show_info_module,
                t_divers.texte,
                t_divers.valeur,
                t_divers.valeur_date::text
            FROM 
                pmsi.t_divers
            ORDER BY code; 
        ]]>		</selectCmd>
		<updateCmd><![CDATA[ 

			UPDATE pmsi.t_divers SET
				texte = w_dbsetup.texte,
				description = w_dbsetup.description
			FROM w_dbsetup
			WHERE
				t_divers.code = w_dbsetup.code
				AND (
					t_divers.texte IS DISTINCT FROM w_dbsetup.texte OR
					t_divers.description IS DISTINCT FROM w_dbsetup.description
				);

            INSERT INTO pmsi.t_divers(code, description, show_info_module, texte, valeur, valeur_date)
                SELECT 
                    w_dbsetup.code,
                    w_dbsetup.description,
                    w_dbsetup.show_info_module,
                    w_dbsetup.texte,
                    w_dbsetup.valeur,
                    CASE WHEN w_dbsetup.valeur_date = '' THEN '19700101'::date ELSE w_dbsetup.valeur_date::date END AS valeur_date
                FROM
                    w_dbsetup
                WHERE w_dbsetup.code NOT IN (SELECT code from pmsi.t_divers)
            ;

            UPDATE pmsi.t_divers SET
				valeur_date = date_trunc('year', (now() - interval '1 year'))::date,
				valeur = to_char(date_trunc('year', (now() - interval '1 year')), 'DD/MM/YYYY')
			WHERE 1=1
				AND code = 'EXPERT_DATE_DEBUT'
				AND valeur_date < date_trunc('year', (now() - interval '1 year'))::date;

            
        ]]>		</updateCmd>
	</TABLE>

	<TABLE name="t_ghm_indicateurs_nationaux" label="GHM">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_ghm.code, 
				t_ghm_indicateurs_nationaux.nb_rsa, 
				t_ghm_indicateurs_nationaux.nb_rsa_prive, 
				t_ghm_indicateurs_nationaux.nb_rsa_public, 
				t_ghm_indicateurs_nationaux.nb_rsa_ambu_ess,
				t_ghm_indicateurs_nationaux.nb_rsa_ambu_ess_prive,
				t_ghm_indicateurs_nationaux.nb_rsa_ambu_ess_public,
				t_ghm_indicateurs_nationaux.age, 
				t_ghm_indicateurs_nationaux.age_prive, 
				t_ghm_indicateurs_nationaux.age_public, 
				t_ghm_indicateurs_nationaux.nb_deces, 
				t_ghm_indicateurs_nationaux.nb_deces_prive, 
				t_ghm_indicateurs_nationaux.nb_deces_public, 
				t_ghm_indicateurs_nationaux.nb_0n_chir, 
				t_ghm_indicateurs_nationaux.nb_0n_chir_prive, 
				t_ghm_indicateurs_nationaux.nb_0n_chir_public, 
				t_ghm_indicateurs_nationaux.nb_chir, 
				t_ghm_indicateurs_nationaux.nb_chir_prive, 
				t_ghm_indicateurs_nationaux.nb_chir_public,
				t_ghm_indicateurs_nationaux.nb_sev_j,
				t_ghm_indicateurs_nationaux.nb_sev_j_prive,
				t_ghm_indicateurs_nationaux.nb_sev_j_public,
				t_ghm_indicateurs_nationaux.nb_sev_t,
				t_ghm_indicateurs_nationaux.nb_sev_t_prive,
				t_ghm_indicateurs_nationaux.nb_sev_t_public,
				t_ghm_indicateurs_nationaux.nb_sev_a,
				t_ghm_indicateurs_nationaux.nb_sev_a_prive,
				t_ghm_indicateurs_nationaux.nb_sev_a_public,
				t_ghm_indicateurs_nationaux.nb_sev_b,
				t_ghm_indicateurs_nationaux.nb_sev_b_prive,
				t_ghm_indicateurs_nationaux.nb_sev_b_public,
				t_ghm_indicateurs_nationaux.nb_sev_c,
				t_ghm_indicateurs_nationaux.nb_sev_c_prive,
				t_ghm_indicateurs_nationaux.nb_sev_c_public,
				t_ghm_indicateurs_nationaux.nb_sev_d,
				t_ghm_indicateurs_nationaux.nb_sev_d_prive,
				t_ghm_indicateurs_nationaux.nb_sev_d_public,
				t_ghm_indicateurs_nationaux.nb_sev_1,
				t_ghm_indicateurs_nationaux.nb_sev_1_prive,
				t_ghm_indicateurs_nationaux.nb_sev_1_public,
				t_ghm_indicateurs_nationaux.nb_sev_2,
				t_ghm_indicateurs_nationaux.nb_sev_2_prive,
				t_ghm_indicateurs_nationaux.nb_sev_2_public,
				t_ghm_indicateurs_nationaux.nb_sev_3,
				t_ghm_indicateurs_nationaux.nb_sev_3_prive,
				t_ghm_indicateurs_nationaux.nb_sev_3_public,
				t_ghm_indicateurs_nationaux.nb_sev_4,
				t_ghm_indicateurs_nationaux.nb_sev_4_prive,
				t_ghm_indicateurs_nationaux.nb_sev_4_public,
				t_ghm_indicateurs_nationaux.nb_sev_e,
				t_ghm_indicateurs_nationaux.nb_sev_e_prive,
				t_ghm_indicateurs_nationaux.nb_sev_e_public,
				t_ghm_indicateurs_nationaux.nb_sev_z,
				t_ghm_indicateurs_nationaux.nb_sev_z_prive,
				t_ghm_indicateurs_nationaux.nb_sev_z_public,
				t_ghm_indicateurs_nationaux.nb_sev_zs,
				t_ghm_indicateurs_nationaux.nb_sev_zs_prive,
				t_ghm_indicateurs_nationaux.nb_sev_zs_public,
				t_ghm_indicateurs_nationaux.nb_seances,
				t_ghm_indicateurs_nationaux.nb_seances_prive,
				t_ghm_indicateurs_nationaux.nb_seances_public			
			FROM 
				pmsi.t_ghm_indicateurs_nationaux 
				JOIN pmsi.t_ghm ON t_ghm.oid = t_ghm_indicateurs_nationaux.ghm_id 
			ORDER BY t_ghm.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_ghm_indicateurs_nationaux SET 
				nb_rsa = w_dbsetup.nb_rsa, 
				nb_rsa_prive = w_dbsetup.nb_rsa_prive, 
				nb_rsa_public = w_dbsetup.nb_rsa_public, 
				nb_rsa_ambu_ess = w_dbsetup.nb_rsa_ambu_ess, 
				nb_rsa_ambu_ess_prive = w_dbsetup.nb_rsa_ambu_ess_prive, 
				nb_rsa_ambu_ess_public = w_dbsetup.nb_rsa_ambu_ess_public, 
				age = w_dbsetup.age, 
				age_prive = w_dbsetup.age_prive, 
				age_public = w_dbsetup.age_public, 
				nb_deces = w_dbsetup.nb_deces, 
				nb_deces_prive = w_dbsetup.nb_deces_prive, 
				nb_deces_public = w_dbsetup.nb_deces_public, 
				nb_0n_chir = w_dbsetup.nb_0n_chir, 
				nb_0n_chir_prive = w_dbsetup.nb_0n_chir_prive, 
				nb_0n_chir_public = w_dbsetup.nb_0n_chir_public, 
				nb_chir = w_dbsetup.nb_chir, 
				nb_chir_prive = w_dbsetup.nb_chir_prive, 
				nb_chir_public = w_dbsetup.nb_chir_public,
				nb_sev_j = w_dbsetup.nb_sev_j,
				nb_sev_j_prive = w_dbsetup.nb_sev_j_prive,
				nb_sev_j_public = w_dbsetup.nb_sev_j_public,
				nb_sev_t = w_dbsetup.nb_sev_t,
				nb_sev_t_prive = w_dbsetup.nb_sev_t_prive,
				nb_sev_t_public = w_dbsetup.nb_sev_t_public,
				nb_sev_a = w_dbsetup.nb_sev_a,
				nb_sev_a_prive = w_dbsetup.nb_sev_a_prive,
				nb_sev_a_public = w_dbsetup.nb_sev_a_public,
				nb_sev_b = w_dbsetup.nb_sev_b,
				nb_sev_b_prive = w_dbsetup.nb_sev_b_prive,
				nb_sev_b_public = w_dbsetup.nb_sev_b_public,
				nb_sev_c = w_dbsetup.nb_sev_c,
				nb_sev_c_prive = w_dbsetup.nb_sev_c_prive,
				nb_sev_c_public = w_dbsetup.nb_sev_c_public,
				nb_sev_d = w_dbsetup.nb_sev_d,
				nb_sev_d_prive = w_dbsetup.nb_sev_d_prive,
				nb_sev_d_public = w_dbsetup.nb_sev_d_public,
				nb_sev_1 = w_dbsetup.nb_sev_1,
				nb_sev_1_prive = w_dbsetup.nb_sev_1_prive,
				nb_sev_1_public = w_dbsetup.nb_sev_1_public,
				nb_sev_2 = w_dbsetup.nb_sev_2,
				nb_sev_2_prive = w_dbsetup.nb_sev_2_prive,
				nb_sev_2_public = w_dbsetup.nb_sev_2_public,
				nb_sev_3 = w_dbsetup.nb_sev_3,
				nb_sev_3_prive = w_dbsetup.nb_sev_3_prive,
				nb_sev_3_public = w_dbsetup.nb_sev_3_public,
				nb_sev_4 = w_dbsetup.nb_sev_4,
				nb_sev_4_prive = w_dbsetup.nb_sev_4_prive,
				nb_sev_4_public = w_dbsetup.nb_sev_4_public,
				nb_sev_e = w_dbsetup.nb_sev_e,
				nb_sev_e_prive = w_dbsetup.nb_sev_e_prive,
				nb_sev_e_public = w_dbsetup.nb_sev_e_public,
				nb_sev_z = w_dbsetup.nb_sev_z,
				nb_sev_z_prive = w_dbsetup.nb_sev_z_prive,
				nb_sev_z_public = w_dbsetup.nb_sev_z_public,
				nb_sev_zs = w_dbsetup.nb_sev_zs,
				nb_sev_zs_prive = w_dbsetup.nb_sev_zs_prive,
				nb_sev_zs_public = w_dbsetup.nb_sev_zs_public,
				nb_seances = w_dbsetup.nb_seances,
				nb_seances_prive = w_dbsetup.nb_seances_prive,
				nb_seances_public = w_dbsetup.nb_seances_public			
			FROM
				w_dbsetup
				JOIN pmsi.t_ghm ON t_ghm.code = w_dbsetup.code
			WHERE 
				t_ghm.oid = t_ghm_indicateurs_nationaux.ghm_id
				AND (
					t_ghm_indicateurs_nationaux.nb_rsa <> w_dbsetup.nb_rsa OR 
					t_ghm_indicateurs_nationaux.nb_rsa_prive <> w_dbsetup.nb_rsa_prive OR 
					t_ghm_indicateurs_nationaux.nb_rsa_public <> w_dbsetup.nb_rsa_public OR 
					t_ghm_indicateurs_nationaux.nb_rsa_ambu_ess <> w_dbsetup.nb_rsa_ambu_ess OR 
					t_ghm_indicateurs_nationaux.nb_rsa_ambu_ess_prive <> w_dbsetup.nb_rsa_ambu_ess_prive OR 
					t_ghm_indicateurs_nationaux.nb_rsa_ambu_ess_public <> w_dbsetup.nb_rsa_ambu_ess_public OR 
					t_ghm_indicateurs_nationaux.age <> w_dbsetup.age OR 
					t_ghm_indicateurs_nationaux.age_prive <> w_dbsetup.age_prive OR 
					t_ghm_indicateurs_nationaux.age_public <> w_dbsetup.age_public OR 
					t_ghm_indicateurs_nationaux.nb_deces <> w_dbsetup.nb_deces OR 
					t_ghm_indicateurs_nationaux.nb_deces_prive <> w_dbsetup.nb_deces_prive OR 
					t_ghm_indicateurs_nationaux.nb_deces_public <> w_dbsetup.nb_deces_public OR 
					t_ghm_indicateurs_nationaux.nb_0n_chir <> w_dbsetup.nb_0n_chir OR 
					t_ghm_indicateurs_nationaux.nb_0n_chir_prive <> w_dbsetup.nb_0n_chir_prive OR 
					t_ghm_indicateurs_nationaux.nb_0n_chir_public <> w_dbsetup.nb_0n_chir_public OR 
					t_ghm_indicateurs_nationaux.nb_chir <> w_dbsetup.nb_chir OR 
					t_ghm_indicateurs_nationaux.nb_chir_prive <> w_dbsetup.nb_chir_prive OR 
					t_ghm_indicateurs_nationaux.nb_chir_public <> w_dbsetup.nb_chir_public OR
					t_ghm_indicateurs_nationaux.nb_sev_j <> w_dbsetup.nb_sev_j OR
					t_ghm_indicateurs_nationaux.nb_sev_j_prive <> w_dbsetup.nb_sev_j_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_j_public <> w_dbsetup.nb_sev_j_public OR
					t_ghm_indicateurs_nationaux.nb_sev_t <> w_dbsetup.nb_sev_t OR
					t_ghm_indicateurs_nationaux.nb_sev_t_prive <> w_dbsetup.nb_sev_t_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_t_public <> w_dbsetup.nb_sev_t_public OR
					t_ghm_indicateurs_nationaux.nb_sev_a <> w_dbsetup.nb_sev_a OR
					t_ghm_indicateurs_nationaux.nb_sev_a_prive <> w_dbsetup.nb_sev_a_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_a_public <> w_dbsetup.nb_sev_a_public OR
					t_ghm_indicateurs_nationaux.nb_sev_b <> w_dbsetup.nb_sev_b OR
					t_ghm_indicateurs_nationaux.nb_sev_b_prive <> w_dbsetup.nb_sev_b_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_b_public <> w_dbsetup.nb_sev_b_public OR
					t_ghm_indicateurs_nationaux.nb_sev_c <> w_dbsetup.nb_sev_c OR
					t_ghm_indicateurs_nationaux.nb_sev_c_prive <> w_dbsetup.nb_sev_c_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_c_public <> w_dbsetup.nb_sev_c_public OR
					t_ghm_indicateurs_nationaux.nb_sev_d <> w_dbsetup.nb_sev_d OR
					t_ghm_indicateurs_nationaux.nb_sev_d_prive <> w_dbsetup.nb_sev_d_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_d_public <> w_dbsetup.nb_sev_d_public OR
					t_ghm_indicateurs_nationaux.nb_sev_1 <> w_dbsetup.nb_sev_1 OR
					t_ghm_indicateurs_nationaux.nb_sev_1_prive <> w_dbsetup.nb_sev_1_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_1_public <> w_dbsetup.nb_sev_1_public OR
					t_ghm_indicateurs_nationaux.nb_sev_2 <> w_dbsetup.nb_sev_2 OR
					t_ghm_indicateurs_nationaux.nb_sev_2_prive <> w_dbsetup.nb_sev_2_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_2_public <> w_dbsetup.nb_sev_2_public OR
					t_ghm_indicateurs_nationaux.nb_sev_3 <> w_dbsetup.nb_sev_3 OR
					t_ghm_indicateurs_nationaux.nb_sev_3_prive <> w_dbsetup.nb_sev_3_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_3_public <> w_dbsetup.nb_sev_3_public OR
					t_ghm_indicateurs_nationaux.nb_sev_4 <> w_dbsetup.nb_sev_4 OR
					t_ghm_indicateurs_nationaux.nb_sev_4_prive <> w_dbsetup.nb_sev_4_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_4_public <> w_dbsetup.nb_sev_4_public OR
					t_ghm_indicateurs_nationaux.nb_sev_e <> w_dbsetup.nb_sev_e OR
					t_ghm_indicateurs_nationaux.nb_sev_e_prive <> w_dbsetup.nb_sev_e_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_e_public <> w_dbsetup.nb_sev_e_public OR
					t_ghm_indicateurs_nationaux.nb_sev_z <> w_dbsetup.nb_sev_z OR
					t_ghm_indicateurs_nationaux.nb_sev_z_prive <> w_dbsetup.nb_sev_z_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_z_public <> w_dbsetup.nb_sev_z_public OR
					t_ghm_indicateurs_nationaux.nb_sev_zs <> w_dbsetup.nb_sev_zs OR
					t_ghm_indicateurs_nationaux.nb_sev_zs_prive <> w_dbsetup.nb_sev_zs_prive OR
					t_ghm_indicateurs_nationaux.nb_sev_zs_public <> w_dbsetup.nb_sev_zs_public OR
					t_ghm_indicateurs_nationaux.nb_seances <> w_dbsetup.nb_seances OR
					t_ghm_indicateurs_nationaux.nb_seances_prive <> w_dbsetup.nb_seances_prive OR
					t_ghm_indicateurs_nationaux.nb_seances_public <> w_dbsetup.nb_seances_public
				)
			;
			
			
			INSERT INTO pmsi.t_ghm_indicateurs_nationaux (
				ghm_id, 
				nb_rsa,
				nb_rsa_prive, 
				nb_rsa_public, 
				nb_rsa_ambu_ess,
				nb_rsa_ambu_ess_prive,
				nb_rsa_ambu_ess_public,
				age, 
				age_prive, 
				age_public, 
				nb_deces, 
				nb_deces_prive, 
				nb_deces_public, 
				nb_0n_chir, 
				nb_0n_chir_prive, 
				nb_0n_chir_public, 
				nb_chir, 
				nb_chir_prive, 
				nb_chir_public,
				nb_sev_j,
				nb_sev_j_prive,
				nb_sev_j_public,
				nb_sev_t,
				nb_sev_t_prive,
				nb_sev_t_public,
				nb_sev_a,
				nb_sev_a_prive,
				nb_sev_a_public,
				nb_sev_b,
				nb_sev_b_prive,
				nb_sev_b_public,
				nb_sev_c,
				nb_sev_c_prive,
				nb_sev_c_public,
				nb_sev_d,
				nb_sev_d_prive,
				nb_sev_d_public,
				nb_sev_1,
				nb_sev_1_prive,
				nb_sev_1_public,
				nb_sev_2,
				nb_sev_2_prive,
				nb_sev_2_public,
				nb_sev_3,
				nb_sev_3_prive,
				nb_sev_3_public,
				nb_sev_4,
				nb_sev_4_prive,
				nb_sev_4_public,
				nb_sev_e,
				nb_sev_e_prive,
				nb_sev_e_public,
				nb_sev_z,
				nb_sev_z_prive,
				nb_sev_z_public,
				nb_sev_zs,
				nb_sev_zs_prive,
				nb_sev_zs_public,
				nb_seances,
				nb_seances_prive,
				nb_seances_public) 
			SELECT 
				t_ghm.oid, 
				w_dbsetup.nb_rsa, 
				w_dbsetup.nb_rsa_prive, 
				w_dbsetup.nb_rsa_public, 
				w_dbsetup.nb_rsa_ambu_ess,
				w_dbsetup.nb_rsa_ambu_ess_prive,
				w_dbsetup.nb_rsa_ambu_ess_public,
				w_dbsetup.age, 
				w_dbsetup.age_prive, 
				w_dbsetup.age_public, 
				w_dbsetup.nb_deces, 
				w_dbsetup.nb_deces_prive, 
				w_dbsetup.nb_deces_public, 
				w_dbsetup.nb_0n_chir, 
				w_dbsetup.nb_0n_chir_prive, 
				w_dbsetup.nb_0n_chir_public, 
				w_dbsetup.nb_chir, 
				w_dbsetup.nb_chir_prive, 
				w_dbsetup.nb_chir_public,
				w_dbsetup.nb_sev_j,
				w_dbsetup.nb_sev_j_prive,
				w_dbsetup.nb_sev_j_public,
				w_dbsetup.nb_sev_t,
				w_dbsetup.nb_sev_t_prive,
				w_dbsetup.nb_sev_t_public,
				w_dbsetup.nb_sev_a,
				w_dbsetup.nb_sev_a_prive,
				w_dbsetup.nb_sev_a_public,
				w_dbsetup.nb_sev_b,
				w_dbsetup.nb_sev_b_prive,
				w_dbsetup.nb_sev_b_public,
				w_dbsetup.nb_sev_c,
				w_dbsetup.nb_sev_c_prive,
				w_dbsetup.nb_sev_c_public,
				w_dbsetup.nb_sev_d,
				w_dbsetup.nb_sev_d_prive,
				w_dbsetup.nb_sev_d_public,
				w_dbsetup.nb_sev_1,
				w_dbsetup.nb_sev_1_prive,
				w_dbsetup.nb_sev_1_public,
				w_dbsetup.nb_sev_2,
				w_dbsetup.nb_sev_2_prive,
				w_dbsetup.nb_sev_2_public,
				w_dbsetup.nb_sev_3,
				w_dbsetup.nb_sev_3_prive,
				w_dbsetup.nb_sev_3_public,
				w_dbsetup.nb_sev_4,
				w_dbsetup.nb_sev_4_prive,
				w_dbsetup.nb_sev_4_public,
				w_dbsetup.nb_sev_e,
				w_dbsetup.nb_sev_e_prive,
				w_dbsetup.nb_sev_e_public,
				w_dbsetup.nb_sev_z,
				w_dbsetup.nb_sev_z_prive,
				w_dbsetup.nb_sev_z_public,
				w_dbsetup.nb_sev_zs,
				w_dbsetup.nb_sev_zs_prive,
				w_dbsetup.nb_sev_zs_public,
				w_dbsetup.nb_seances,
				w_dbsetup.nb_seances_prive,
				w_dbsetup.nb_seances_public
			FROM 
				w_dbsetup JOIN pmsi.t_ghm ON t_ghm.code = w_dbsetup.code 
			WHERE w_dbsetup.code NOT IN (SELECT t_ghm.code 
											FROM pmsi.t_ghm_indicateurs_nationaux 
											JOIN pmsi.t_ghm ON t_ghm.oid = t_ghm_indicateurs_nationaux.ghm_id)
			;
			
			SELECT * FROM pmsi.cti_reorganize_ghm_c();
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ghs" label="GHS" >
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_ghs.oid = 0 THEN t_ghs.oid ELSE 1 END AS oid,
				t_ghs.texte, 
				t_ghs.code, 
				t_ghs.code_text, 
				t_ghs.borne_basse, 
				t_ghs.borne_haute, 
				t_ghs.dms_nationale,
				t_ghs.tarif_ghs,
				t_ghs.tarif_exh,
				t_ghs.date_debut_prive_1,
				t_ghs.date_fin_prive_1,
				t_ghs.borne_basse_prive_1,
				t_ghs.borne_basse_public_1,
				t_ghs.borne_haute_prive_1,
				t_ghs.borne_haute_public_1,
				t_ghs.dms_nationale_prive_1,
				t_ghs.dms_nationale_public_1,
				t_ghs.tarif_ghs_prive_1,
				t_ghs.tarif_ghs_public_1,
				t_ghs.tarif_exh_prive_1,
				t_ghs.tarif_exh_public_1,
				t_ghs.date_debut_prive_2,
				t_ghs.date_fin_prive_2,
				t_ghs.borne_basse_prive_2,
				t_ghs.borne_basse_public_2,
				t_ghs.borne_haute_prive_2,
				t_ghs.borne_haute_public_2,
				t_ghs.dms_nationale_prive_2,
				t_ghs.dms_nationale_public_2,
				t_ghs.tarif_ghs_prive_2,
				t_ghs.tarif_ghs_public_2,
				t_ghs.tarif_exh_prive_2,
				t_ghs.tarif_exh_public_2,
				t_ghs.date_debut_prive_3,
				t_ghs.date_fin_prive_3,
				t_ghs.borne_basse_prive_3,
				t_ghs.borne_basse_public_3,
				t_ghs.borne_haute_prive_3,
				t_ghs.borne_haute_public_3,
				t_ghs.dms_nationale_prive_3,
				t_ghs.dms_nationale_public_3,
				t_ghs.tarif_ghs_prive_3,
				t_ghs.tarif_ghs_public_3,
				t_ghs.tarif_exh_prive_3,
				t_ghs.tarif_exh_public_3,
				t_ghs.date_debut_prive_4,
				t_ghs.date_fin_prive_4,
				t_ghs.borne_basse_prive_4,
				t_ghs.borne_basse_public_4,
				t_ghs.borne_haute_prive_4,
				t_ghs.borne_haute_public_4,
				t_ghs.dms_nationale_prive_4,
				t_ghs.dms_nationale_public_4,
				t_ghs.tarif_ghs_prive_4,
				t_ghs.tarif_ghs_public_4,
				t_ghs.tarif_exh_prive_4,
				t_ghs.tarif_exh_public_4,
				t_ghs.date_debut_prive_5,
				t_ghs.date_fin_prive_5,
				t_ghs.borne_basse_prive_5,
				t_ghs.borne_basse_public_5,
				t_ghs.borne_haute_prive_5,
				t_ghs.borne_haute_public_5,
				t_ghs.dms_nationale_prive_5,
				t_ghs.dms_nationale_public_5,
				t_ghs.tarif_ghs_prive_5,
				t_ghs.tarif_ghs_public_5,
				t_ghs.tarif_exh_prive_5,
				t_ghs.tarif_exh_public_5,
				t_ghs.date_debut_prive_6,
				t_ghs.date_fin_prive_6,
				t_ghs.borne_basse_prive_6,
				t_ghs.borne_basse_public_6,
				t_ghs.borne_haute_prive_6,
				t_ghs.borne_haute_public_6,
				t_ghs.dms_nationale_prive_6,
				t_ghs.dms_nationale_public_6,
				t_ghs.tarif_ghs_prive_6,
				t_ghs.tarif_ghs_public_6,
				t_ghs.tarif_exh_prive_6,
				t_ghs.tarif_exh_public_6,
				t_ghs.date_debut_prive_7,
				t_ghs.date_fin_prive_7,
				t_ghs.borne_basse_prive_7,
				t_ghs.borne_basse_public_7,
				t_ghs.borne_haute_prive_7,
				t_ghs.borne_haute_public_7,
				t_ghs.dms_nationale_prive_7,
				t_ghs.dms_nationale_public_7,
				t_ghs.tarif_ghs_prive_7,
				t_ghs.tarif_ghs_public_7,
				t_ghs.tarif_exh_prive_7,
				t_ghs.tarif_exh_public_7,
				t_ghs.date_debut_prive_8,
				t_ghs.date_fin_prive_8,
				t_ghs.borne_basse_prive_8,
				t_ghs.borne_basse_public_8,
				t_ghs.borne_haute_prive_8,
				t_ghs.borne_haute_public_8,
				t_ghs.dms_nationale_prive_8,
				t_ghs.dms_nationale_public_8,
				t_ghs.tarif_ghs_prive_8,
				t_ghs.tarif_ghs_public_8,
				t_ghs.tarif_exh_prive_8,
				t_ghs.tarif_exh_public_8,
				t_ghs.date_debut_prive_9,
				t_ghs.date_fin_prive_9,
				t_ghs.borne_basse_prive_9,
				t_ghs.borne_basse_public_9,
				t_ghs.borne_haute_prive_9,
				t_ghs.borne_haute_public_9,
				t_ghs.dms_nationale_prive_9,
				t_ghs.dms_nationale_public_9,
				t_ghs.tarif_ghs_prive_9,
				t_ghs.tarif_ghs_public_9,
				t_ghs.tarif_exh_prive_9,
				t_ghs.tarif_exh_public_9,
				t_ghs.date_debut_public_1,
				t_ghs.date_fin_public_1,
				t_ghs.date_debut_public_2,
				t_ghs.date_fin_public_2,
				t_ghs.date_debut_public_3,
				t_ghs.date_fin_public_3,
				t_ghs.date_debut_public_4,
				t_ghs.date_fin_public_4,
				t_ghs.date_debut_public_5,
				t_ghs.date_fin_public_5,
				t_ghs.date_debut_public_6,
				t_ghs.date_fin_public_6,
				t_ghs.date_debut_public_7,
				t_ghs.date_fin_public_7,
				t_ghs.date_debut_public_8,
				t_ghs.date_fin_public_8,
				t_ghs.date_debut_public_9,
				t_ghs.date_fin_public_9,
				t_ghs.derniere_borne_basse_prive,
				t_ghs.derniere_borne_haute_prive,
				t_ghs.derniere_dms_nationale_prive,
				t_ghs.dernier_tarif_ghs_prive,
				t_ghs.dernier_tarif_exh_prive,
				t_ghs.derniere_borne_basse_public,
				t_ghs.derniere_borne_haute_public,
				t_ghs.derniere_dms_nationale_public,
				t_ghs.dernier_tarif_ghs_public,
				t_ghs.dernier_tarif_exh_public,
				t_ghs.code_fg11,
				t_ghs.forfait_exb_prive_1,
				t_ghs.forfait_exb_prive_2,
				t_ghs.forfait_exb_prive_3,
				t_ghs.forfait_exb_prive_4,
				t_ghs.forfait_exb_prive_5,
				t_ghs.forfait_exb_prive_6,
				t_ghs.forfait_exb_prive_7,
				t_ghs.forfait_exb_prive_8,
				t_ghs.forfait_exb_prive_9,
				t_ghs.tarif_exb_prive_1,
				t_ghs.tarif_exb_prive_2,
				t_ghs.tarif_exb_prive_3,
				t_ghs.tarif_exb_prive_4,
				t_ghs.tarif_exb_prive_5,
				t_ghs.tarif_exb_prive_6,
				t_ghs.tarif_exb_prive_7,
				t_ghs.tarif_exb_prive_8,
				t_ghs.tarif_exb_prive_9,
				t_ghs.forfait_exb_public_1,
				t_ghs.forfait_exb_public_2,
				t_ghs.forfait_exb_public_3,
				t_ghs.forfait_exb_public_4,
				t_ghs.forfait_exb_public_5,
				t_ghs.forfait_exb_public_6,
				t_ghs.forfait_exb_public_7,
				t_ghs.forfait_exb_public_8,
				t_ghs.forfait_exb_public_9,
				t_ghs.tarif_exb_public_1,
				t_ghs.tarif_exb_public_2,
				t_ghs.tarif_exb_public_3,
				t_ghs.tarif_exb_public_4,
				t_ghs.tarif_exb_public_5,
				t_ghs.tarif_exb_public_6,
				t_ghs.tarif_exb_public_7,
				t_ghs.tarif_exb_public_8,
				t_ghs.tarif_exb_public_9,
				t_ghs.tarif_exb,
				t_ghs.forfait_exb,
				t_ghs.dernier_tarif_exb_prive,
				t_ghs.dernier_forfait_exb_prive,
				t_ghs.dernier_tarif_exb_public,
				t_ghs.dernier_forfait_exb_public,
				t_ghs.derniere_borne_basse_fg10_prive,
				t_ghs.derniere_borne_haute_fg10_prive,
				t_ghs.dernier_tarif_ghs_fg10_prive,
				t_ghs.dernier_tarif_exh_fg10_prive,
				t_ghs.derniere_borne_basse_fg10_public,
				t_ghs.derniere_borne_haute_fg10_public,
				t_ghs.dernier_tarif_ghs_fg10_public,
				t_ghs.dernier_tarif_exh_fg10_public,
				t_ghm.code AS ghm_code,
				t_ghm_1.code AS ghm_1_code, 
				t_ghm_2.code AS ghm_2_code, 
				t_ghm_3.code AS ghm_3_code, 
				t_ghm_4.code AS ghm_4_code, 
				t_ghm_5.code AS ghm_5_code
			FROM pmsi.t_ghs, pmsi.t_ghm, 
			pmsi.t_ghm t_ghm_1, pmsi.t_ghm t_ghm_2, pmsi.t_ghm t_ghm_3, pmsi.t_ghm t_ghm_4, pmsi.t_ghm t_ghm_5
			WHERE t_ghs.ghm_id = t_ghm.oid
				AND t_ghs.ghm_1_id = t_ghm_1.oid
				AND t_ghs.ghm_2_id = t_ghm_2.oid
				AND t_ghs.ghm_3_id = t_ghm_3.oid
				AND t_ghs.ghm_4_id = t_ghm_4.oid
				AND t_ghs.ghm_5_id = t_ghm_5.oid
			ORDER BY t_ghs.code
			;
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			SELECT setval('pmsi.s_ghs', coalesce((SELECT COALESCE(MAX(oid), 1) FROM pmsi.t_ghs WHERE oid > 0), 1), true)
			;
			
			UPDATE pmsi.t_ghs
				SET code = w_dbsetup.code ,
					code_text = w_dbsetup.code_text ,
					code_fg11 = w_dbsetup.code_fg11
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_ghs.oid AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_ghs
				SET code_text = w_dbsetup.code_text , 
				texte = w_dbsetup.texte , 
			
				ghm_id = t_ghm.oid,
				ghm_1_id = t_ghm_1.oid,
				ghm_2_id = t_ghm_2.oid,
				ghm_3_id = t_ghm_3.oid,
				ghm_4_id = t_ghm_4.oid,
				ghm_5_id = t_ghm_5.oid,

				borne_basse = w_dbsetup.borne_basse, 
				borne_haute = w_dbsetup.borne_haute, 
				dms_nationale = w_dbsetup.dms_nationale, 
				tarif_ghs = w_dbsetup.tarif_ghs, 
				forfait_exb = w_dbsetup.forfait_exb, 
				tarif_exb = w_dbsetup.tarif_exb, 
				tarif_exh = w_dbsetup.tarif_exh,

				date_debut_prive_1 = w_dbsetup.date_debut_prive_1,
				date_fin_prive_1 = w_dbsetup.date_fin_prive_1,
				borne_basse_prive_1 = w_dbsetup.borne_basse_prive_1, 
				borne_haute_prive_1 = w_dbsetup.borne_haute_prive_1, 
				dms_nationale_prive_1 = w_dbsetup.dms_nationale_prive_1, 
				tarif_ghs_prive_1 = w_dbsetup.tarif_ghs_prive_1, 
				forfait_exb_prive_1 = w_dbsetup.forfait_exb_prive_1, 
				tarif_exb_prive_1 = w_dbsetup.tarif_exb_prive_1, 
				tarif_exh_prive_1 = w_dbsetup.tarif_exh_prive_1,

				date_debut_public_1 = w_dbsetup.date_debut_public_1,
				date_fin_public_1 = w_dbsetup.date_fin_public_1,
				borne_basse_public_1 = w_dbsetup.borne_basse_public_1, 
				borne_haute_public_1 = w_dbsetup.borne_haute_public_1, 
				dms_nationale_public_1 = w_dbsetup.dms_nationale_public_1, 
				tarif_ghs_public_1 = w_dbsetup.tarif_ghs_public_1, 
				forfait_exb_public_1 = w_dbsetup.forfait_exb_public_1, 
				tarif_exb_public_1 = w_dbsetup.tarif_exb_public_1, 
				tarif_exh_public_1 = w_dbsetup.tarif_exh_public_1,

				date_debut_prive_2 = w_dbsetup.date_debut_prive_2,
				date_fin_prive_2 = w_dbsetup.date_fin_prive_2,
				borne_basse_prive_2 = w_dbsetup.borne_basse_prive_2, 
				borne_haute_prive_2 = w_dbsetup.borne_haute_prive_2, 
				dms_nationale_prive_2 = w_dbsetup.dms_nationale_prive_2, 
				tarif_ghs_prive_2 = w_dbsetup.tarif_ghs_prive_2, 
				forfait_exb_prive_2 = w_dbsetup.forfait_exb_prive_2, 
				tarif_exb_prive_2 = w_dbsetup.tarif_exb_prive_2, 
				tarif_exh_prive_2 = w_dbsetup.tarif_exh_prive_2,

				date_debut_public_2 = w_dbsetup.date_debut_public_2,
				date_fin_public_2 = w_dbsetup.date_fin_public_2,
				borne_basse_public_2 = w_dbsetup.borne_basse_public_2, 
				borne_haute_public_2 = w_dbsetup.borne_haute_public_2, 
				dms_nationale_public_2 = w_dbsetup.dms_nationale_public_2, 
				tarif_ghs_public_2 = w_dbsetup.tarif_ghs_public_2, 
				forfait_exb_public_2 = w_dbsetup.forfait_exb_public_2, 
				tarif_exb_public_2 = w_dbsetup.tarif_exb_public_2, 
				tarif_exh_public_2 = w_dbsetup.tarif_exh_public_2,

				date_debut_prive_3 = w_dbsetup.date_debut_prive_3,
				date_fin_prive_3 = w_dbsetup.date_fin_prive_3,
				borne_basse_prive_3 = w_dbsetup.borne_basse_prive_3, 
				borne_haute_prive_3 = w_dbsetup.borne_haute_prive_3, 
				dms_nationale_prive_3 = w_dbsetup.dms_nationale_prive_3, 
				tarif_ghs_prive_3 = w_dbsetup.tarif_ghs_prive_3, 
				forfait_exb_prive_3 = w_dbsetup.forfait_exb_prive_3, 
				tarif_exb_prive_3 = w_dbsetup.tarif_exb_prive_3, 
				tarif_exh_prive_3 = w_dbsetup.tarif_exh_prive_3,

				date_debut_public_3 = w_dbsetup.date_debut_public_3,
				date_fin_public_3 = w_dbsetup.date_fin_public_3,
				borne_basse_public_3 = w_dbsetup.borne_basse_public_3, 
				borne_haute_public_3 = w_dbsetup.borne_haute_public_3, 
				dms_nationale_public_3 = w_dbsetup.dms_nationale_public_3, 
				tarif_ghs_public_3 = w_dbsetup.tarif_ghs_public_3, 
				forfait_exb_public_3 = w_dbsetup.forfait_exb_public_3, 
				tarif_exb_public_3 = w_dbsetup.tarif_exb_public_3, 
				tarif_exh_public_3 = w_dbsetup.tarif_exh_public_3,

				date_debut_prive_4 = w_dbsetup.date_debut_prive_4,
				date_fin_prive_4 = w_dbsetup.date_fin_prive_4,
				borne_basse_prive_4 = w_dbsetup.borne_basse_prive_4, 
				borne_haute_prive_4 = w_dbsetup.borne_haute_prive_4, 
				dms_nationale_prive_4 = w_dbsetup.dms_nationale_prive_4, 
				tarif_ghs_prive_4 = w_dbsetup.tarif_ghs_prive_4, 
				forfait_exb_prive_4 = w_dbsetup.forfait_exb_prive_4, 
				tarif_exb_prive_4 = w_dbsetup.tarif_exb_prive_4, 
				tarif_exh_prive_4 = w_dbsetup.tarif_exh_prive_4,

				date_debut_public_4 = w_dbsetup.date_debut_public_4,
				date_fin_public_4 = w_dbsetup.date_fin_public_4,
				borne_basse_public_4 = w_dbsetup.borne_basse_public_4, 
				borne_haute_public_4 = w_dbsetup.borne_haute_public_4, 
				dms_nationale_public_4 = w_dbsetup.dms_nationale_public_4, 
				tarif_ghs_public_4 = w_dbsetup.tarif_ghs_public_4, 
				forfait_exb_public_4 = w_dbsetup.forfait_exb_public_4, 
				tarif_exb_public_4 = w_dbsetup.tarif_exb_public_4, 
				tarif_exh_public_4 = w_dbsetup.tarif_exh_public_4,

				date_debut_prive_5 = w_dbsetup.date_debut_prive_5,
				date_fin_prive_5 = w_dbsetup.date_fin_prive_5,
				borne_basse_prive_5 = w_dbsetup.borne_basse_prive_5, 
				borne_haute_prive_5 = w_dbsetup.borne_haute_prive_5, 
				dms_nationale_prive_5 = w_dbsetup.dms_nationale_prive_5, 
				tarif_ghs_prive_5 = w_dbsetup.tarif_ghs_prive_5, 
				forfait_exb_prive_5 = w_dbsetup.forfait_exb_prive_5, 
				tarif_exb_prive_5 = w_dbsetup.tarif_exb_prive_5, 
				tarif_exh_prive_5 = w_dbsetup.tarif_exh_prive_5,

				date_debut_public_5 = w_dbsetup.date_debut_public_5,
				date_fin_public_5 = w_dbsetup.date_fin_public_5,
				borne_basse_public_5 = w_dbsetup.borne_basse_public_5, 
				borne_haute_public_5 = w_dbsetup.borne_haute_public_5, 
				dms_nationale_public_5 = w_dbsetup.dms_nationale_public_5, 
				tarif_ghs_public_5 = w_dbsetup.tarif_ghs_public_5, 
				forfait_exb_public_5 = w_dbsetup.forfait_exb_public_5, 
				tarif_exb_public_5 = w_dbsetup.tarif_exb_public_5, 
				tarif_exh_public_5 = w_dbsetup.tarif_exh_public_5,

				date_debut_prive_6 = w_dbsetup.date_debut_prive_6,
				date_fin_prive_6 = w_dbsetup.date_fin_prive_6,
				borne_basse_prive_6 = w_dbsetup.borne_basse_prive_6, 
				borne_haute_prive_6 = w_dbsetup.borne_haute_prive_6, 
				dms_nationale_prive_6 = w_dbsetup.dms_nationale_prive_6, 
				tarif_ghs_prive_6 = w_dbsetup.tarif_ghs_prive_6, 
				forfait_exb_prive_6 = w_dbsetup.forfait_exb_prive_6, 
				tarif_exb_prive_6 = w_dbsetup.tarif_exb_prive_6, 
				tarif_exh_prive_6 = w_dbsetup.tarif_exh_prive_6,

				date_debut_public_6 = w_dbsetup.date_debut_public_6,
				date_fin_public_6 = w_dbsetup.date_fin_public_6,
				borne_basse_public_6 = w_dbsetup.borne_basse_public_6, 
				borne_haute_public_6 = w_dbsetup.borne_haute_public_6, 
				dms_nationale_public_6 = w_dbsetup.dms_nationale_public_6, 
				tarif_ghs_public_6 = w_dbsetup.tarif_ghs_public_6, 
				forfait_exb_public_6 = w_dbsetup.forfait_exb_public_6, 
				tarif_exb_public_6 = w_dbsetup.tarif_exb_public_6, 
				tarif_exh_public_6 = w_dbsetup.tarif_exh_public_6,

				date_debut_prive_7 = w_dbsetup.date_debut_prive_7,
				date_fin_prive_7 = w_dbsetup.date_fin_prive_7,
				borne_basse_prive_7 = w_dbsetup.borne_basse_prive_7, 
				borne_haute_prive_7 = w_dbsetup.borne_haute_prive_7, 
				dms_nationale_prive_7 = w_dbsetup.dms_nationale_prive_7, 
				tarif_ghs_prive_7 = w_dbsetup.tarif_ghs_prive_7, 
				forfait_exb_prive_7 = w_dbsetup.forfait_exb_prive_7, 
				tarif_exb_prive_7 = w_dbsetup.tarif_exb_prive_7, 
				tarif_exh_prive_7 = w_dbsetup.tarif_exh_prive_7,

				date_debut_public_7 = w_dbsetup.date_debut_public_7,
				date_fin_public_7 = w_dbsetup.date_fin_public_7,
				borne_basse_public_7 = w_dbsetup.borne_basse_public_7, 
				borne_haute_public_7 = w_dbsetup.borne_haute_public_7, 
				dms_nationale_public_7 = w_dbsetup.dms_nationale_public_7, 
				tarif_ghs_public_7 = w_dbsetup.tarif_ghs_public_7, 
				forfait_exb_public_7 = w_dbsetup.forfait_exb_public_7, 
				tarif_exb_public_7 = w_dbsetup.tarif_exb_public_7, 
				tarif_exh_public_7 = w_dbsetup.tarif_exh_public_7,

				date_debut_prive_8 = w_dbsetup.date_debut_prive_8,
				date_fin_prive_8 = w_dbsetup.date_fin_prive_8,
				borne_basse_prive_8 = w_dbsetup.borne_basse_prive_8, 
				borne_haute_prive_8 = w_dbsetup.borne_haute_prive_8, 
				dms_nationale_prive_8 = w_dbsetup.dms_nationale_prive_8, 
				tarif_ghs_prive_8 = w_dbsetup.tarif_ghs_prive_8, 
				forfait_exb_prive_8 = w_dbsetup.forfait_exb_prive_8, 
				tarif_exb_prive_8 = w_dbsetup.tarif_exb_prive_8, 
				tarif_exh_prive_8 = w_dbsetup.tarif_exh_prive_8,

				date_debut_public_8 = w_dbsetup.date_debut_public_8,
				date_fin_public_8 = w_dbsetup.date_fin_public_8,
				borne_basse_public_8 = w_dbsetup.borne_basse_public_8, 
				borne_haute_public_8 = w_dbsetup.borne_haute_public_8, 
				dms_nationale_public_8 = w_dbsetup.dms_nationale_public_8, 
				tarif_ghs_public_8 = w_dbsetup.tarif_ghs_public_8, 
				forfait_exb_public_8 = w_dbsetup.forfait_exb_public_8, 
				tarif_exb_public_8 = w_dbsetup.tarif_exb_public_8, 
				tarif_exh_public_8 = w_dbsetup.tarif_exh_public_8,

				date_debut_prive_9 = w_dbsetup.date_debut_prive_9,
				date_fin_prive_9 = w_dbsetup.date_fin_prive_9,
				borne_basse_prive_9 = w_dbsetup.borne_basse_prive_9, 
				borne_haute_prive_9 = w_dbsetup.borne_haute_prive_9, 
				dms_nationale_prive_9 = w_dbsetup.dms_nationale_prive_9, 
				tarif_ghs_prive_9 = w_dbsetup.tarif_ghs_prive_9, 
				forfait_exb_prive_9 = w_dbsetup.forfait_exb_prive_9, 
				tarif_exb_prive_9 = w_dbsetup.tarif_exb_prive_9, 
				tarif_exh_prive_9 = w_dbsetup.tarif_exh_prive_9,

				date_debut_public_9 = w_dbsetup.date_debut_public_9,
				date_fin_public_9 = w_dbsetup.date_fin_public_9,
				borne_basse_public_9 = w_dbsetup.borne_basse_public_9, 
				borne_haute_public_9 = w_dbsetup.borne_haute_public_9, 
				dms_nationale_public_9 = w_dbsetup.dms_nationale_public_9, 
				tarif_ghs_public_9 = w_dbsetup.tarif_ghs_public_9, 
				forfait_exb_public_9 = w_dbsetup.forfait_exb_public_9, 
				tarif_exb_public_9 = w_dbsetup.tarif_exb_public_9, 
				tarif_exh_public_9 = w_dbsetup.tarif_exh_public_9,

				derniere_borne_basse_prive = w_dbsetup.derniere_borne_basse_prive, 
				derniere_borne_haute_prive = w_dbsetup.derniere_borne_haute_prive, 
				derniere_dms_nationale_prive = w_dbsetup.derniere_dms_nationale_prive, 
				dernier_tarif_ghs_prive = w_dbsetup.dernier_tarif_ghs_prive, 
				dernier_forfait_exb_prive = w_dbsetup.dernier_forfait_exb_prive, 
				dernier_tarif_exb_prive = w_dbsetup.dernier_tarif_exb_prive, 
				dernier_tarif_exh_prive = w_dbsetup.dernier_tarif_exh_prive,

				derniere_borne_basse_public = w_dbsetup.derniere_borne_basse_public, 
				derniere_borne_haute_public = w_dbsetup.derniere_borne_haute_public, 
				derniere_dms_nationale_public = w_dbsetup.derniere_dms_nationale_public, 
				dernier_tarif_ghs_public = w_dbsetup.dernier_tarif_ghs_public, 
				dernier_forfait_exb_public = w_dbsetup.dernier_forfait_exb_public, 
				dernier_tarif_exb_public = w_dbsetup.dernier_tarif_exb_public, 
				dernier_tarif_exh_public = w_dbsetup.dernier_tarif_exh_public,
				
				derniere_borne_basse_fg10_prive = w_dbsetup.derniere_borne_basse_fg10_prive,
				derniere_borne_haute_fg10_prive = w_dbsetup.derniere_borne_haute_fg10_prive,
				dernier_tarif_ghs_fg10_prive = w_dbsetup.dernier_tarif_ghs_fg10_prive,
				dernier_tarif_exh_fg10_prive = w_dbsetup.dernier_tarif_exh_fg10_prive,
				derniere_borne_basse_fg10_public = w_dbsetup.derniere_borne_basse_fg10_public,
				derniere_borne_haute_fg10_public = w_dbsetup.derniere_borne_haute_fg10_public,
				dernier_tarif_ghs_fg10_public = w_dbsetup.dernier_tarif_ghs_fg10_public,
				dernier_tarif_exh_fg10_public = w_dbsetup.dernier_tarif_exh_fg10_public,
				
				code_fg11 = w_dbsetup.code_fg11

			FROM 
				w_dbsetup, 
				pmsi.t_ghm, 
				pmsi.t_ghm t_ghm_1, 
				pmsi.t_ghm t_ghm_2, 
				pmsi.t_ghm t_ghm_3, 
				pmsi.t_ghm t_ghm_4, 
				pmsi.t_ghm t_ghm_5
			WHERE 
				w_dbsetup.code = t_ghs.code 
				AND w_dbsetup.ghm_code = t_ghm.code
				AND w_dbsetup.ghm_1_code = t_ghm_1.code
				AND w_dbsetup.ghm_2_code = t_ghm_2.code
				AND w_dbsetup.ghm_3_code = t_ghm_3.code
				AND w_dbsetup.ghm_4_code = t_ghm_4.code
				AND w_dbsetup.ghm_5_code = t_ghm_5.code
				;

			INSERT INTO pmsi.t_ghs
				(oid, code_text, texte, ghm_id, ghm_1_id, ghm_2_id, ghm_3_id, ghm_4_id, ghm_5_id, code, borne_basse, borne_haute, dms_nationale, 
				tarif_ghs, tarif_exh, date_debut_prive_1, date_fin_prive_1, borne_basse_prive_1, 
				borne_basse_public_1, borne_haute_prive_1, borne_haute_public_1, 
				dms_nationale_prive_1, dms_nationale_public_1, tarif_ghs_prive_1, 
				tarif_ghs_public_1, tarif_exh_prive_1, tarif_exh_public_1, date_debut_prive_2, 
				date_fin_prive_2, borne_basse_prive_2, borne_basse_public_2, 
				borne_haute_prive_2, borne_haute_public_2, dms_nationale_prive_2, 
				dms_nationale_public_2, tarif_ghs_prive_2, tarif_ghs_public_2, 
				tarif_exh_prive_2, tarif_exh_public_2, date_debut_prive_3, date_fin_prive_3, 
				borne_basse_prive_3, borne_basse_public_3, borne_haute_prive_3, 
				borne_haute_public_3, dms_nationale_prive_3, dms_nationale_public_3, 
				tarif_ghs_prive_3, tarif_ghs_public_3, tarif_exh_prive_3, tarif_exh_public_3, 
				date_debut_prive_4, date_fin_prive_4, borne_basse_prive_4, borne_basse_public_4, 
				borne_haute_prive_4, borne_haute_public_4, dms_nationale_prive_4, 
				dms_nationale_public_4, tarif_ghs_prive_4, tarif_ghs_public_4, 
				tarif_exh_prive_4, tarif_exh_public_4, date_debut_prive_5, date_fin_prive_5, 
				borne_basse_prive_5, borne_basse_public_5, borne_haute_prive_5, 
				borne_haute_public_5, dms_nationale_prive_5, dms_nationale_public_5, 
				tarif_ghs_prive_5, tarif_ghs_public_5, tarif_exh_prive_5, tarif_exh_public_5, 
				date_debut_prive_6, date_fin_prive_6, borne_basse_prive_6, borne_basse_public_6, 
				borne_haute_prive_6, borne_haute_public_6, dms_nationale_prive_6, 
				dms_nationale_public_6, tarif_ghs_prive_6, tarif_ghs_public_6, 
				tarif_exh_prive_6, tarif_exh_public_6, date_debut_prive_7, date_fin_prive_7, 
				borne_basse_prive_7, borne_basse_public_7, borne_haute_prive_7, 
				borne_haute_public_7, dms_nationale_prive_7, dms_nationale_public_7, 
				tarif_ghs_prive_7, tarif_ghs_public_7, tarif_exh_prive_7, tarif_exh_public_7, 
				date_debut_prive_8, date_fin_prive_8, borne_basse_prive_8, borne_basse_public_8, 
				borne_haute_prive_8, borne_haute_public_8, dms_nationale_prive_8, 
				dms_nationale_public_8, tarif_ghs_prive_8, tarif_ghs_public_8, 
				tarif_exh_prive_8, tarif_exh_public_8, date_debut_prive_9, date_fin_prive_9, 
				borne_basse_prive_9, borne_basse_public_9, borne_haute_prive_9, 
				borne_haute_public_9, dms_nationale_prive_9, dms_nationale_public_9, 
				tarif_ghs_prive_9, tarif_ghs_public_9, tarif_exh_prive_9, tarif_exh_public_9, 
				date_debut_public_1, date_fin_public_1, date_debut_public_2, 
				date_fin_public_2, date_debut_public_3, date_fin_public_3, date_debut_public_4, 
				date_fin_public_4, date_debut_public_5, date_fin_public_5, date_debut_public_6, 
				date_fin_public_6, date_debut_public_7, date_fin_public_7, date_debut_public_8, 
				date_fin_public_8, date_debut_public_9, date_fin_public_9,
				derniere_borne_basse_prive, derniere_borne_haute_prive, derniere_dms_nationale_prive, 
				dernier_tarif_ghs_prive, dernier_tarif_exh_prive,
				derniere_borne_basse_public, derniere_borne_haute_public, derniere_dms_nationale_public, 
				dernier_tarif_ghs_public, dernier_tarif_exh_public,
				derniere_borne_basse_fg10_prive,
				derniere_borne_haute_fg10_prive,
				dernier_tarif_ghs_fg10_prive,
				dernier_tarif_exh_fg10_prive,
				derniere_borne_basse_fg10_public,
				derniere_borne_haute_fg10_public,
				dernier_tarif_ghs_fg10_public,
				dernier_tarif_exh_fg10_public,
				code_fg11
			)
			SELECT 
				w_dbsetup.oid, w_dbsetup.code_text, w_dbsetup.texte, 0, 0, 0, 0, 0, 0, w_dbsetup.code, borne_basse, borne_haute, dms_nationale, 
				tarif_ghs, tarif_exh, date_debut_prive_1, date_fin_prive_1, borne_basse_prive_1, 
				borne_basse_public_1, borne_haute_prive_1, borne_haute_public_1, 
				dms_nationale_prive_1, dms_nationale_public_1, tarif_ghs_prive_1, 
				tarif_ghs_public_1, tarif_exh_prive_1, tarif_exh_public_1, date_debut_prive_2, 
				date_fin_prive_2, borne_basse_prive_2, borne_basse_public_2, 
				borne_haute_prive_2, borne_haute_public_2, dms_nationale_prive_2, 
				dms_nationale_public_2, tarif_ghs_prive_2, tarif_ghs_public_2, 
				tarif_exh_prive_2, tarif_exh_public_2, date_debut_prive_3, date_fin_prive_3, 
				borne_basse_prive_3, borne_basse_public_3, borne_haute_prive_3, 
				borne_haute_public_3, dms_nationale_prive_3, dms_nationale_public_3, 
				tarif_ghs_prive_3, tarif_ghs_public_3, tarif_exh_prive_3, tarif_exh_public_3, 
				date_debut_prive_4, date_fin_prive_4, borne_basse_prive_4, borne_basse_public_4, 
				borne_haute_prive_4, borne_haute_public_4, dms_nationale_prive_4, 
				dms_nationale_public_4, tarif_ghs_prive_4, tarif_ghs_public_4, 
				tarif_exh_prive_4, tarif_exh_public_4, date_debut_prive_5, date_fin_prive_5, 
				borne_basse_prive_5, borne_basse_public_5, borne_haute_prive_5, 
				borne_haute_public_5, dms_nationale_prive_5, dms_nationale_public_5, 
				tarif_ghs_prive_5, tarif_ghs_public_5, tarif_exh_prive_5, tarif_exh_public_5, 
				date_debut_prive_6, date_fin_prive_6, borne_basse_prive_6, borne_basse_public_6, 
				borne_haute_prive_6, borne_haute_public_6, dms_nationale_prive_6, 
				dms_nationale_public_6, tarif_ghs_prive_6, tarif_ghs_public_6, 
				tarif_exh_prive_6, tarif_exh_public_6, date_debut_prive_7, date_fin_prive_7, 
				borne_basse_prive_7, borne_basse_public_7, borne_haute_prive_7, 
				borne_haute_public_7, dms_nationale_prive_7, dms_nationale_public_7, 
				tarif_ghs_prive_7, tarif_ghs_public_7, tarif_exh_prive_7, tarif_exh_public_7, 
				date_debut_prive_8, date_fin_prive_8, borne_basse_prive_8, borne_basse_public_8, 
				borne_haute_prive_8, borne_haute_public_8, dms_nationale_prive_8, 
				dms_nationale_public_8, tarif_ghs_prive_8, tarif_ghs_public_8, 
				tarif_exh_prive_8, tarif_exh_public_8, date_debut_prive_9, date_fin_prive_9, 
				borne_basse_prive_9, borne_basse_public_9, borne_haute_prive_9, 
				borne_haute_public_9, dms_nationale_prive_9, dms_nationale_public_9, 
				tarif_ghs_prive_9, tarif_ghs_public_9, tarif_exh_prive_9, tarif_exh_public_9, 
				date_debut_public_1, date_fin_public_1, date_debut_public_2, 
				date_fin_public_2, date_debut_public_3, date_fin_public_3, date_debut_public_4, 
				date_fin_public_4, date_debut_public_5, date_fin_public_5, date_debut_public_6, 
				date_fin_public_6, date_debut_public_7, date_fin_public_7, date_debut_public_8, 
				date_fin_public_8, date_debut_public_9, date_fin_public_9,
				derniere_borne_basse_prive, derniere_borne_haute_prive, derniere_dms_nationale_prive, 
				dernier_tarif_ghs_prive, dernier_tarif_exh_prive,
				derniere_borne_basse_public, derniere_borne_haute_public, derniere_dms_nationale_public, 
				dernier_tarif_ghs_public, dernier_tarif_exh_public,
				derniere_borne_basse_fg10_prive,
				derniere_borne_haute_fg10_prive,
				dernier_tarif_ghs_fg10_prive,
				dernier_tarif_exh_fg10_prive,
				derniere_borne_basse_fg10_public,
				derniere_borne_haute_fg10_public,
				dernier_tarif_ghs_fg10_public,
				dernier_tarif_exh_fg10_public,
				'0'
			FROM w_dbsetup
			WHERE w_dbsetup.oid = 0 AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_ghs);


			INSERT INTO pmsi.t_ghs(
				code_text, texte, ghm_id, ghm_1_id, ghm_2_id, ghm_3_id, ghm_4_id, ghm_5_id, code, borne_basse, borne_haute, dms_nationale, 
				tarif_ghs, tarif_exh, date_debut_prive_1, date_fin_prive_1, borne_basse_prive_1, 
				borne_basse_public_1, borne_haute_prive_1, borne_haute_public_1, 
				dms_nationale_prive_1, dms_nationale_public_1, tarif_ghs_prive_1, 
				tarif_ghs_public_1, tarif_exh_prive_1, tarif_exh_public_1, date_debut_prive_2, 
				date_fin_prive_2, borne_basse_prive_2, borne_basse_public_2, 
				borne_haute_prive_2, borne_haute_public_2, dms_nationale_prive_2, 
				dms_nationale_public_2, tarif_ghs_prive_2, tarif_ghs_public_2, 
				tarif_exh_prive_2, tarif_exh_public_2, date_debut_prive_3, date_fin_prive_3, 
				borne_basse_prive_3, borne_basse_public_3, borne_haute_prive_3, 
				borne_haute_public_3, dms_nationale_prive_3, dms_nationale_public_3, 
				tarif_ghs_prive_3, tarif_ghs_public_3, tarif_exh_prive_3, tarif_exh_public_3, 
				date_debut_prive_4, date_fin_prive_4, borne_basse_prive_4, borne_basse_public_4, 
				borne_haute_prive_4, borne_haute_public_4, dms_nationale_prive_4, 
				dms_nationale_public_4, tarif_ghs_prive_4, tarif_ghs_public_4, 
				tarif_exh_prive_4, tarif_exh_public_4, date_debut_prive_5, date_fin_prive_5, 
				borne_basse_prive_5, borne_basse_public_5, borne_haute_prive_5, 
				borne_haute_public_5, dms_nationale_prive_5, dms_nationale_public_5, 
				tarif_ghs_prive_5, tarif_ghs_public_5, tarif_exh_prive_5, tarif_exh_public_5, 
				date_debut_prive_6, date_fin_prive_6, borne_basse_prive_6, borne_basse_public_6, 
				borne_haute_prive_6, borne_haute_public_6, dms_nationale_prive_6, 
				dms_nationale_public_6, tarif_ghs_prive_6, tarif_ghs_public_6, 
				tarif_exh_prive_6, tarif_exh_public_6, date_debut_prive_7, date_fin_prive_7, 
				borne_basse_prive_7, borne_basse_public_7, borne_haute_prive_7, 
				borne_haute_public_7, dms_nationale_prive_7, dms_nationale_public_7, 
				tarif_ghs_prive_7, tarif_ghs_public_7, tarif_exh_prive_7, tarif_exh_public_7, 
				date_debut_prive_8, date_fin_prive_8, borne_basse_prive_8, borne_basse_public_8, 
				borne_haute_prive_8, borne_haute_public_8, dms_nationale_prive_8, 
				dms_nationale_public_8, tarif_ghs_prive_8, tarif_ghs_public_8, 
				tarif_exh_prive_8, tarif_exh_public_8, date_debut_prive_9, date_fin_prive_9, 
				borne_basse_prive_9, borne_basse_public_9, borne_haute_prive_9, 
				borne_haute_public_9, dms_nationale_prive_9, dms_nationale_public_9, 
				tarif_ghs_prive_9, tarif_ghs_public_9, tarif_exh_prive_9, tarif_exh_public_9, 
				date_debut_public_1, date_fin_public_1, date_debut_public_2, 
				date_fin_public_2, date_debut_public_3, date_fin_public_3, date_debut_public_4, 
				date_fin_public_4, date_debut_public_5, date_fin_public_5, date_debut_public_6, 
				date_fin_public_6, date_debut_public_7, date_fin_public_7, date_debut_public_8, 
				date_fin_public_8, date_debut_public_9, date_fin_public_9, derniere_borne_basse_prive, 
				derniere_borne_haute_prive, derniere_dms_nationale_prive, dernier_tarif_ghs_prive, 
				dernier_tarif_exh_prive, derniere_borne_basse_public, derniere_borne_haute_public, 
				derniere_dms_nationale_public, dernier_tarif_ghs_public, dernier_tarif_exh_public, 
				code_fg11, 
				forfait_exb_prive_1, forfait_exb_prive_2, forfait_exb_prive_3, 
				forfait_exb_prive_4, forfait_exb_prive_5, forfait_exb_prive_6, 
				forfait_exb_prive_7, forfait_exb_prive_8, forfait_exb_prive_9, 
				tarif_exb_prive_1, tarif_exb_prive_2, tarif_exb_prive_3, tarif_exb_prive_4, 
				tarif_exb_prive_5, tarif_exb_prive_6, tarif_exb_prive_7, tarif_exb_prive_8, 
				tarif_exb_prive_9, forfait_exb_public_1, forfait_exb_public_2, 
				forfait_exb_public_3, forfait_exb_public_4, forfait_exb_public_5, 
				forfait_exb_public_6, forfait_exb_public_7, forfait_exb_public_8, 
				forfait_exb_public_9, tarif_exb_public_1, tarif_exb_public_2, 
				tarif_exb_public_3, tarif_exb_public_4, tarif_exb_public_5, tarif_exb_public_6, 
				tarif_exb_public_7, tarif_exb_public_8, tarif_exb_public_9, tarif_exb, 
				forfait_exb, dernier_tarif_exb_prive, dernier_forfait_exb_prive, 
				dernier_tarif_exb_public, dernier_forfait_exb_public,
				derniere_borne_basse_fg10_prive,
				derniere_borne_haute_fg10_prive,
				dernier_tarif_ghs_fg10_prive,
				dernier_tarif_exh_fg10_prive,
				derniere_borne_basse_fg10_public,
				derniere_borne_haute_fg10_public,
				dernier_tarif_ghs_fg10_public,
				dernier_tarif_exh_fg10_public
				)
				SELECT 
					w_dbsetup.code_text, w_dbsetup.texte, t_ghm.oid, t_ghm_1.oid, t_ghm_2.oid, t_ghm_3.oid, t_ghm_4.oid, t_ghm_5.oid, w_dbsetup.code, borne_basse, borne_haute, dms_nationale, 
					tarif_ghs, tarif_exh, date_debut_prive_1, date_fin_prive_1, borne_basse_prive_1, 
					borne_basse_public_1, borne_haute_prive_1, borne_haute_public_1, 
					dms_nationale_prive_1, dms_nationale_public_1, tarif_ghs_prive_1, 
					tarif_ghs_public_1, tarif_exh_prive_1, tarif_exh_public_1, date_debut_prive_2, 
					date_fin_prive_2, borne_basse_prive_2, borne_basse_public_2, 
					borne_haute_prive_2, borne_haute_public_2, dms_nationale_prive_2, 
					dms_nationale_public_2, tarif_ghs_prive_2, tarif_ghs_public_2, 
					tarif_exh_prive_2, tarif_exh_public_2, date_debut_prive_3, date_fin_prive_3, 
					borne_basse_prive_3, borne_basse_public_3, borne_haute_prive_3, 
					borne_haute_public_3, dms_nationale_prive_3, dms_nationale_public_3, 
					tarif_ghs_prive_3, tarif_ghs_public_3, tarif_exh_prive_3, tarif_exh_public_3, 
					date_debut_prive_4, date_fin_prive_4, borne_basse_prive_4, borne_basse_public_4, 
					borne_haute_prive_4, borne_haute_public_4, dms_nationale_prive_4, 
					dms_nationale_public_4, tarif_ghs_prive_4, tarif_ghs_public_4, 
					tarif_exh_prive_4, tarif_exh_public_4, date_debut_prive_5, date_fin_prive_5, 
					borne_basse_prive_5, borne_basse_public_5, borne_haute_prive_5, 
					borne_haute_public_5, dms_nationale_prive_5, dms_nationale_public_5, 
					tarif_ghs_prive_5, tarif_ghs_public_5, tarif_exh_prive_5, tarif_exh_public_5, 
					date_debut_prive_6, date_fin_prive_6, borne_basse_prive_6, borne_basse_public_6, 
					borne_haute_prive_6, borne_haute_public_6, dms_nationale_prive_6, 
					dms_nationale_public_6, tarif_ghs_prive_6, tarif_ghs_public_6, 
					tarif_exh_prive_6, tarif_exh_public_6, date_debut_prive_7, date_fin_prive_7, 
					borne_basse_prive_7, borne_basse_public_7, borne_haute_prive_7, 
					borne_haute_public_7, dms_nationale_prive_7, dms_nationale_public_7, 
					tarif_ghs_prive_7, tarif_ghs_public_7, tarif_exh_prive_7, tarif_exh_public_7, 
					date_debut_prive_8, date_fin_prive_8, borne_basse_prive_8, borne_basse_public_8, 
					borne_haute_prive_8, borne_haute_public_8, dms_nationale_prive_8, 
					dms_nationale_public_8, tarif_ghs_prive_8, tarif_ghs_public_8, 
					tarif_exh_prive_8, tarif_exh_public_8, date_debut_prive_9, date_fin_prive_9, 
					borne_basse_prive_9, borne_basse_public_9, borne_haute_prive_9, 
					borne_haute_public_9, dms_nationale_prive_9, dms_nationale_public_9, 
					tarif_ghs_prive_9, tarif_ghs_public_9, tarif_exh_prive_9, tarif_exh_public_9, 
					date_debut_public_1, date_fin_public_1, date_debut_public_2, 
					date_fin_public_2, date_debut_public_3, date_fin_public_3, date_debut_public_4, 
					date_fin_public_4, date_debut_public_5, date_fin_public_5, date_debut_public_6, 
					date_fin_public_6, date_debut_public_7, date_fin_public_7, date_debut_public_8, 
					date_fin_public_8, date_debut_public_9, date_fin_public_9, derniere_borne_basse_prive, 
					derniere_borne_haute_prive, derniere_dms_nationale_prive, dernier_tarif_ghs_prive, 
					dernier_tarif_exh_prive, derniere_borne_basse_public, derniere_borne_haute_public, 
					derniere_dms_nationale_public, dernier_tarif_ghs_public, dernier_tarif_exh_public, 
					w_dbsetup.code_fg11,
					forfait_exb_prive_1, forfait_exb_prive_2, forfait_exb_prive_3, 
					forfait_exb_prive_4, forfait_exb_prive_5, forfait_exb_prive_6, 
					forfait_exb_prive_7, forfait_exb_prive_8, forfait_exb_prive_9, 
					tarif_exb_prive_1, tarif_exb_prive_2, tarif_exb_prive_3, tarif_exb_prive_4, 
					tarif_exb_prive_5, tarif_exb_prive_6, tarif_exb_prive_7, tarif_exb_prive_8, 
					tarif_exb_prive_9, forfait_exb_public_1, forfait_exb_public_2, 
					forfait_exb_public_3, forfait_exb_public_4, forfait_exb_public_5, 
					forfait_exb_public_6, forfait_exb_public_7, forfait_exb_public_8, 
					forfait_exb_public_9, tarif_exb_public_1, tarif_exb_public_2, 
					tarif_exb_public_3, tarif_exb_public_4, tarif_exb_public_5, tarif_exb_public_6, 
					tarif_exb_public_7, tarif_exb_public_8, tarif_exb_public_9, tarif_exb, 
					forfait_exb, dernier_tarif_exb_prive, dernier_forfait_exb_prive, 
					dernier_tarif_exb_public, dernier_forfait_exb_public,
					derniere_borne_basse_fg10_prive,
					derniere_borne_haute_fg10_prive,
					dernier_tarif_ghs_fg10_prive,
					dernier_tarif_exh_fg10_prive,
					derniere_borne_basse_fg10_public,
					derniere_borne_haute_fg10_public,
					dernier_tarif_ghs_fg10_public,
					dernier_tarif_exh_fg10_public
				FROM w_dbsetup, pmsi.t_ghm, pmsi.t_ghm t_ghm_1, pmsi.t_ghm t_ghm_2, pmsi.t_ghm t_ghm_3, pmsi.t_ghm t_ghm_4, pmsi.t_ghm t_ghm_5
				WHERE w_dbsetup.oid <> 0 AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_ghs)
					AND w_dbsetup.ghm_code = t_ghm.code
					AND w_dbsetup.ghm_1_code = t_ghm_1.code
					AND w_dbsetup.ghm_2_code = t_ghm_2.code
					AND w_dbsetup.ghm_3_code = t_ghm_3.code
					AND w_dbsetup.ghm_4_code = t_ghm_4.code
					AND w_dbsetup.ghm_5_code = t_ghm_5.code;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_1" label="Couples GHM (Analyse ambulatoire)">
		<selectCmd><![CDATA[ 
			
			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;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			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;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_2" label="Seuils cancérologie (cdc 26 mars 2008)">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_etude_2.oid = 0 THEN t_etude_2.oid ELSE 1 END AS oid,
				t_etude_2.code, 
				t_etude_2.texte, 
				t_etude_2.texte_court
			FROM pmsi.t_etude_2
			ORDER BY t_etude_2.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_etude_2 SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_etude_2.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_etude_2(code, texte, texte_court)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_etude_2);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_3" label="Indicateurs OQOS (arrêté 8 juin 2005)">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_etude_3.oid = 0 THEN t_etude_3.oid ELSE 1 END AS oid,
				t_etude_3.code, 
				t_etude_3.texte, 
				t_etude_3.texte_court
			FROM pmsi.t_etude_3
			ORDER BY t_etude_3.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_etude_3 SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_etude_3.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_etude_3(code, texte, texte_court)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_etude_3);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_4" label="Analyse des Gestes Marqueurs">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court
			FROM pmsi.t_etude_4
			ORDER BY t_etude_4.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_etude_4 SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_etude_4.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_etude_4(code, texte, texte_court)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE w_dbsetup.oid <> 0 AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_etude_4);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_5" label="Analyse des Gestes Marqueurs">
		<selectCmd><![CDATA[
			
			SELECT
				CASE WHEN t_etude_5.oid = 0 THEN t_etude_5.oid ELSE 1 END AS oid, 
				t_etude_5.code, 
				t_etude_5.sequence, 
				t_territoires_sante.code as tersante_code, 
				t_etude_5.taux_moyen_tersante, 
				t_etude_5.taux_moyen_tersante_prive, 
				t_etude_5.taux_moyen_tersante_public, 
				t_regions.code as region_code, 
				t_etude_5.taux_moyen_regional, 
				t_etude_5.taux_moyen_regional_prive, 
				t_etude_5.taux_moyen_regional_public, 
				t_etude_5.taux_moyen_national, 
				t_etude_5.taux_moyen_national_prive, 
				t_etude_5.taux_moyen_national_public, 
				t_etude_5.taux_cible_ets, 
				t_etude_5.taux_cible_ars 
			FROM 
				pmsi.t_etude_5 
				JOIN base.t_territoires_sante ON t_territoires_sante.oid = t_etude_5.tersante_id 
				JOIN base.t_regions ON t_regions.oid = t_etude_5.region_id 
			ORDER BY t_etude_5.sequence, t_etude_5.code, t_territoires_sante.code, t_regions.code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[	
			
			-- recuperation des seuils par activite de la table t_etude_6 avant mise a jour
			DROP TABLE IF EXISTS w_old; 
			CREATE TEMP TABLE w_old AS select code, sequence, MAX(taux_cible_ets) AS taux_cible_ets, MAX(taux_cible_ars) AS taux_cible_ars from pmsi.t_etude_5 group by 1,2 order by 1; 
			
			-- effacement de la table
			TRUNCATE pmsi.t_etude_5;
			
			INSERT INTO pmsi.t_etude_5(
				code,
				sequence,
				tersante_id,
				taux_moyen_tersante,
				taux_moyen_tersante_prive,
				taux_moyen_tersante_public,
				region_id,
				taux_moyen_regional,
				taux_moyen_regional_prive,
				taux_moyen_regional_public,
				taux_moyen_national,
				taux_moyen_national_prive,
				taux_moyen_national_public,
				taux_cible_ets,
				taux_cible_ars)
			SELECT 
				w_dbsetup.code,
				w_dbsetup.sequence,
				t_territoires_sante.oid,
				w_dbsetup.taux_moyen_tersante,
				w_dbsetup.taux_moyen_tersante_prive,
				w_dbsetup.taux_moyen_tersante_public,
				t_regions.oid,
				w_dbsetup.taux_moyen_regional,
				w_dbsetup.taux_moyen_regional_prive,
				w_dbsetup.taux_moyen_regional_public,
				w_dbsetup.taux_moyen_national,
				w_dbsetup.taux_moyen_national_prive,
				w_dbsetup.taux_moyen_national_public,
				COALESCE(w_old.taux_cible_ets, w_dbsetup.taux_cible_ets),
				COALESCE(w_old.taux_cible_ars, w_dbsetup.taux_cible_ars)
			FROM 
				w_dbsetup
				JOIN base.t_territoires_sante ON t_territoires_sante.code = w_dbsetup.tersante_code 
				JOIN base.t_regions ON t_regions.code = w_dbsetup.region_code 
				LEFT JOIN w_old ON w_old.code = w_dbsetup.code AND w_old.sequence = w_dbsetup.sequence
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.code || w_dbsetup.sequence || w_dbsetup.tersante_code NOT IN
					(	SELECT 
							t_etude_5.code || t_etude_5.sequence || t_territoires_sante.code 
						FROM 
							pmsi.t_etude_5 
							JOIN base.t_territoires_sante ON t_territoires_sante.oid = pmsi.t_etude_5.tersante_id 
						GROUP BY 1
					);			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_6" label="Seuils cancérologie">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_etude_6.oid = 0 THEN t_etude_6.oid ELSE 1 END AS oid,
				t_etude_6.code, 
				t_etude_6.texte, 
				t_etude_6.texte_court, 
				t_etude_6.annee, 
				t_regions.code as region_code, 
				t_territoires_sante.code as tersante_code, 
				t_etude_6.seuil_ets, 
				t_etude_6.seuil_ars, 
				t_etude_6.moy_tersante, 
				t_etude_6.moy_tersante_prive, 
				t_etude_6.moy_tersante_public, 
				t_etude_6.moy_region, 
				t_etude_6.moy_region_prive, 
				t_etude_6.moy_region_public, 
				t_etude_6.moy_nat, 
				t_etude_6.moy_nat_prive, 
				t_etude_6.moy_nat_public, 
				CASE WHEN t_etude_6.activite_code = 'CHIR' THEN 1 WHEN t_etude_6.activite_code = 'CHIM' THEN 2 ELSE 3 END AS activite_oid, 
				t_etude_6.activite_code, 
				t_etude_6.activite_texte,
				t_etude_6.seuils_annuel
					
			FROM 
				pmsi.t_etude_6 
				JOIN base.t_territoires_sante ON t_territoires_sante.oid = t_etude_6.tersante_id 
				JOIN base.t_regions ON t_regions.oid = t_etude_6.region_id 
			ORDER BY t_etude_6.annee, t_etude_6.code, t_regions.code, t_territoires_sante.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			-- effacement de la table
			TRUNCATE pmsi.t_etude_6;
			
			-- peuplement de la table
			INSERT INTO pmsi.t_etude_6(
				code, texte, texte_court,
				annee,
				seuil_ets,
				seuil_ars,
				moy_tersante,
				moy_tersante_prive,
				moy_tersante_public,
				moy_region,
				moy_region_prive,
				moy_region_public,
				moy_nat,
				moy_nat_prive,
				moy_nat_public, 
				activite_id, 
				activite_code, 
				activite_texte, 
				tersante_id, 
				region_id ,
				seuils_annuel
				)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.texte_court,
				w_dbsetup.annee,
				w_dbsetup.seuil_ets,
				w_dbsetup.seuil_ars,
				w_dbsetup.moy_tersante,
				w_dbsetup.moy_tersante_prive,
				w_dbsetup.moy_tersante_public,
				w_dbsetup.moy_region,
				w_dbsetup.moy_region_prive,
				w_dbsetup.moy_region_public,
				w_dbsetup.moy_nat,
				w_dbsetup.moy_nat_prive,
				w_dbsetup.moy_nat_public, 
				w_dbsetup.activite_oid, 
				w_dbsetup.activite_code, 
				w_dbsetup.activite_texte, 
				t_territoires_sante.oid, 
				t_regions.oid,
				w_dbsetup.seuils_annuel 				
			FROM 
				w_dbsetup 
				JOIN base.t_territoires_sante ON t_territoires_sante.code = w_dbsetup.tersante_code 
				JOIN base.t_regions ON t_regions.code = w_dbsetup.region_code
			WHERE 
				w_dbsetup.code <> ''
			;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_7" label="Seuils dégressivité tarifaire">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_etude_7.oid = 0 THEN t_etude_7.oid ELSE 1 END AS oid
				,t_etude_7.annee
				,t_etude_7.code
				,t_etude_7.texte
				,t_etude_7.seuil_degressivite
			FROM 
				pmsi.t_etude_7 
			ORDER BY t_etude_7.annee, t_etude_7.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_etude_7 SET 
				texte = w_dbsetup.texte
				,seuil_degressivite = w_dbsetup.seuil_degressivite
			FROM w_dbsetup
			WHERE 1=1
				AND t_etude_7.code = w_dbsetup.code
				AND t_etude_7.annee = w_dbsetup.annee
				AND (t_etude_7.texte = w_dbsetup.texte OR t_etude_7.seuil_degressivite = w_dbsetup.seuil_degressivite);
			
			
			INSERT INTO pmsi.t_etude_7(code, texte, annee, seuil_degressivite)
			SELECT 
				w_dbsetup.code
				,w_dbsetup.texte
				,w_dbsetup.annee
				,w_dbsetup.seuil_degressivite
			FROM w_dbsetup
				LEFT JOIN pmsi.t_etude_7 w_source ON w_source.code = w_dbsetup.code AND w_source.annee = w_dbsetup.annee
			WHERE w_source.code IS NULL OR w_source.annee IS NULL;
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_etude_cardio_modalites" label="Liste des modalités de l'étude cardio interventionnelle">
		<selectCmd><![CDATA[

			SELECT
				code,
				texte,
				texte_court
			FROM pmsi.t_etude_cardio_modalites
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_etude_cardio_modalites SET
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 1=1
				AND t_etude_cardio_modalites.code = w_dbsetup.code
				AND (
					t_etude_cardio_modalites.texte IS DISTINCT FROM w_dbsetup.texte
					OR t_etude_cardio_modalites.texte_court IS DISTINCT FROM w_dbsetup.texte_court
				)
				;

			INSERT INTO pmsi.t_etude_cardio_modalites(code, texte, texte_court)
				SELECT
					w_dbsetup.code,
					w_dbsetup.texte,
					w_dbsetup.texte_court
				FROM w_dbsetup
				LEFT JOIN pmsi.t_etude_cardio_modalites USING (code)
				WHERE t_etude_cardio_modalites.code IS NULL
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_cardio_mentions" label="Etude Cardio">
		<selectCmd><![CDATA[

			SELECT
				t_etude_cardio_mentions.code,
				t_etude_cardio_mentions.texte,
				t_etude_cardio_mentions.texte_court,
				t_etude_cardio_mentions.seuil_activite_1,
				t_etude_cardio_mentions.seuil_activite_2,
				t_etude_cardio_modalites.code AS cardio_modalite_code,
				t_etude_cardio_mentions.type AS type,
				t_etude_cardio_mentions.cdc
			FROM pmsi.t_etude_cardio_mentions
			JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.oid = cardio_modalite_id
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_etude_cardio_mentions SET
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte_court,
				seuil_activite_1 = w_dbsetup.seuil_activite_1,
				seuil_activite_2 = w_dbsetup.seuil_activite_2,
				cardio_modalite_id = t_etude_cardio_modalites.oid,
				type = w_dbsetup.type
			FROM w_dbsetup
			JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.code = cardio_modalite_code
			WHERE 1=1
				AND t_etude_cardio_mentions.code = w_dbsetup.code
				AND t_etude_cardio_mentions.cdc = w_dbsetup.cdc
				AND (
					t_etude_cardio_mentions.texte IS DISTINCT FROM w_dbsetup.texte
					OR t_etude_cardio_mentions.texte_court IS DISTINCT FROM w_dbsetup.texte_court
					OR t_etude_cardio_mentions.seuil_activite_1 IS DISTINCT FROM w_dbsetup.seuil_activite_1
					OR t_etude_cardio_mentions.seuil_activite_2 IS DISTINCT FROM w_dbsetup.seuil_activite_2
					OR t_etude_cardio_mentions.cardio_modalite_id IS DISTINCT FROM t_etude_cardio_modalites.oid
					OR t_etude_cardio_mentions.type IS DISTINCT FROM w_dbsetup.type
				)
			;

			INSERT INTO pmsi.t_etude_cardio_mentions(
				code,
				texte,
				texte_court,
				seuil_activite_1,
				seuil_activite_2,
				cardio_modalite_id,
				type,
				cdc
			)
				SELECT
					w_dbsetup.code,
					w_dbsetup.texte,
					w_dbsetup.texte_court,
					w_dbsetup.seuil_activite_1,
					w_dbsetup.seuil_activite_2,
					t_etude_cardio_modalites.oid,
					w_dbsetup.type,
					w_dbsetup.cdc
				FROM w_dbsetup
				JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.code = w_dbsetup.cardio_modalite_code
					LEFT JOIN pmsi.t_etude_cardio_mentions ON t_etude_cardio_mentions.code = w_dbsetup.code
						AND t_etude_cardio_mentions.cdc = w_dbsetup.cdc
					WHERE t_etude_cardio_mentions.code IS NULL OR t_etude_cardio_mentions.cdc IS NULL
			;
		]]></updateCmd>
	</TABLE>_actes

	<TABLE name="t_etude_cardio_familles_actes" label="Liste des familles des seuils Cardio">
		<selectCmd><![CDATA[

			SELECT
				t_etude_cardio_familles_actes.code,
				t_etude_cardio_familles_actes.texte,
				t_etude_cardio_familles_actes.texte_court,
				t_etude_cardio_familles_actes.mention,
				t_etude_cardio_familles_actes.seuil_annuel,
				t_etude_cardio_familles_actes.est_seuil,
				t_listes.code AS liste_code,
				t_etude_cardio_familles_actes.cdc,
				t_etude_cardio_modalites.code AS cardio_modalite_code,
				t_etude_cardio_mentions.code AS cardio_mention_code
			FROM pmsi.t_etude_cardio_familles_actes
        	JOIN pmsi.t_listes ON t_listes.oid = liste_id
			JOIN pmsi.t_etude_cardio_mentions ON t_etude_cardio_mentions.oid = t_etude_cardio_familles_actes.cardio_mention_id
			JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.oid = t_etude_cardio_familles_actes.cardio_modalite_id
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			UPDATE pmsi.t_etude_cardio_familles_actes SET
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte_court,
				mention = w_dbsetup.mention,
				seuil_annuel = w_dbsetup.seuil_annuel,
				est_seuil = w_dbsetup.est_seuil,
				liste_id = t_listes.oid,
				cardio_modalite_id = t_etude_cardio_modalites.oid,
				cardio_mention_id = t_etude_cardio_mentions.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON t_listes.code = liste_code
			JOIN pmsi.t_etude_cardio_mentions ON t_etude_cardio_mentions.code = cardio_mention_code
			JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.code = cardio_modalite_code
			WHERE 1=1
				AND t_etude_cardio_familles_actes.code = w_dbsetup.code
				AND t_etude_cardio_familles_actes.cdc = w_dbsetup.cdc
				AND (
					t_etude_cardio_familles_actes.texte IS DISTINCT FROM w_dbsetup.texte
					OR t_etude_cardio_familles_actes.texte_court IS DISTINCT FROM w_dbsetup.texte_court
					OR t_etude_cardio_familles_actes.mention IS DISTINCT FROM w_dbsetup.mention
					OR t_etude_cardio_familles_actes.seuil_annuel IS DISTINCT FROM w_dbsetup.seuil_annuel
					OR t_etude_cardio_familles_actes.est_seuil IS DISTINCT FROM w_dbsetup.est_seuil
					OR t_etude_cardio_familles_actes.liste_id IS DISTINCT FROM t_listes.oid
					OR t_etude_cardio_familles_actes.cardio_modalite_id IS DISTINCT FROM t_etude_cardio_modalites.oid
					OR t_etude_cardio_familles_actes.cardio_mention_id IS DISTINCT FROM t_etude_cardio_mentions.oid
				)
			;


			INSERT INTO pmsi.t_etude_cardio_familles_actes(
				code,
				texte,
				texte_court,
				mention,
				seuil_annuel,
				est_seuil,
				liste_id,
				cdc,
				cardio_modalite_id,
				cardio_mention_id
				)
				SELECT
					w_dbsetup.code,
					w_dbsetup.texte,
					w_dbsetup.texte_court,
					w_dbsetup.mention,
					w_dbsetup.seuil_annuel,
					w_dbsetup.est_seuil,
					t_listes.oid,
					w_dbsetup.cdc,
					t_etude_cardio_modalites.oid,
					t_etude_cardio_mentions.oid
				FROM w_dbsetup
				JOIN pmsi.t_listes ON t_listes.code = w_dbsetup.liste_code
				JOIN pmsi.t_etude_cardio_mentions ON t_etude_cardio_mentions.code = w_dbsetup.cardio_mention_code
				JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.code = w_dbsetup.cardio_modalite_code
				LEFT JOIN pmsi.t_etude_cardio_familles_actes
					ON t_etude_cardio_familles_actes.code = w_dbsetup.code
					AND t_etude_cardio_familles_actes.cdc = w_dbsetup.cdc
				WHERE t_etude_cardio_familles_actes.code IS NULL OR t_etude_cardio_familles_actes.cdc IS NULL
			;
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_specialites_medecin" label="Spécialités médecin">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				texte,
				"type"
			FROM pmsi.t_specialites_medecin 
			WHERE 
				(length(code) = 2 OR oid = 0)
				AND code != texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_specialites_medecin SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE
				t_specialites_medecin.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_specialites_medecin SET 
				texte = w_dbsetup.texte, 
				type = w_dbsetup.type
			FROM w_dbsetup
			WHERE 
				t_specialites_medecin.code = w_dbsetup.code
				AND (
					t_specialites_medecin.texte <> w_dbsetup.texte OR
					t_specialites_medecin.type <> w_dbsetup.type OR
					t_specialites_medecin.texte IS NULL OR
					t_specialites_medecin.type IS NULL
				);
			
			
			INSERT INTO pmsi.t_specialites_medecin(oid, code, texte, type)
			SELECT oid, code, texte, type 
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_specialites_medecin);
			
			
			INSERT INTO pmsi.t_specialites_medecin(code, texte, type)
			SELECT code, texte, type 
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_specialites_medecin);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_modes_hospitalisation" label="Modes hospitalisation">
		<selectCmd><![CDATA[
		
			SELECT 
				t_modes_hospitalisation.oid as keepoid,
				t_modes_hospitalisation.code,
				t_modes_hospitalisation.texte
			FROM
				pmsi.t_modes_hospitalisation
			WHERE 
				t_modes_hospitalisation.code <> t_modes_hospitalisation.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_modes_hospitalisation
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_modes_hospitalisation.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_modes_hospitalisation.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_modes_hospitalisation.code IS DISTINCT FROM w_dbsetup.code
					)
				;
			
			INSERT INTO pmsi.t_modes_hospitalisation (oid,code,texte)
				SELECT 
					w_dbsetup.keepoid,
					w_dbsetup.code,
					w_dbsetup.texte
				FROM
					w_dbsetup
					LEFT JOIN pmsi.t_modes_hospitalisation ON t_modes_hospitalisation.oid = w_dbsetup.keepoid
				WHERE 
					t_modes_hospitalisation.oid IS NULL
				;
		
		]]></updateCmd>

	</TABLE>

	<TABLE name="t_disciplines_sae" label="Disciplines SAE">
		<selectCmd><![CDATA[
		
			SELECT 
				t_disciplines_sae.oid as keepoid,
				t_disciplines_sae.code,
				t_disciplines_sae.texte
			FROM
				pmsi.t_disciplines_sae
			WHERE 
				t_disciplines_sae.code <> t_disciplines_sae.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_disciplines_sae
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_disciplines_sae.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_disciplines_sae.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_disciplines_sae.code IS DISTINCT FROM w_dbsetup.code
					)
				;
			
			INSERT INTO pmsi.t_disciplines_sae (oid,code,texte)
				SELECT 
					w_dbsetup.keepoid,
					w_dbsetup.code,
					w_dbsetup.texte
				FROM
					w_dbsetup
					LEFT JOIN pmsi.t_disciplines_sae ON t_disciplines_sae.oid = w_dbsetup.keepoid
				WHERE 
					t_disciplines_sae.oid IS NULL
				;
		
		]]></updateCmd>




	</TABLE>

	<TABLE name="t_types_autorisations" label="Types d'autorisations">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_types_autorisations.code, 
				t_types_autorisations.texte, 
				t_types_autorisations.texte_court,
				t_modes_hospitalisation.code AS mode_hospitalisation_code,
				t_disciplines_sae.code AS discipline_sae_code
			FROM 
				pmsi.t_types_autorisations 
			JOIN pmsi.t_modes_hospitalisation ON t_types_autorisations.mode_hospitalisation_id = t_modes_hospitalisation.oid
			JOIN pmsi.t_disciplines_sae ON t_types_autorisations.discipline_sae_id = t_disciplines_sae.oid
			ORDER BY t_types_autorisations.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_types_autorisations SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				mode_hospitalisation_id = t_modes_hospitalisation.oid,
				discipline_sae_id = t_disciplines_sae.oid
			FROM w_dbsetup
			JOIN pmsi.t_modes_hospitalisation ON w_dbsetup.mode_hospitalisation_code = t_modes_hospitalisation.code
			JOIN pmsi.t_disciplines_sae ON w_dbsetup.discipline_sae_code = t_disciplines_sae.code
			WHERE t_types_autorisations.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_types_autorisations(code, texte, texte_court,mode_hospitalisation_id,discipline_sae_id)
			SELECT 
			w_dbsetup.code, 
			w_dbsetup.texte, 
			w_dbsetup.texte_court, 
			t_modes_hospitalisation.oid , 
			t_disciplines_sae.oid 
			FROM w_dbsetup
				JOIN pmsi.t_modes_hospitalisation ON w_dbsetup.mode_hospitalisation_code = t_modes_hospitalisation.code
				JOIN pmsi.t_disciplines_sae ON w_dbsetup.discipline_sae_code = t_disciplines_sae.code
			WHERE w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_types_autorisations);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_modes_entree" label="Modes d'entree">
		<selectCmd><![CDATA[ 
			
			SELECT code, texte, texte_court FROM pmsi.t_modes_entree ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_modes_entree SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_modes_entree.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_modes_entree(code, texte, texte_court)
			SELECT code, texte, texte_court 
			FROM w_dbsetup 
			WHERE code NOT IN (SELECT code FROM pmsi.t_modes_entree); 
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_modes_sortie" label="Modes de sortie">
		<selectCmd><![CDATA[ 
			
			SELECT code, texte, texte_court FROM pmsi.t_modes_sortie ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_modes_sortie SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_modes_sortie.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_modes_sortie(code, texte, texte_court)
			SELECT code, texte, texte_court 
			FROM w_dbsetup 
			WHERE code NOT IN (SELECT code FROM pmsi.t_modes_sortie);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_provenances_entree" label="Provenances d'entrée">
		<selectCmd><![CDATA[ 
			
			SELECT code, texte, texte_court FROM pmsi.t_provenances_entree ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_provenances_entree SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_provenances_entree.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_provenances_entree(code, texte, texte_court)
			SELECT code, texte, texte_court 
			FROM w_dbsetup 
			WHERE code NOT IN (SELECT code FROM pmsi.t_provenances_entree);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_destinations_sortie" label="Destinations de sortie">
		<selectCmd><![CDATA[ 
			
			SELECT code, texte, texte_court FROM pmsi.t_destinations_sortie ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_destinations_sortie SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_destinations_sortie.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_destinations_sortie(code, texte, texte_court)
			SELECT code, texte, texte_court 
			FROM w_dbsetup 
			WHERE code NOT IN (SELECT code FROM pmsi.t_destinations_sortie);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_passage_urgences" label="Passage Urgence">
		<selectCmd><![CDATA[
		
			SELECT 
				t_passage_urgences.oid as keepoid,
				t_passage_urgences.code,
				t_passage_urgences.texte,
				t_passage_urgences.texte_court
			FROM
				pmsi.t_passage_urgences
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_passage_urgences
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 
				t_passage_urgences.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_passage_urgences.texte_court IS DISTINCT FROM w_dbsetup.texte_court
					OR t_passage_urgences.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_passage_urgences.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_passage_urgences (oid,code,texte,texte_court)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte,
                w_dbsetup.texte_court
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_passage_urgences ON t_passage_urgences.oid = w_dbsetup.keepoid
            WHERE 
                t_passage_urgences.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_prestations" label="Spécialités médecin">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court,
				compteur_rsf,
				type_ghs,
				supplement,
				statut,
				date_debut_prive_1,
				date_fin_prive_1,
				tarif_prive_1,
				date_debut_prive_2,
				date_fin_prive_2,
				tarif_prive_2,
				date_debut_prive_3,
				date_fin_prive_3,
				tarif_prive_3,
				date_debut_prive_4,
				date_fin_prive_4,
				tarif_prive_4,
				date_debut_prive_5,
				date_fin_prive_5,
				tarif_prive_5,
				date_debut_prive_6,
				date_fin_prive_6,
				tarif_prive_6,
				date_debut_prive_7,
				date_fin_prive_7,
				tarif_prive_7,
				date_debut_prive_8,
				date_fin_prive_8,
				tarif_prive_8,
				date_debut_prive_9,
				date_fin_prive_9,
				tarif_prive_9,
				date_debut_public_1,
				date_fin_public_1,
				tarif_public_1,
				date_debut_public_2,
				date_fin_public_2,
				tarif_public_2,
				date_debut_public_3,
				date_fin_public_3,
				tarif_public_3,
				date_debut_public_4,
				date_fin_public_4,
				tarif_public_4,
				date_debut_public_5,
				date_fin_public_5,
				tarif_public_5,
				date_debut_public_6,
				date_fin_public_6,
				tarif_public_6,
				date_debut_public_7,
				date_fin_public_7,
				tarif_public_7,
				date_debut_public_8,
				date_fin_public_8,
				tarif_public_8,
				date_debut_public_9,
				date_fin_public_9,
				tarif_public_9
			FROM pmsi.t_prestations
			WHERE code != texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_prestations SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_prestations.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_prestations SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				compteur_rsf = w_dbsetup.compteur_rsf,
				type_ghs = w_dbsetup.type_ghs,
				supplement = w_dbsetup.supplement,
				statut = w_dbsetup.statut
			FROM w_dbsetup
			WHERE t_prestations.code = w_dbsetup.code
			;

			INSERT INTO pmsi.t_prestations(
				oid, code, texte, texte_court, compteur_rsf, type_ghs, supplement, statut
				)
				SELECT oid, code, texte, texte_court, compteur_rsf, type_ghs, supplement, statut
				FROM w_dbsetup 
				WHERE 1=1
					AND oid = 0 
					AND oid NOT IN (SELECT oid FROM pmsi.t_prestations)
			;
				
			INSERT INTO pmsi.t_prestations(
				code, texte, texte_court, compteur_rsf, type_ghs, supplement, statut
				)
				SELECT code, texte, texte_court, compteur_rsf, type_ghs, supplement, statut
				FROM w_dbsetup 
				WHERE 1=1
					AND oid <> 0 
					AND code NOT IN (SELECT code FROM pmsi.t_prestations)
			;
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_prestation_tarif" label="Tarif des prestations">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_prestation_tarif.code,
				t_prestation_tarif.texte,
				t_prestation_tarif.texte_court,
				t_prestation_tarif.type_ets,
				t_prestation_tarif.date_debut,
				t_prestation_tarif.date_fin,
				t_prestation_tarif.tarif,
				t_prestations.code AS prestation_code
			FROM pmsi.t_prestation_tarif
				JOIN pmsi.t_prestations ON t_prestations.oid = t_prestation_tarif.prestation_id
			WHERE t_prestation_tarif.code != t_prestation_tarif.texte
			ORDER BY t_prestation_tarif.code, t_prestation_tarif.type_ets, t_prestation_tarif.date_debut DESC
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			TRUNCATE pmsi.t_prestation_tarif
			;
			
			INSERT INTO pmsi.t_prestation_tarif (
				code,
				texte,
				texte_court,
				type_ets,
				date_debut,
				date_fin,
				tarif,
				prestation_id
				)
			SELECT 
				w_dbsetup.code,
				w_dbsetup.texte,
				w_dbsetup.texte_court,
				w_dbsetup.type_ets,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin,
				w_dbsetup.tarif,
				t_prestations.oid
			FROM w_dbsetup
				JOIN pmsi.t_prestations ON t_prestations.code = w_dbsetup.prestation_code
			;
			
			-- Replication sur table des prestations
			UPDATE pmsi.t_prestations SET
				date_debut_prive_1 = date_debut_1,
				date_fin_prive_1 = date_fin_1,
				tarif_prive_1 = tarif_1,
				date_debut_prive_2 = date_debut_2,
				date_fin_prive_2 = date_fin_2,
				tarif_prive_2 = tarif_2,
				date_debut_prive_3 = date_debut_3,
				date_fin_prive_3 = date_fin_3,
				tarif_prive_3 = tarif_3,
				date_debut_prive_4 = date_debut_4,
				date_fin_prive_4 = date_fin_4,
				tarif_prive_4 = tarif_4,
				date_debut_prive_5 = date_debut_5,
				date_fin_prive_5 = date_fin_5,
				tarif_prive_5 = tarif_5,
				date_debut_prive_6 = date_debut_6,
				date_fin_prive_6 = date_fin_6,
				tarif_prive_6 = tarif_6,
				date_debut_prive_7 = date_debut_7,
				date_fin_prive_7 = date_fin_7,
				tarif_prive_7 = tarif_7,
				date_debut_prive_8 = date_debut_8,
				date_fin_prive_8 = date_fin_8,
				tarif_prive_8 = tarif_8,
				date_debut_prive_9 = date_debut_9,
				date_fin_prive_9 = date_fin_9,
				tarif_prive_9 = tarif_9
			FROM(
			SELECT
				prestation_id,
				MAX(CASE WHEN rang = 9 THEN date_debut ELSE NULL END) AS date_debut_9,
				MAX(CASE WHEN rang = 9 THEN date_fin ELSE NULL END) AS date_fin_9,
				MAX(CASE WHEN rang = 9 THEN tarif ELSE NULL END) AS tarif_9,
				MAX(CASE WHEN rang = 8 THEN date_debut ELSE NULL END) AS date_debut_8,
				MAX(CASE WHEN rang = 8 THEN date_fin ELSE NULL END) AS date_fin_8,
				MAX(CASE WHEN rang = 8 THEN tarif ELSE NULL END) AS tarif_8,
				MAX(CASE WHEN rang = 7 THEN date_debut ELSE NULL END) AS date_debut_7,
				MAX(CASE WHEN rang = 7 THEN date_fin ELSE NULL END) AS date_fin_7,
				MAX(CASE WHEN rang = 7 THEN tarif ELSE NULL END) AS tarif_7,
				MAX(CASE WHEN rang = 6 THEN date_debut ELSE NULL END) AS date_debut_6,
				MAX(CASE WHEN rang = 6 THEN date_fin ELSE NULL END) AS date_fin_6,
				MAX(CASE WHEN rang = 6 THEN tarif ELSE NULL END) AS tarif_6,
				MAX(CASE WHEN rang = 5 THEN date_debut ELSE NULL END) AS date_debut_5,
				MAX(CASE WHEN rang = 5 THEN date_fin ELSE NULL END) AS date_fin_5,
				MAX(CASE WHEN rang = 5 THEN tarif ELSE NULL END) AS tarif_5,
				MAX(CASE WHEN rang = 4 THEN date_debut ELSE NULL END) AS date_debut_4,
				MAX(CASE WHEN rang = 4 THEN date_fin ELSE NULL END) AS date_fin_4,
				MAX(CASE WHEN rang = 4 THEN tarif ELSE NULL END) AS tarif_4,
				MAX(CASE WHEN rang = 3 THEN date_debut ELSE NULL END) AS date_debut_3,
				MAX(CASE WHEN rang = 3 THEN date_fin ELSE NULL END) AS date_fin_3,
				MAX(CASE WHEN rang = 3 THEN tarif ELSE NULL END) AS tarif_3,
				MAX(CASE WHEN rang = 2 THEN date_debut ELSE NULL END) AS date_debut_2,
				MAX(CASE WHEN rang = 2 THEN date_fin ELSE NULL END) AS date_fin_2,
				MAX(CASE WHEN rang = 2 THEN tarif ELSE NULL END) AS tarif_2,
				MAX(CASE WHEN rang = 1 THEN date_debut ELSE NULL END) AS date_debut_1,
				MAX(CASE WHEN rang = 1 THEN date_fin ELSE NULL END) AS date_fin_1,
				MAX(CASE WHEN rang = 1 THEN tarif ELSE NULL END) AS tarif_1
			FROM
			(
			SELECT prestation_id, date_debut, date_fin, tarif,
				row_number() OVER (PARTITION BY prestation_id ORDER BY date_debut) +
				9 - count(*) OVER (PARTITION BY prestation_id) AS rang
			FROM pmsi.t_prestation_tarif
			WHERE type_ets = '1'
			ORDER BY 1,2 desc
			) subview
			GROUP BY 1
			) subview
			WHERE t_prestations.oid = subview.prestation_id
			;

			UPDATE pmsi.t_prestations SET
				date_debut_public_1 = date_debut_1,
				date_fin_public_1 = date_fin_1,
				tarif_public_1 = tarif_1,
				date_debut_public_2 = date_debut_2,
				date_fin_public_2 = date_fin_2,
				tarif_public_2 = tarif_2,
				date_debut_public_3 = date_debut_3,
				date_fin_public_3 = date_fin_3,
				tarif_public_3 = tarif_3,
				date_debut_public_4 = date_debut_4,
				date_fin_public_4 = date_fin_4,
				tarif_public_4 = tarif_4,
				date_debut_public_5 = date_debut_5,
				date_fin_public_5 = date_fin_5,
				tarif_public_5 = tarif_5,
				date_debut_public_6 = date_debut_6,
				date_fin_public_6 = date_fin_6,
				tarif_public_6 = tarif_6,
				date_debut_public_7 = date_debut_7,
				date_fin_public_7 = date_fin_7,
				tarif_public_7 = tarif_7,
				date_debut_public_8 = date_debut_8,
				date_fin_public_8 = date_fin_8,
				tarif_public_8 = tarif_8,
				date_debut_public_9 = date_debut_9,
				date_fin_public_9 = date_fin_9,
				tarif_public_9 = tarif_9
			FROM(
			SELECT
				prestation_id,
				MAX(CASE WHEN rang = 9 THEN date_debut ELSE NULL END) AS date_debut_9,
				MAX(CASE WHEN rang = 9 THEN date_fin ELSE NULL END) AS date_fin_9,
				MAX(CASE WHEN rang = 9 THEN tarif ELSE NULL END) AS tarif_9,
				MAX(CASE WHEN rang = 8 THEN date_debut ELSE NULL END) AS date_debut_8,
				MAX(CASE WHEN rang = 8 THEN date_fin ELSE NULL END) AS date_fin_8,
				MAX(CASE WHEN rang = 8 THEN tarif ELSE NULL END) AS tarif_8,
				MAX(CASE WHEN rang = 7 THEN date_debut ELSE NULL END) AS date_debut_7,
				MAX(CASE WHEN rang = 7 THEN date_fin ELSE NULL END) AS date_fin_7,
				MAX(CASE WHEN rang = 7 THEN tarif ELSE NULL END) AS tarif_7,
				MAX(CASE WHEN rang = 6 THEN date_debut ELSE NULL END) AS date_debut_6,
				MAX(CASE WHEN rang = 6 THEN date_fin ELSE NULL END) AS date_fin_6,
				MAX(CASE WHEN rang = 6 THEN tarif ELSE NULL END) AS tarif_6,
				MAX(CASE WHEN rang = 5 THEN date_debut ELSE NULL END) AS date_debut_5,
				MAX(CASE WHEN rang = 5 THEN date_fin ELSE NULL END) AS date_fin_5,
				MAX(CASE WHEN rang = 5 THEN tarif ELSE NULL END) AS tarif_5,
				MAX(CASE WHEN rang = 4 THEN date_debut ELSE NULL END) AS date_debut_4,
				MAX(CASE WHEN rang = 4 THEN date_fin ELSE NULL END) AS date_fin_4,
				MAX(CASE WHEN rang = 4 THEN tarif ELSE NULL END) AS tarif_4,
				MAX(CASE WHEN rang = 3 THEN date_debut ELSE NULL END) AS date_debut_3,
				MAX(CASE WHEN rang = 3 THEN date_fin ELSE NULL END) AS date_fin_3,
				MAX(CASE WHEN rang = 3 THEN tarif ELSE NULL END) AS tarif_3,
				MAX(CASE WHEN rang = 2 THEN date_debut ELSE NULL END) AS date_debut_2,
				MAX(CASE WHEN rang = 2 THEN date_fin ELSE NULL END) AS date_fin_2,
				MAX(CASE WHEN rang = 2 THEN tarif ELSE NULL END) AS tarif_2,
				MAX(CASE WHEN rang = 1 THEN date_debut ELSE NULL END) AS date_debut_1,
				MAX(CASE WHEN rang = 1 THEN date_fin ELSE NULL END) AS date_fin_1,
				MAX(CASE WHEN rang = 1 THEN tarif ELSE NULL END) AS tarif_1
			FROM
			(
			SELECT prestation_id, date_debut, date_fin, tarif,
				row_number() OVER (PARTITION BY prestation_id ORDER BY date_debut) +
				9 - count(*) OVER (PARTITION BY prestation_id) AS rang
			FROM pmsi.t_prestation_tarif
			WHERE type_ets = '2'
			ORDER BY 1,2 desc
			) subview
			GROUP BY 1
			) subview
			WHERE t_prestations.oid = subview.prestation_id
			;

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_lpp" label="LPP">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_lpp.oid = 0 THEN t_lpp.oid ELSE 1 END AS oid,
				t_lpp.code, 
				t_lpp.texte, 
				t_lpp.texte_court, 
				t_lpp.code_fg11, 
				t_prestations.code AS prestation_defaut_code 
			FROM 
				pmsi.t_lpp, 
				pmsi.t_prestations
			WHERE 
				t_lpp.prestation_defaut_id = t_prestations.oid
				AND t_lpp.code <> t_lpp.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_lpp SET 
				code = w_dbsetup.code,
				prestation_defaut_id = 0
			FROM w_dbsetup
			WHERE 
				t_lpp.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;

			UPDATE pmsi.t_lpp SET 
				code_fg11 = w_dbsetup.code_fg11, 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				prestation_defaut_id = t_prestations.oid
			FROM w_dbsetup, pmsi.t_prestations
			WHERE 
				t_lpp.code = w_dbsetup.code
				AND w_dbsetup.prestation_defaut_code = t_prestations.code
				AND (
					t_lpp.code_fg11 <> w_dbsetup.code_fg11 OR
					t_lpp.texte <> w_dbsetup.texte OR
					t_lpp.texte_court <> w_dbsetup.texte_court OR
					t_lpp.prestation_defaut_id <> t_prestations.oid OR
					t_lpp.code_fg11 IS NULL OR
					t_lpp.texte IS NULL OR
					t_lpp.texte_court IS NULL OR
					t_lpp.prestation_defaut_id IS NULL 
				);
			
			
			INSERT INTO pmsi.t_lpp(
				oid, 
				code, 
				texte, 
				texte_court, 
				code_fg11, 
				prestation_defaut_id)
			SELECT 
				oid, 
				code, 
				texte, 
				texte_court, 
				code_fg11, 
				0 
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_lpp);
			
			
			INSERT INTO pmsi.t_lpp(
				code, 
				texte, 
				texte_court, 
				code_fg11,  
				prestation_defaut_id)
			SELECT 
				w_dbsetup.code,
				w_dbsetup.texte, 
				w_dbsetup.texte_court, 
				w_dbsetup.code_fg11, 
				t_prestations.oid 
			FROM w_dbsetup, pmsi.t_prestations 
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.prestation_defaut_code = t_prestations.code
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_lpp);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_lpp_tarifs" label="Tarifs LPP">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_lpp.code AS lpp_code, 
				t_lpp_tarifs.rang, 
				t_lpp_tarifs.date_debut, 
				t_lpp_tarifs.date_fin, 
				t_prestations.code AS prestation_code, 
				t_lpp_tarifs.prix_unitaire 
			FROM 
				pmsi.t_lpp_tarifs, 
				pmsi.t_lpp, 
				pmsi.t_prestations
			WHERE 
				t_lpp_tarifs.lpp_id = t_lpp.oid 
				AND t_lpp_tarifs.prestation_id = t_prestations.oid
				AND t_lpp.code != t_lpp.texte
			ORDER BY t_lpp.code, t_lpp_tarifs.rang DESC;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			DELETE FROM pmsi.t_lpp_tarifs;
			
			
			INSERT INTO pmsi.t_lpp_tarifs(
				lpp_id, 
				rang, 
				date_debut, 
				date_fin, 
				prestation_id, 
				prix_unitaire)
			SELECT 
				t_lpp.oid, 
				w_dbsetup.rang,
				w_dbsetup.date_debut, 
				w_dbsetup.date_fin, 
				t_prestations.oid, 
				w_dbsetup.prix_unitaire
			FROM 
				w_dbsetup, 
				pmsi.t_lpp, 
				pmsi.t_prestations 
			WHERE 
				w_dbsetup.lpp_code = t_lpp.code 
				AND w_dbsetup.prestation_code = t_prestations.code;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ucd" label="UCD">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_ucd.oid = 0 THEN t_ucd.oid ELSE 1 END AS oid,
				t_ucd.code, 
				t_ucd.texte, 
				t_ucd.texte_court, 
				t_ucd.laboratoire_texte, 
				t_ucd.conditionnement_texte, 
				t_ucd.atc_code, 
				t_ucd.atc_texte, 
				t_ucd.ephmra_code, 
				t_ucd.ephmra_texte, 
				t_prestations.code AS prestation_defaut_code 
			FROM 
				pmsi.t_ucd, 
				pmsi.t_prestations
			WHERE 
				t_ucd.prestation_defaut_id = t_prestations.oid
				AND t_ucd.code <> t_ucd.texte
			ORDER BY code; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_ucd SET 
				code = w_dbsetup.code,
				prestation_defaut_id = 0
			FROM w_dbsetup
			WHERE t_ucd.oid = w_dbsetup.oid 
			AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_ucd SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				laboratoire_texte = w_dbsetup.laboratoire_texte, 
				conditionnement_texte = w_dbsetup.conditionnement_texte, 
				atc_code = w_dbsetup.atc_code, 
				atc_texte = w_dbsetup.atc_texte,
				ephmra_code = w_dbsetup.ephmra_code, 
				ephmra_texte = w_dbsetup.ephmra_texte, 
				prestation_defaut_id = t_prestations.oid
			FROM 
				w_dbsetup, 
				pmsi.t_prestations
			WHERE 
				t_ucd.code = w_dbsetup.code
				AND w_dbsetup.prestation_defaut_code = t_prestations.code
				AND (
					t_ucd.texte <> w_dbsetup.texte OR
					t_ucd.texte_court <> w_dbsetup.texte_court OR
					t_ucd.laboratoire_texte <> w_dbsetup.laboratoire_texte OR
					t_ucd.conditionnement_texte <> w_dbsetup.conditionnement_texte OR
					t_ucd.atc_code <> w_dbsetup.atc_code OR
					t_ucd.atc_texte <> w_dbsetup.atc_texte OR
					t_ucd.ephmra_code <> w_dbsetup.ephmra_code OR
					t_ucd.ephmra_texte <> w_dbsetup.ephmra_texte OR
					t_ucd.prestation_defaut_id <> t_prestations.oid OR
					t_ucd.texte IS NULL OR
					t_ucd.texte_court IS NULL OR
					t_ucd.laboratoire_texte IS NULL OR
					t_ucd.conditionnement_texte IS NULL OR
					t_ucd.atc_code IS NULL OR
					t_ucd.atc_texte IS NULL OR
					t_ucd.ephmra_code IS NULL OR
					t_ucd.ephmra_texte IS NULL OR
					t_ucd.prestation_defaut_id IS NULL
				);
			
			
			INSERT INTO pmsi.t_ucd(
				oid, 
				code, 
				texte, 
				texte_court, 
				laboratoire_texte, 
				conditionnement_texte, 
				atc_code, 
				atc_texte, 
				ephmra_code, 
				ephmra_texte, 
				prestation_defaut_id)
			SELECT 
				oid, 
				code, 
				texte, 
				texte_court, 
				laboratoire_texte, 
				conditionnement_texte, 
				atc_code, 
				atc_texte, 
				ephmra_code, 
				ephmra_texte, 
				0 
			FROM w_dbsetup 
			WHERE
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_ucd);
			
			
			INSERT INTO pmsi.t_ucd(
				code, 
				texte, 
				texte_court, 
				laboratoire_texte, 
				conditionnement_texte, 
				atc_code, 
				atc_texte, 
				ephmra_code, ephmra_texte, 
				prestation_defaut_id)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.texte_court, 
				w_dbsetup.laboratoire_texte, 
				conditionnement_texte, 
				atc_code, 
				atc_texte, 
				ephmra_code, 
				ephmra_texte,
				t_prestations.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_prestations 
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.prestation_defaut_code = t_prestations.code
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_ucd);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_type_ucd" label="Type UCD">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_type_ucd.oid as keepoid, 
				t_type_ucd.code, 
				t_type_ucd.texte
			FROM 
				pmsi.t_type_ucd
			WHERE 
				t_type_ucd.code <> t_type_ucd.texte
			ORDER BY code; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_type_ucd
			SET
				code = w_dbsetup.code,
                texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_type_ucd.oid = w_dbsetup.keepoid
				AND ( 1!=1
                    OR t_type_ucd.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_type_ucd.code IS DISTINCT FROM w_dbsetup.code
                    )
			;
			
			INSERT INTO pmsi.t_type_ucd(oid, code, texte)
				SELECT 
                    w_dbsetup.keepoid,
                    w_dbsetup.code,
                    w_dbsetup.texte
				FROM
                    w_dbsetup
                    LEFT JOIN pmsi.t_type_ucd ON t_type_ucd.oid = w_dbsetup.keepoid
				WHERE
                    t_type_ucd.oid IS NULL
			;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_ucd_tarifs" label="Tarifs UCD">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_ucd.code AS ucd_code, 
				t_ucd_tarifs.rang, 
				t_ucd_tarifs.date_debut, 
				t_ucd_tarifs.date_fin, 
				t_prestations.code AS prestation_code, 
				t_ucd_tarifs.prix_unitaire_ht, 
				t_ucd_tarifs.prix_unitaire_ttc, 
				top_retrocession, 
				top_taa, 
				top_retrocession_date_fin, 
				top_taa_date_fin
			FROM 
				pmsi.t_ucd_tarifs, 
				pmsi.t_ucd, 
				pmsi.t_prestations
			WHERE 
				t_ucd_tarifs.ucd_id = t_ucd.oid 
				AND t_ucd_tarifs.prestation_id = t_prestations.oid
				AND t_ucd.code != t_ucd.texte
			ORDER BY t_ucd.code, t_ucd_tarifs.rang DESC;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			DELETE FROM pmsi.t_ucd_tarifs;
			
			INSERT INTO pmsi.t_ucd_tarifs(
				ucd_id, 
				rang, 
				date_debut, 
				date_fin, 
				prestation_id, 
				prix_unitaire_ht, 
				prix_unitaire_ttc, 
				top_retrocession, 
				top_taa, 
				top_retrocession_date_fin, 
				top_taa_date_fin)
			SELECT 
				t_ucd.oid, w_dbsetup.rang, 
				w_dbsetup.date_debut, 
				w_dbsetup.date_fin, 
				t_prestations.oid, 
				w_dbsetup.prix_unitaire_ht, 
				w_dbsetup.prix_unitaire_ttc, 
				w_dbsetup.top_retrocession, 
				w_dbsetup.top_taa, 
				w_dbsetup.top_retrocession_date_fin, 
				w_dbsetup.top_taa_date_fin
			FROM 
				w_dbsetup, 
				pmsi.t_ucd, 
				pmsi.t_prestations 
			WHERE
				w_dbsetup.ucd_code = t_ucd.code 
				AND w_dbsetup.prestation_code = t_prestations.code;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_indication" label="Indication UCD">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_indication.oid = 0 THEN t_indication.oid ELSE 1 END AS oid, 
				t_indication.code, 
				t_indication.texte, 
				t_indication.texte_court 
			FROM 
				pmsi.t_indication
			WHERE 
                t_indication.code <> t_indication.texte
			ORDER BY code; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_indication SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_indication.oid = w_dbsetup.oid 
			AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_indication SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM 
				w_dbsetup
			WHERE 
				t_indication.code = w_dbsetup.code
				AND (
					t_indication.texte <> w_dbsetup.texte OR
					t_indication.texte_court <> w_dbsetup.texte_court OR
					t_indication.texte IS NULL OR
					t_indication.texte_court IS NULL
				);
			
			
			INSERT INTO pmsi.t_indication(
				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_indication);
			
			
			INSERT INTO pmsi.t_indication(
				code, 
				texte, 
				texte_court)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.texte_court
			FROM 
				w_dbsetup 
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_indication);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_expert_controle" label="t_expert_controle">
		<selectCmd><![CDATA[
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court,
				is_datim,
				code_datim,
				COALESCE(date_validite_debut,'20100301') AS date_validite_debut,
				COALESCE(date_validite_fin,'20991231') AS date_validite_fin,
				is_global,
				texte_abrege,
				description,
				is_cti
			FROM pmsi.t_expert_controle
			WHERE is_cti = '1' AND code <> ''
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			DELETE FROM pmsi.t_expert_controle
			WHERE is_datim = '1' AND 
				split_part(code,'_',2) <> '' AND
				split_part(code,'_',1) IN (SELECT code FROM pmsi.t_expert_controle);

			
			UPDATE pmsi.t_expert_controle SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court, 
				texte_abrege = w_dbsetup.texte_abrege, 
				description = w_dbsetup.description, 
				code_datim = w_dbsetup.code_datim, 
				is_datim = w_dbsetup.is_datim, 
				is_cti = w_dbsetup.is_cti, 
				is_global = w_dbsetup.is_global, 
				date_validite_debut = w_dbsetup.date_validite_debut, 
				date_validite_fin = w_dbsetup.date_validite_fin
			FROM w_dbsetup
			WHERE t_expert_controle.code = w_dbsetup.code
			AND (
				t_expert_controle.texte IS DISTINCT FROM w_dbsetup.texte OR
				t_expert_controle.texte_court IS DISTINCT FROM w_dbsetup.texte_court OR
				t_expert_controle.texte_abrege IS DISTINCT FROM w_dbsetup.texte_abrege OR
				t_expert_controle.description IS DISTINCT FROM w_dbsetup.description OR
				t_expert_controle.code_datim IS DISTINCT FROM w_dbsetup.code_datim OR
				t_expert_controle.is_datim IS DISTINCT FROM w_dbsetup.is_datim OR 
				t_expert_controle.is_cti IS DISTINCT FROM w_dbsetup.is_cti OR
				t_expert_controle.is_global IS DISTINCT FROM w_dbsetup.is_global OR
				t_expert_controle.date_validite_debut IS DISTINCT FROM w_dbsetup.date_validite_debut OR
				t_expert_controle.date_validite_fin IS DISTINCT FROM w_dbsetup.date_validite_fin
			);
			
			UPDATE pmsi.t_expert_controle
			SET gravite_id = 5 
			WHERE gravite_id IS NULL OR gravite_id NOT IN (0,5,9);
			
			UPDATE pmsi.t_expert_controle
			SET is_hide = '0' 
			WHERE is_hide IS NULL OR is_hide NOT IN ('0','1');
			
			
			INSERT INTO pmsi.t_expert_controle(
				code,
				texte,
				texte_court,
				is_datim,
				code_datim,
				date_validite_debut,
				date_validite_fin,
				is_global,
				texte_abrege,
				description,
				is_cti,
				gravite_id,
				is_hide)
			SELECT 
				code,
				texte,
				texte_court,
				is_datim,
				code_datim,
				date_validite_debut,
				date_validite_fin,
				is_global,
				texte_abrege,
				description,
				is_cti,
				5,
				'0'
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_expert_controle WHERE code IS NOT NULL);
			
			

			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_expert_controle_rule" label="t_expert_controle_rule">
		<selectCmd><![CDATA[
			
			SELECT
				CASE WHEN t_expert_controle_rule.oid = 0 THEN t_expert_controle_rule.oid ELSE 1 END AS oid, 
				t_expert_controle.code AS controle_code,
				t_expert_controle_rule.numero, 
				t_expert_controle_rule.sqlcmd_where, 
				t_expert_controle_rule.sqlcmd_justificatif
			FROM pmsi.t_expert_controle_rule
			JOIN pmsi.t_expert_controle ON controle_id = t_expert_controle.oid
			WHERE t_expert_controle.is_cti = '1' AND
				t_expert_controle.code <> ''
			ORDER BY t_expert_controle.code, t_expert_controle_rule.numero
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE pmsi.t_expert_controle_rule
			SET numero = 1
			WHERE numero IS NULL or NUMERO NOT IN (1,2,3);
			
			UPDATE pmsi.t_expert_controle_rule
			SET 
				sqlcmd_where = w_dbsetup.sqlcmd_where,
				sqlcmd_justificatif = w_dbsetup.sqlcmd_justificatif
			FROM w_dbsetup
				JOIN pmsi.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
			WHERE t_expert_controle_rule.controle_id = t_expert_controle.oid AND
				t_expert_controle_rule.numero = w_dbsetup.numero AND
				(
				t_expert_controle_rule.sqlcmd_where IS DISTINCT FROM w_dbsetup.sqlcmd_where OR
				t_expert_controle_rule.sqlcmd_justificatif IS DISTINCT FROM w_dbsetup.sqlcmd_justificatif
				);
				
			INSERT INTO pmsi.t_expert_controle_rule (
				controle_id,
				numero, 
				sqlcmd_where, 
				sqlcmd_justificatif
			)
			SELECT
				t_expert_controle.oid,
				w_dbsetup.numero, 
				w_dbsetup.sqlcmd_where, 
				w_dbsetup.sqlcmd_justificatif
			FROM w_dbsetup
				JOIN pmsi.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
				LEFT JOIN pmsi.t_expert_controle_rule ON
					t_expert_controle_rule.controle_id = t_expert_controle.oid AND
					t_expert_controle_rule.numero = w_dbsetup.numero
			WHERE t_expert_controle_rule.oid IS NULL;	
			

			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_tables" label="Tables pour les listes">
		<selectCmd><![CDATA[ 
			
			SELECT
				code,
				name,
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				select_cmd,
				texte
			FROM pmsi.t_listes_tables
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_listes_tables SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte, 
				select_cmd = w_dbsetup.select_cmd
			FROM w_dbsetup
			WHERE t_listes_tables.name = w_dbsetup.name;
			
			
			INSERT INTO pmsi.t_listes_tables(oid, code, name, texte, select_cmd)
			SELECT 
				oid, 
				code, 
				name, 
				texte, 
				select_cmd 
			FROM 
				w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_listes_tables);
				
				
			INSERT INTO pmsi.t_listes_tables(code, name, texte, select_cmd)
			SELECT 
				code, 
				name, 
				texte, 
				select_cmd 
			FROM 
				w_dbsetup 
			WHERE 
				oid <> 0 
				AND name NOT IN (SELECT name FROM pmsi.t_listes_tables);

			DELETE FROM pmsi.t_listes_tables WHERE code = 'RSS';
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes" label="Listes de codes">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_listes.oid = 0 THEN t_listes.oid ELSE 1 END AS oid, 
				t_listes.code,
				t_listes.texte,
				t_listes.chapitre,
				t_listes.is_cti, 
				t_listes.show_omit, 
				t_listes.utilisateur_createur, 
				t_listes.est_partagee_modification,
				t_listes.contenu_by_code,
				t_listes.include_code,
				t_listes.exclude_code,
				t_listes_tables.name as table_name 
			FROM 
				pmsi.t_listes, 
				pmsi.t_listes_tables 
			WHERE 
				is_cti = '1' 
				AND t_listes.code <> ''
				AND table_id = t_listes_tables.oid
			ORDER BY t_listes.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_listes SET 
				texte = w_dbsetup.texte, 
				chapitre = w_dbsetup.chapitre, 
				is_cti = w_dbsetup.is_cti,
				show_omit = w_dbsetup.show_omit,
				utilisateur_createur = w_dbsetup.utilisateur_createur,
				est_partagee_modification = w_dbsetup.est_partagee_modification,
				table_id = t_listes_tables.oid,
                contenu_by_code = w_dbsetup.contenu_by_code,
                include_code = w_dbsetup.include_code,
                exclude_code = w_dbsetup.exclude_code
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables
			WHERE 
				w_dbsetup.table_name = t_listes_tables.name 
				AND t_listes.code = w_dbsetup.code
			;
			
			
			INSERT INTO pmsi.t_listes(
				code, 
				texte, 
				chapitre,
				is_cti, 
				show_omit, 
				utilisateur_createur, 
				est_partagee_modification, 
				table_id,
                contenu_by_code,
                include_code,
                exclude_code
				)
				SELECT 
					w_dbsetup.code, 
					w_dbsetup.texte, 
					w_dbsetup.chapitre,
					w_dbsetup.is_cti, 
					w_dbsetup.show_omit, 
					w_dbsetup.utilisateur_createur, 
					w_dbsetup.est_partagee_modification, 
					t_listes_tables.oid,
                    w_dbsetup.contenu_by_code,
                    w_dbsetup.include_code,
                    w_dbsetup.exclude_code
				FROM 
					w_dbsetup, 
					pmsi.t_listes_tables 
				WHERE 
					w_dbsetup.table_name = t_listes_tables.name 
					AND w_dbsetup.code NOT IN (SELECT code FROM pmsi.t_listes) 
					AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
			;
			
			
			DELETE FROM pmsi.t_listes WHERE texte like '*SUPPRIME%' AND is_cti = '1'
			;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_ghm" label="Contenu des listes : GHM">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_ghm.code as to_code 
			FROM 
				pmsi.t_listes_contenu, 
				pmsi.t_listes, 
				pmsi.t_listes_tables,
				pmsi.t_ghm 
			WHERE 
				liste_id = t_listes.oid
				AND t_listes.code <> '' 
				AND t_listes.is_cti = '1' 
				AND t_listes.table_id = t_listes_tables.oid 
				AND t_listes_tables.name = 't_ghm' 
				AND to_id = t_ghm.oid 
			ORDER BY 1, 2; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_ghm.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_ghm ON w_dbsetup.to_code = t_ghm.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_ghm.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE 
				t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
					( 	SELECT t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
						FROM
							pmsi.t_listes_contenu, 
							pmsi.t_listes, 
							pmsi.t_ghm 
						WHERE 
							t_listes_contenu.liste_id = t_listes.oid
							AND t_listes_contenu.to_id = t_ghm.oid
							AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
							AND t_listes.code::text || '|'::text || t_ghm.code::text NOT IN (	SELECT 
																				liste_code::text || '|'::text || to_code::text 
																			FROM w_dbsetup)
					);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_ghs" label="Contenu des listes : GHS">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_ghs.code as to_code
			FROM 
				pmsi.t_listes_contenu, 
				pmsi.t_listes, 
				pmsi.t_listes_tables, 
				pmsi.t_ghs 
			WHERE 
				liste_id = t_listes.oid 
				AND t_listes.is_cti = '1'
				AND t_listes.code <> '' 
				AND t_listes.table_id = t_listes_tables.oid 
				AND t_listes_tables.name = 't_ghs' 
				AND to_id = t_ghs.oid 
			ORDER BY 1, 2;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_ghs.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_ghs ON w_dbsetup.to_code = t_ghs.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_ghs.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
				(	SELECT 
						t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
					FROM
						pmsi.t_listes_contenu,
						pmsi.t_listes, 
						pmsi.t_ghs 
					WHERE 
						t_listes_contenu.liste_id = t_listes.oid
						AND t_listes_contenu.to_id = t_ghs.oid
						AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
						AND t_listes.code::text || '|'::text || trim(to_char(t_ghs.code,'9999'))::text NOT IN (SELECT 
																								liste_code::text || '|'::text || trim(to_char(to_code,'9999'))::text 
																							FROM w_dbsetup)
				);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_diagnostics" label="Contenu des listes : Diagnostics">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_diagnostics.code as to_code 
			FROM
				pmsi.t_listes_contenu, 
				pmsi.t_listes, 
				pmsi.t_listes_tables, 
				pmsi.t_diagnostics 
			WHERE 
				liste_id = t_listes.oid 
				AND t_listes.is_cti = '1'
				AND t_listes.code <> '' 
				AND t_listes.table_id = t_listes_tables.oid 
				AND t_listes_tables.name = 't_diagnostics' 
				AND to_id = t_diagnostics.oid 
			ORDER BY 1, 2;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_diagnostics.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_diagnostics ON w_dbsetup.to_code = t_diagnostics.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_diagnostics.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
				(SELECT 
					t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
				FROM 
					pmsi.t_listes_contenu, 
					pmsi.t_listes, 
					pmsi.t_diagnostics 
				WHERE t_listes_contenu.liste_id = t_listes.oid
				AND t_listes_contenu.to_id = t_diagnostics.oid
				AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
				AND t_listes.code::text || '|'::text || t_diagnostics.code::text NOT IN (SELECT liste_code::text || '|'::text || to_code::text FROM w_dbsetup)
				);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_actes" label="Contenu des listes : Actes">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_actes.code as to_code 
			FROM 
				pmsi.t_listes_contenu, 
				pmsi.t_listes, 
				pmsi.t_listes_tables, 
				pmsi.t_actes 
			WHERE 
				liste_id = t_listes.oid 
				AND t_listes.is_cti = '1'
				AND t_listes.code <> '' 
				AND t_listes.table_id = t_listes_tables.oid 
				AND t_listes_tables.name = 't_actes' 
				AND to_id = t_actes.oid 
			ORDER BY 1, 2; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_actes.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_actes ON w_dbsetup.to_code = t_actes.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_actes.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
				(SELECT 
					t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
					FROM 
						pmsi.t_listes_contenu, 
						pmsi.t_listes,
						pmsi.t_actes 
					WHERE 
						t_listes_contenu.liste_id = t_listes.oid
						AND t_listes_contenu.to_id = t_actes.oid
						AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
						AND t_listes.code::text || '|'::text || t_actes.code::text NOT IN (SELECT liste_code::text || '|'::text || to_code::text FROM w_dbsetup)
				);
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_lpp" label="Contenu des listes : LPP">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_lpp.code as to_code 
			FROM pmsi.t_listes
				JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid 
				JOIN pmsi.t_listes_tables ON t_listes_tables.oid = t_listes.table_id
				JOIN pmsi.t_lpp ON t_lpp.oid = t_listes_contenu.to_id
			WHERE 1=1
				AND t_listes.is_cti = '1' 
				AND t_listes.code <> ''
				AND t_listes_tables.name = 't_lpp'
			ORDER BY 1,2;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_lpp.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_lpp ON w_dbsetup.to_code = t_lpp.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_lpp.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
				(
				SELECT t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
				FROM pmsi.t_listes_contenu
					JOIN pmsi.t_listes ON t_listes.oid = t_listes_contenu.liste_id
					JOIN pmsi.t_lpp ON t_lpp.oid = t_listes_contenu.to_id
				WHERE 1=1
					AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
					AND t_listes.code || '|' || t_lpp.code NOT IN 
						(
						SELECT liste_code || '|' || to_code 
						FROM w_dbsetup
						)
				)
			;
			
        ]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_ucd" label="Contenu des listes : UCD">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_ucd.code as to_code 
			FROM pmsi.t_listes
				JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid 
				JOIN pmsi.t_listes_tables ON t_listes_tables.oid = t_listes.table_id
				JOIN pmsi.t_ucd ON t_ucd.oid = t_listes_contenu.to_id
			WHERE 1=1
				AND t_listes.is_cti = '1'
				AND t_listes.code <> '' 
				AND t_listes_tables.name = 't_ucd'
			ORDER BY 1,2
			; 
			
        ]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_ucd.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_ucd ON w_dbsetup.to_code = t_ucd.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_ucd.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
				(
				SELECT t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
				FROM pmsi.t_listes_contenu
					JOIN pmsi.t_listes ON t_listes.oid = t_listes_contenu.liste_id
					JOIN pmsi.t_ucd ON t_ucd.oid = t_listes_contenu.to_id
				WHERE 1=1
					AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
					AND t_listes.code::text || '|'::text || t_ucd.code::text NOT IN 
						(
						SELECT liste_code::text || '|'::text || to_code::text 
						FROM w_dbsetup
						)
				)
			;
			
        ]]></updateCmd>
	</TABLE>

	<TABLE name="t_listes_contenu_prestation" label="Contenu des listes : Prestations">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.code as liste_code, 
				t_prestations.code as to_code 
			FROM pmsi.t_listes
				JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid 
				JOIN pmsi.t_listes_tables ON t_listes_tables.oid = t_listes.table_id
				JOIN pmsi.t_prestations ON t_prestations.oid = t_listes_contenu.to_id
			WHERE 1=1
				AND t_listes.is_cti = '1'
				AND t_listes.code <> '' 
				AND t_listes_tables.name = 't_prestations'
			ORDER BY 1,2
			; 
			
            ]]>
		</selectCmd>
		<updateCmd><![CDATA[ 
		
			INSERT INTO pmsi.t_listes_contenu (liste_id, to_id)
			SELECT
				t_listes.oid, 
				t_prestations.oid
			FROM w_dbsetup
			JOIN pmsi.t_listes ON w_dbsetup.liste_code = t_listes.code
			JOIN pmsi.t_prestations ON w_dbsetup.to_code = t_prestations.code
			LEFT JOIN pmsi.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND t_prestations.oid = t_listes_contenu.to_id
			WHERE t_listes_contenu.liste_id IS NULL
			;
			
			
			DELETE FROM pmsi.t_listes_contenu 
			WHERE t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text IN
				(
				SELECT t_listes_contenu.liste_id::text || '|'::text || t_listes_contenu.to_id::text 
				FROM pmsi.t_listes_contenu
					JOIN pmsi.t_listes ON t_listes.oid = t_listes_contenu.liste_id
					JOIN pmsi.t_prestations ON t_prestations.oid = t_listes_contenu.to_id
				WHERE 1=1
					AND t_listes.code IN (SELECT DISTINCT liste_code FROM w_dbsetup)
					AND t_listes.code::text || '|'::text || t_prestations.code::text NOT IN 
						(
						SELECT liste_code::text || '|'::text || to_code::text 
						FROM w_dbsetup
						)
				)
			;
			
        ]]></updateCmd>
	</TABLE>

	<!--
    <TABLE name="t_classes_base" label="Classifications BASE" schema="base">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes.*,
				t_listes_tables.name as table_name 
			FROM 
				base.t_classes,
				base.t_listes_tables 
			WHERE 
				(is_cti = '1' OR t_classes.code like 'CTI_%')
				AND table_id = t_listes_tables.oid 
			ORDER BY t_classes.code; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE base.t_classes SET 
				code = w_dbsetup.code, 
				texte = w_dbsetup.texte, 
				table_id = t_listes_tables.oid,
				is_cti = w_dbsetup.is_cti
			FROM 
				w_dbsetup, 
				base.t_listes_tables
			WHERE 
				w_dbsetup.table_name = t_listes_tables.name 
				AND t_listes.sequence = w_dbsetup.sequence;
			
			
			INSERT INTO base.t_classes(code, texte, table_id, is_cti, sequence)     
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				t_listes_tables.oid, 
				w_dbsetup.is_cti, 
				w_dbsetup.sequence      
			FROM 
				w_dbsetup, 
				base.t_listes_tables       
			WHERE 
				w_dbsetup.table_name = t_listes_tables.name 
				AND t_listes_tables.oid::text || '-'::text || w_dbsetup.sequence::text NOT IN (SELECT table_id::text || '-'::text || sequence::text FROM base.classes) AND w_dbsetup.texte NOT LIKE '*SUPPRIME%';
			
		]]></updateCmd>
	</TABLE>
    -->

	<TABLE name="t_classes" label="Classifications PMSI">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_classes.oid = 0 THEN t_classes.oid ELSE 1 END AS oid,
				t_classes.code,
				t_classes.texte,
				t_classes.is_cti,
				t_classes.sequence, 
				t_listes_tables.code as table_code,
				t_classes.contenu_by_code
			FROM 
				pmsi.t_classes, 
				pmsi.t_listes_tables 
			WHERE  
				(is_cti = '1' OR t_classes.code ilike 'CTI_%')
				AND table_id = t_listes_tables.oid 
			ORDER BY t_classes.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			DELETE FROM pmsi.t_classes 
			WHERE 
				oid NOT IN (
					SELECT t_classes.oid 
					FROM 
						w_dbsetup, 
						pmsi.t_listes_tables, 
						pmsi.t_classes
					WHERE 
						w_dbsetup.table_code = t_listes_tables.code 
						AND t_classes.sequence = w_dbsetup.sequence 
						AND t_classes.table_id = t_listes_tables.oid 
				)
				AND is_cti = '1';  
			
			UPDATE pmsi.t_classes SET
				code = w_dbsetup.code,       
				texte = w_dbsetup.texte,       
				table_id = t_listes_tables.oid,      
				is_cti = w_dbsetup.is_cti,
				contenu_by_code = w_dbsetup.contenu_by_code
			FROM 
				w_dbsetup,
				pmsi.t_listes_tables     
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code 
				AND t_classes.sequence = w_dbsetup.sequence 
				AND t_classes.table_id = t_listes_tables.oid ;     
			
			
			INSERT INTO pmsi.t_classes(code, texte, table_id, is_cti, sequence, contenu_by_code)     
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				t_listes_tables.oid, 
				w_dbsetup.is_cti, 
				w_dbsetup.sequence,
				w_dbsetup.contenu_by_code
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables       
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code 
				AND t_listes_tables.oid::text || '-'::text || w_dbsetup.sequence::text NOT IN (SELECT table_id::text || '-'::text || sequence::text FROM pmsi.t_classes) AND w_dbsetup.texte NOT LIKE '*SUPPRIME%';
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections" label="Classifications PMSI (sections)">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_classes_sections.oid = 0 THEN t_classes_sections.oid ELSE 1 END AS oid,
				t_classes_sections.code,
				t_classes_sections.texte,
				t_classes_sections.condition, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence,
				t_classes_sections.include_code,
				t_classes_sections.exclude_code 
			FROM 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables 
			WHERE 1=1
				AND t_classes_sections.classe_id = t_classes.oid 
				AND t_classes.is_cti = '1' 
				AND table_id = t_listes_tables.oid
			ORDER BY 
				t_listes_tables.code, 
				t_classes.sequence, 
				t_classes_sections.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[

			-- Ajustement des codes à deux caractères pour CTI_RAFA_P
			UPDATE pmsi.t_classes_sections
			SET code = substr(t_classes_sections.code,1,2)
			FROM pmsi.t_classes
			WHERE t_classes_sections.classe_id = t_classes.oid AND
				t_classes.code = 'CTI_RAFA_P' AND
				length(t_classes_sections.code) > 2
			;

			-- Sections à supprimer -> Sections CTI non-livrées
			DROP TABLE IF EXISTS w_del_sections
			;
			CREATE TEMP TABLE w_del_sections as (
				SELECT oid FROM pmsi.t_classes_sections 
				WHERE classe_id IN (
					SELECT
						t_classes.oid
					FROM pmsi.t_classes
					LEFT JOIN w_dbsetup ON w_dbsetup.code = t_classes_sections.code
					WHERE 1=1
						AND is_cti = '1'
						AND w_dbsetup.code IS NULL
				)
			);
			
			DELETE FROM pmsi.t_classes_sections_elements 
			WHERE section_id IN (SELECT oid FROM w_del_sections);
			
			DELETE FROM pmsi.t_classes_sections 
			WHERE oid IN (SELECT oid FROM w_del_sections);
			
			UPDATE pmsi.t_classes_sections SET
				texte = w_dbsetup.texte,
				condition = w_dbsetup.condition,
				exclude_code = w_dbsetup.exclude_code,
				include_code = w_dbsetup.include_code
			FROM w_dbsetup, pmsi.t_classes, pmsi.t_listes_tables
			WHERE t_classes.oid = t_classes_sections.classe_id
            AND t_listes_tables.oid = t_classes.table_id
			AND w_dbsetup.code||'@'||w_dbsetup.classe_sequence||'@'||w_dbsetup.table_code = t_classes_sections.code||'@'||t_classes.sequence||'@'||t_listes_tables.code
			AND (
				t_classes_sections.texte IS DISTINCT FROM w_dbsetup.texte
				OR t_classes_sections.condition IS DISTINCT FROM w_dbsetup.condition
				OR t_classes_sections.exclude_code IS DISTINCT FROM w_dbsetup.exclude_code
				OR t_classes_sections.include_code IS DISTINCT FROM w_dbsetup.include_code
			);
			
			INSERT INTO pmsi.t_classes_sections(code, texte, condition, classe_id, include_code, exclude_code)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.condition, 
				t_classes.oid,
				w_dbsetup.include_code,
				w_dbsetup.exclude_code
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code 
				AND t_classes.table_id = t_listes_tables.oid   
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.code||'@'||w_dbsetup.classe_sequence||'@'||w_dbsetup.table_code
                    NOT IN (
                        SELECT t_classes_sections.code||'@'||t_classes.sequence||'@'||t_listes_tables.code
                        FROM pmsi.t_classes_sections
                            JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
                            JOIN pmsi.t_listes_tables ON t_listes_tables.oid = t_classes.table_id)
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_codes_postaux" label="Classifications PMSI (codes_postaux)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_codes_postaux.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				base.t_codes_postaux
			WHERE 1=1
				AND 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 = 'CODEPOST'
				AND t_classes_sections_elements.to_id = t_codes_postaux.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_codes_postaux.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_codes_postaux.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				base.t_codes_postaux   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_codes_postaux.code
				AND t_classes_sections.oid::text || '-' || t_codes_postaux.oid::text NOT IN (SELECT section_id::text || '-' || to_id::text FROM pmsi.t_classes_sections_elements);
			
			SELECT * FROM pmsi.cti_update_schema_classes('CODEPOST');
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_ages" label="Classifications PMSI (ages)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_ages.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				base.t_ages
			WHERE 1=1
				AND 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 = 'AGE'
				AND t_classes_sections_elements.to_id = t_ages.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_ages.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_ages.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				base.t_ages   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_ages.code
				AND t_classes_sections.oid::text || '-'::text || t_ages.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM pmsi.t_classes_sections_elements);
			
			SELECT * FROM pmsi.cti_update_schema_classes('AGE');
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_controle_expert" label="Classifications PMSI (Controles expert)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_expert_controle.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				pmsi.t_expert_controle
			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 = 'EXPERTCTL'
				AND t_classes_sections_elements.to_id = t_expert_controle.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_expert_controle.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_expert_controle.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				pmsi.t_expert_controle   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_expert_controle.code
				AND t_classes_sections.oid::text || '-' || t_expert_controle.oid::text NOT IN (SELECT section_id::text || '-' || to_id::text FROM pmsi.t_classes_sections_elements);
			
			SELECT * FROM pmsi.cti_update_schema_classes('EXPERTCTL');
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_prestations" label="Classifications PMSI (prestations)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_prestations.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				pmsi.t_prestations
			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 = 'PRESTA'
				AND t_classes_sections_elements.to_id = t_prestations.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_prestations.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_prestations.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				pmsi.t_prestations   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_prestations.code
				AND t_classes_sections.oid::text || '-'::text || t_prestations.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM pmsi.t_classes_sections_elements);
			
			SELECT * FROM pmsi.cti_update_schema_classes('PRESTA');
			
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_classes_sections_elements_types_facturation" label="Classifications PMSI (Types de facturation)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_prestations.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				pmsi.t_prestations
			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 = 'TYPEFACT'
				AND t_classes_sections_elements.to_id = t_prestations.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_prestations.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			-- Supprime la classe de type de facturation qui a été créée sur la séquence 9 (elle sera remplacée par la séquence 7) _ by CLA 15-07-2014
			
			DELETE FROM pmsi.t_classes_sections_elements
			USING pmsi.t_classes_sections cs
				JOIN pmsi.t_classes c ON c.oid = cs.classe_id
				JOIN pmsi.t_listes_tables lt ON lt.oid = c.table_id
			WHERE
				1=1
				AND lt.code = 'TYPEFACT'
                AND c.is_cti = '1'
				AND c.sequence = 9
				AND t_classes_sections_elements.section_id = cs.oid;


			DELETE FROM pmsi.t_classes_sections
			USING pmsi.t_classes c
				JOIN pmsi.t_listes_tables lt ON lt.oid = c.table_id
			WHERE 1=1
				AND lt.code = 'TYPEFACT'
				AND c.sequence = 9
				AND pmsi.t_classes_sections.classe_id = c.oid;

			DELETE FROM pmsi.t_classes
			USING pmsi.t_listes_tables lt
			WHERE 1=1
				AND lt.oid = t_classes.table_id
				AND lt.code = 'TYPEFACT'
                AND t_classes.is_cti = '1'
				AND t_classes.sequence = 9;
				
				
			-- Insertion des classes de type de facturation
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_prestations.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				pmsi.t_prestations   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_prestations.code
				AND t_classes_sections.oid::text || '-'::text || t_prestations.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM pmsi.t_classes_sections_elements);
			
			SELECT * FROM pmsi.cti_update_schema_classes('TYPEFACT');
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_actes" label="Classifications PMSI (actes)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.code AS section_code, 
				t_actes.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				pmsi.t_actes
			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 = 'ACTE'
				AND t_classes_sections_elements.to_id = t_actes.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_actes.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_actes.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				pmsi.t_actes   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_actes.code
				AND t_classes_sections.oid::text || '-'::text || t_actes.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM pmsi.t_classes_sections_elements);
			
			
			SELECT * FROM pmsi.cti_update_schema_classes('ACTE');
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_diagnostics" label="Classifications PMSI (diagnostics)">
		<selectCmd>
			<![CDATA[
			SELECT
				t_classes_sections.code AS section_code, 
				t_diagnostics.code as to_code, 
				t_diagnostics.texte as to_texte,
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				pmsi.t_diagnostics
			WHERE 1=1
				AND 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 = 'DIAG'
				AND t_classes_sections_elements.to_id = t_diagnostics.oid
			ORDER BY 
				t_classes.sequence,
				t_classes_sections.code,
				t_diagnostics.code
			;
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
				SELECT t_classes_sections.oid, t_diagnostics.oid 
				FROM 
					w_dbsetup, 
					pmsi.t_listes_tables, 
					pmsi.t_classes, 
					pmsi.t_classes_sections, 
					pmsi.t_diagnostics   
				WHERE 1=1
					AND w_dbsetup.table_code = t_listes_tables.code       
					AND t_classes.table_id = t_listes_tables.oid   
					AND t_classes_sections.classe_id = t_classes.oid   
					AND w_dbsetup.section_code = t_classes_sections.code
					AND w_dbsetup.classe_sequence = t_classes.sequence
					AND w_dbsetup.to_code = t_diagnostics.code
					AND t_classes_sections.oid::text || '-' || t_diagnostics.oid::text NOT IN 
						(
						SELECT section_id::text || '-' || to_id::text 
						FROM pmsi.t_classes_sections_elements
						)
			;
						SELECT * FROM pmsi.cti_update_schema_classes('DIAG'); 

			]]>
		</updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_ghm" label="Classifications PMSI (GHM)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.code AS section_code, 
				t_ghm.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections,
				pmsi.t_classes, 
				pmsi.t_listes_tables,
				pmsi.t_ghm
			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 = 'GHM'
				AND t_classes_sections_elements.to_id = t_ghm.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_ghm.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT t_classes_sections.oid, t_ghm.oid 
			FROM w_dbsetup, pmsi.t_listes_tables, pmsi.t_classes, pmsi.t_classes_sections, pmsi.t_ghm   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_ghm.code
				AND t_classes_sections.oid::text || '-'::text || t_ghm.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM pmsi.t_classes_sections_elements);
			
			-- Suppression doublons
			DELETE FROM pmsi.t_classes_sections_elements
			USING
			(
			SELECT t_classes_sections_elements.section_id, t_classes_sections_elements.to_id
			FROM pmsi.t_classes_sections_elements
			JOIN (
				SELECT t_classes_sections.oid AS section_id
				FROM w_dbsetup, pmsi.t_listes_tables, pmsi.t_classes, pmsi.t_classes_sections
				WHERE 
					w_dbsetup.table_code = t_listes_tables.code       
					AND t_classes.table_id = t_listes_tables.oid   
					AND t_classes_sections.classe_id = t_classes.oid   
					AND w_dbsetup.section_code = t_classes_sections.code
					AND w_dbsetup.classe_sequence = t_classes.sequence
				GROUP BY 1
				) subview1 ON t_classes_sections_elements.section_id = subview1.section_id
			LEFT JOIN 
				(
				SELECT t_classes_sections.oid AS section_id, t_ghm.oid AS to_id
				FROM w_dbsetup, pmsi.t_listes_tables, pmsi.t_classes, pmsi.t_classes_sections, pmsi.t_ghm   
				WHERE 
					w_dbsetup.table_code = t_listes_tables.code       
					AND t_classes.table_id = t_listes_tables.oid   
					AND t_classes_sections.classe_id = t_classes.oid   
					AND w_dbsetup.section_code = t_classes_sections.code
					AND w_dbsetup.classe_sequence = t_classes.sequence
					AND w_dbsetup.to_code = t_ghm.code
				) subview2 ON t_classes_sections_elements.section_id = subview2.section_id AND
					t_classes_sections_elements.to_id = subview2.to_id
			WHERE subview2.to_id IS NULL
			) subview 
			WHERE t_classes_sections_elements.section_id = subview.section_id AND
					t_classes_sections_elements.to_id = subview.to_id;
			
			SELECT * FROM pmsi.cti_update_schema_classes('GHM');
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_lpp" label="Classifications PMSI (LPP)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.code AS section_code, 
				t_lpp.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements
				JOIN pmsi.t_classes_sections ON t_classes_sections_elements.section_id = t_classes_sections.oid
				JOIN pmsi.t_classes ON t_classes_sections.classe_id = t_classes.oid
				JOIN pmsi.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
				JOIN pmsi.t_lpp ON t_classes_sections_elements.to_id = t_lpp.oid
			WHERE 
				t_classes.is_cti = '1'
				AND t_listes_tables.code = 'LPP'
			ORDER BY t_classes.sequence, t_classes_sections.code, t_lpp.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT t_classes_sections.oid, t_lpp.oid 
			FROM
				w_dbsetup
				JOIN pmsi.t_listes_tables ON w_dbsetup.table_code = t_listes_tables.code
				JOIN pmsi.t_classes ON t_classes.table_id = t_listes_tables.oid
				JOIN pmsi.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
				JOIN pmsi.t_lpp ON w_dbsetup.to_code = t_lpp.code
			WHERE
			    w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND NOT EXISTS (
					SELECT 1
					FROM pmsi.t_classes_sections_elements
					WHERE t_classes_sections.oid::text || '-'::text || t_lpp.oid::text = t_classes_sections_elements.section_id::text || '-'::text || t_classes_sections_elements.to_id::text
				)
				;
			
			
			SELECT * FROM pmsi.cti_update_schema_classes('LPP');
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_ucd" label="Classifications PMSI (UCD)">
		<selectCmd>
			<![CDATA[
			SELECT
				t_classes_sections.code AS section_code, 
				t_ucd.code as to_code, 
				t_ucd.texte as to_texte,
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements
				JOIN pmsi.t_classes_sections ON t_classes_sections_elements.section_id = t_classes_sections.oid
				JOIN pmsi.t_classes ON t_classes_sections.classe_id = t_classes.oid
				JOIN pmsi.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
				JOIN pmsi.t_ucd ON t_classes_sections_elements.to_id = t_ucd.oid
			WHERE 1=1
				AND t_classes.is_cti = '1'
				AND t_listes_tables.code = 'UCD'
			ORDER BY 
				t_classes.sequence,
				t_classes_sections.code,
				t_ucd.code
			;
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
				SELECT t_classes_sections.oid, t_ucd.oid 
				FROM 
					w_dbsetup, 
					pmsi.t_listes_tables, 
					pmsi.t_classes, 
					pmsi.t_classes_sections, 
					pmsi.t_ucd   
				WHERE 1=1
					AND w_dbsetup.table_code = t_listes_tables.code       
					AND t_classes.table_id = t_listes_tables.oid   
					AND t_classes_sections.classe_id = t_classes.oid   
					AND w_dbsetup.section_code = t_classes_sections.code
					AND w_dbsetup.classe_sequence = t_classes.sequence
					AND w_dbsetup.to_code = t_ucd.code
					AND t_classes_sections.oid::text || '-' || t_ucd.oid::text NOT IN 
						(
						SELECT section_id::text || '-' || to_id::text 
						FROM pmsi.t_classes_sections_elements
						)
			;
						SELECT * FROM pmsi.cti_update_schema_classes('UCD'); 

			]]>
		</updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_indication_ucd" label="Indication UCD">
		<selectCmd>
			<![CDATA[
			SELECT
				t_classes_sections.code AS section_code, 
				t_indication.code as to_code, 
				t_indication.texte as to_texte,
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections, 
				pmsi.t_classes, 
				pmsi.t_listes_tables, 
				pmsi.t_indication
			WHERE 1=1
				AND 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 = 'INDICATIONUCD'
				AND t_classes_sections_elements.to_id = t_indication.oid
			ORDER BY 
				t_classes.sequence,
				t_classes_sections.code,
				t_indication.code
			;
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
				SELECT t_classes_sections.oid, t_indication.oid 
				FROM 
					w_dbsetup, 
					pmsi.t_listes_tables, 
					pmsi.t_classes, 
					pmsi.t_classes_sections, 
					pmsi.t_indication   
				WHERE 1=1
					AND w_dbsetup.table_code = t_listes_tables.code       
					AND t_classes.table_id = t_listes_tables.oid   
					AND t_classes_sections.classe_id = t_classes.oid   
					AND w_dbsetup.section_code = t_classes_sections.code
					AND w_dbsetup.classe_sequence = t_classes.sequence
					AND w_dbsetup.to_code = t_indication.code
					AND t_classes_sections.oid::text || '-' || t_indication.oid::text NOT IN 
						(
						SELECT section_id::text || '-' || to_id::text 
						FROM pmsi.t_classes_sections_elements
						)
			;
						SELECT * FROM pmsi.cti_update_schema_classes('INDICATIONUCD'); 

			]]>
		</updateCmd>
	</TABLE>

	<TABLE name="t_classes_sections_elements_duree_sejour" label="Classifications PMSI (durees_sejour)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.code AS section_code, 
				t_durees_sejour.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				pmsi.t_classes_sections_elements, 
				pmsi.t_classes_sections,
				pmsi.t_classes, 
				pmsi.t_listes_tables,
				base.t_durees_sejour
			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 = 'DURSEJ'
				AND t_classes_sections_elements.to_id = t_durees_sejour.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_durees_sejour.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_durees_sejour.oid 
			FROM 
				w_dbsetup, 
				pmsi.t_listes_tables, 
				pmsi.t_classes, 
				pmsi.t_classes_sections, 
				base.t_durees_sejour   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_durees_sejour.code
				AND t_classes_sections.oid::text || '-'::text || t_durees_sejour.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM pmsi.t_classes_sections_elements);
			
			
			SELECT * FROM pmsi.cti_update_schema_classes('DURSEJ');
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_indicateurs" label="Indicateurs de rapports">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN t_indicateurs.oid = 0 THEN t_indicateurs.oid ELSE 1 END AS oid,
				t_indicateurs.texte,
				t_indicateurs.table_name,
				t_indicateurs.column_name,
				t_indicateurs.is_cti,
				t_indicateurs.entete,
				t_indicateurs.code,
				t_indicateurs.total_function,
				t_indicateurs.output_format,
				t_indicateurs.width,
				t_indicateurs.entete_etendue,
				t_indicateurs.dimension_date_1,
				t_indicateurs.dimension_date_2,
				t_indicateurs.dimension_date_3,
				t_indicateurs.dimension_date_4,
				t_indicateurs.dimension_date_5,
				t_indicateurs.dimension_date,
				t_indicateurs_1.code as indicateur_associe_1_code, 
				t_indicateurs_2.code as indicateur_associe_2_code, 
				t_indicateurs_3.code as indicateur_associe_3_code, 
				t_indicateurs_4.code as indicateur_associe_4_code, 
				t_indicateurs_5.code as indicateur_associe_5_code, 
				t_indicateurs_6.code as indicateur_associe_6_code, 
				t_indicateurs_7.code as indicateur_associe_7_code, 
				t_indicateurs_8.code as indicateur_associe_8_code, 
				t_indicateurs_9.code as indicateur_associe_9_code, 
				t_indicateurs_10.code as indicateur_associe_10_code, 
				t_indicateurs_11.code as indicateur_associe_11_code, 
				t_indicateurs_12.code as indicateur_associe_12_code, 
				t_indicateurs_13.code as indicateur_associe_13_code, 
				t_indicateurs_14.code as indicateur_associe_14_code, 
				t_indicateurs_15.code as indicateur_associe_15_code, 
				t_indicateurs_16.code as indicateur_associe_16_code, 
				t_indicateurs_17.code as indicateur_associe_17_code, 
				t_indicateurs_18.code as indicateur_associe_18_code, 
				t_indicateurs_19.code as indicateur_associe_19_code, 
				t_indicateurs_20.code as indicateur_associe_20_code,
				t_indicateurs_reference.code as indicateur_reference_code
			FROM 
				pmsi.t_indicateurs 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_1 ON (t_indicateurs.indicateur_associe_1_id = t_indicateurs_1.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_2 ON (t_indicateurs.indicateur_associe_2_id = t_indicateurs_2.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_3 ON (t_indicateurs.indicateur_associe_3_id = t_indicateurs_3.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_4 ON (t_indicateurs.indicateur_associe_4_id = t_indicateurs_4.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_5 ON (t_indicateurs.indicateur_associe_5_id = t_indicateurs_5.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_6 ON (t_indicateurs.indicateur_associe_6_id = t_indicateurs_6.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_7 ON (t_indicateurs.indicateur_associe_7_id = t_indicateurs_7.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_8 ON (t_indicateurs.indicateur_associe_8_id = t_indicateurs_8.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_9 ON (t_indicateurs.indicateur_associe_9_id = t_indicateurs_9.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_10 ON (t_indicateurs.indicateur_associe_10_id = t_indicateurs_10.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_11 ON (t_indicateurs.indicateur_associe_11_id = t_indicateurs_11.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_12 ON (t_indicateurs.indicateur_associe_12_id = t_indicateurs_12.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_13 ON (t_indicateurs.indicateur_associe_13_id = t_indicateurs_13.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_14 ON (t_indicateurs.indicateur_associe_14_id = t_indicateurs_14.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_15 ON (t_indicateurs.indicateur_associe_15_id = t_indicateurs_15.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_16 ON (t_indicateurs.indicateur_associe_16_id = t_indicateurs_16.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_17 ON (t_indicateurs.indicateur_associe_17_id = t_indicateurs_17.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_18 ON (t_indicateurs.indicateur_associe_18_id = t_indicateurs_18.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_19 ON (t_indicateurs.indicateur_associe_19_id = t_indicateurs_19.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_20 ON (t_indicateurs.indicateur_associe_20_id = t_indicateurs_20.oid) 
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_reference ON (t_indicateurs.indicateur_reference_id = t_indicateurs_reference.oid) 
			WHERE 
				t_indicateurs.is_cti = '1'
			ORDER BY t_indicateurs.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			DELETE FROM pmsi.t_indicateurs 
			USING
				(
				SELECT code, MIN(oid) AS minoid FROM pmsi.t_indicateurs WHERE code <> '' GROUP BY 1 HAVING count(*) > 1
				) subview 
			WHERE 
				t_indicateurs.code = subview.code 
				AND t_indicateurs.oid <> subview.minoid;
			
			
			UPDATE pmsi.t_indicateurs SET 
				texte = w_dbsetup.texte, 
				table_name = w_dbsetup.table_name,
				column_name = w_dbsetup.column_name,
				is_cti = w_dbsetup.is_cti, 
				entete = w_dbsetup.entete,
				entete_etendue = w_dbsetup.entete_etendue,
				total_function = w_dbsetup.total_function,
				output_format = w_dbsetup.output_format,
				width = w_dbsetup.width,
				dimension_date_1 = w_dbsetup.dimension_date_1,
				dimension_date_2 = w_dbsetup.dimension_date_2,
				dimension_date_3 = w_dbsetup.dimension_date_3,
				dimension_date_4 = w_dbsetup.dimension_date_4,
				dimension_date_5 = w_dbsetup.dimension_date_5,
				dimension_date = w_dbsetup.dimension_date	
			FROM w_dbsetup
			WHERE t_indicateurs.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_indicateurs(
				texte, 
				table_name, 
				column_name, 
				is_cti, 
				entete, 
				entete_etendue, 
				code, 
				total_function, 
				output_format, width,
				dimension_date_1, 
				dimension_date_2, 
				dimension_date_3, 
				dimension_date_4, 
				dimension_date_5, 
				dimension_date)
			SELECT 
				texte, 
				table_name, 
				column_name, 
				is_cti, 
				entete, 
				entete_etendue, 
				code, 
				total_function, 
				output_format, 
				width,
				dimension_date_1, 
				dimension_date_2, 
				dimension_date_3, 
				dimension_date_4, 
				dimension_date_5, 
				dimension_date 
			FROM 
				w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_indicateurs WHERE code IS NOT NULL);
			
			
			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_1_id = COALESCE(t_indicateurs_1.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_1 ON (w_dbsetup.indicateur_associe_1_code = t_indicateurs_1.code AND w_dbsetup.indicateur_associe_1_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_1_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_2_id = COALESCE(t_indicateurs_2.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_2 ON (w_dbsetup.indicateur_associe_2_code = t_indicateurs_2.code AND w_dbsetup.indicateur_associe_2_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_2_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_3_id = COALESCE(t_indicateurs_3.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_3 ON (w_dbsetup.indicateur_associe_3_code = t_indicateurs_3.code AND w_dbsetup.indicateur_associe_3_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_3_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_4_id = COALESCE(t_indicateurs_4.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_4 ON (w_dbsetup.indicateur_associe_4_code = t_indicateurs_4.code AND w_dbsetup.indicateur_associe_4_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_4_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_5_id = COALESCE(t_indicateurs_5.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_5 ON (w_dbsetup.indicateur_associe_5_code = t_indicateurs_5.code AND w_dbsetup.indicateur_associe_5_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_5_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_6_id = COALESCE(t_indicateurs_6.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_6 ON (w_dbsetup.indicateur_associe_6_code = t_indicateurs_6.code AND w_dbsetup.indicateur_associe_6_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_6_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_7_id = COALESCE(t_indicateurs_7.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_7 ON (w_dbsetup.indicateur_associe_7_code = t_indicateurs_7.code AND w_dbsetup.indicateur_associe_7_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_7_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_8_id = COALESCE(t_indicateurs_8.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_8 ON (w_dbsetup.indicateur_associe_8_code = t_indicateurs_8.code AND w_dbsetup.indicateur_associe_8_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_8_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_9_id = COALESCE(t_indicateurs_9.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_9 ON (w_dbsetup.indicateur_associe_9_code = t_indicateurs_9.code AND w_dbsetup.indicateur_associe_9_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_9_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_10_id = COALESCE(t_indicateurs_10.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_10 ON (w_dbsetup.indicateur_associe_10_code = t_indicateurs_10.code AND w_dbsetup.indicateur_associe_10_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_10_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_11_id = COALESCE(t_indicateurs_11.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_11 ON (w_dbsetup.indicateur_associe_11_code = t_indicateurs_11.code AND w_dbsetup.indicateur_associe_11_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_11_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_12_id = COALESCE(t_indicateurs_12.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_12 ON (w_dbsetup.indicateur_associe_12_code = t_indicateurs_12.code AND w_dbsetup.indicateur_associe_12_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_12_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_13_id = COALESCE(t_indicateurs_13.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_13 ON (w_dbsetup.indicateur_associe_13_code = t_indicateurs_13.code AND w_dbsetup.indicateur_associe_13_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_13_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_14_id = COALESCE(t_indicateurs_14.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_14 ON (w_dbsetup.indicateur_associe_14_code = t_indicateurs_14.code AND w_dbsetup.indicateur_associe_14_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_14_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_15_id = COALESCE(t_indicateurs_15.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_15 ON (w_dbsetup.indicateur_associe_15_code = t_indicateurs_15.code AND w_dbsetup.indicateur_associe_15_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_15_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_16_id = COALESCE(t_indicateurs_16.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_16 ON (w_dbsetup.indicateur_associe_16_code = t_indicateurs_16.code AND w_dbsetup.indicateur_associe_16_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_16_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_17_id = COALESCE(t_indicateurs_17.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_17 ON (w_dbsetup.indicateur_associe_17_code = t_indicateurs_17.code AND w_dbsetup.indicateur_associe_17_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_17_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_18_id = COALESCE(t_indicateurs_18.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_18 ON (w_dbsetup.indicateur_associe_18_code = t_indicateurs_18.code AND w_dbsetup.indicateur_associe_18_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_18_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_19_id = COALESCE(t_indicateurs_19.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_19 ON (w_dbsetup.indicateur_associe_19_code = t_indicateurs_19.code AND w_dbsetup.indicateur_associe_19_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_19_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_associe_20_id = COALESCE(t_indicateurs_20.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_20 ON (w_dbsetup.indicateur_associe_20_code = t_indicateurs_20.code AND w_dbsetup.indicateur_associe_20_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_associe_20_code <> '';

			UPDATE pmsi.t_indicateurs SET 
				indicateur_reference_id = COALESCE(t_indicateurs_reference.oid,0)
			FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_reference ON (w_dbsetup.indicateur_reference_code = t_indicateurs_reference.code AND w_dbsetup.indicateur_reference_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND indicateur_reference_code <> '';
			
			
			SELECT base.cti_reorganize_indicateurs('pmsi');
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_indicateurs_atih" label="Indicateurs de rapports">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_indicateurs_atih.indicateur_id = 0 THEN t_indicateurs_atih.indicateur_id ELSE 1 END AS indicateur_id,
				t_indicateurs.code as indicateur_code, 
				t_indicateurs_atih.periode_type,
				t_indicateurs_atih.date_reference,
				t_indicateurs_atih.value_nat,
				t_indicateurs_atih.value_nat_prive,
				t_indicateurs_atih.value_nat_public,
				t_indicateurs_atih.value_nat_cum,
				t_indicateurs_atih.value_nat_cum_prive,
				t_indicateurs_atih.value_nat_cum_public
			FROM 
				pmsi.t_indicateurs_atih 
				JOIN pmsi.t_indicateurs ON (t_indicateurs_atih.indicateur_id = t_indicateurs.oid) 
			WHERE t_indicateurs_atih.indicateur_id <> 0
			ORDER BY 
				t_indicateurs.code, 
				t_indicateurs_atih.periode_type,
				t_indicateurs_atih.date_reference;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			TRUNCATE pmsi.t_indicateurs_atih;
			
			INSERT INTO pmsi.t_indicateurs_atih(
				indicateur_id,
				periode_type,
				date_reference,
				value_nat,
				value_nat_prive,
				value_nat_public,
				value_nat_cum,
				value_nat_cum_prive,
				value_nat_cum_public)
			SELECT 
				t_indicateurs.oid AS indicateur_id,
				periode_type,
				date_reference,
				value_nat,
				value_nat_prive,
				value_nat_public,
				value_nat_cum,
				value_nat_cum_prive,
				value_nat_cum_public
			FROM 
				w_dbsetup
				JOIN pmsi.t_indicateurs ON (w_dbsetup.indicateur_code = t_indicateurs.code AND t_indicateurs.code <> '') ;
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_rapports" label="Rapports">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_rapports.oid = 0 THEN t_rapports.oid ELSE 1 END AS oid,
				t_rapports.code, 
				t_rapports.texte, 
				t_rapports.is_cti, 
				t_rapports.is_essentiel, 
				t_rapports.essentiel_date_level, 
				t_rapports.colonnes_essentiel, 
				t_rapports.entete, 
				t_rapports.entete_2, 
				CASE WHEN t_rapports.group_id = 0 THEN t_rapports.group_id ELSE 1 END AS group_id,
				t_rapports.is_hide, 
				array_to_string(
					ARRAY[
					COALESCE(t_indicateur_condition_1.external_code::text,''),
					COALESCE(t_indicateur_condition_2.external_code::text,''),
					COALESCE(t_indicateur_condition_3.external_code::text,''),
					COALESCE(t_indicateur_condition_4.external_code::text,'')
					]
					,'||') AS rapport_indicateur_condition_code, 
				t_rapports.label, 
				array_to_string(serie_entete,'||') AS serie_entete, 
				t_rapports.colonnes_essentiel_style
			FROM pmsi.t_rapports 
			LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_1 ON t_rapports.rapport_indicateur_condition_id[1] = t_indicateur_condition_1.oid
			LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_2 ON t_rapports.rapport_indicateur_condition_id[2] = t_indicateur_condition_2.oid
			LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_3 ON t_rapports.rapport_indicateur_condition_id[3] = t_indicateur_condition_3.oid
			LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_4 ON t_rapports.rapport_indicateur_condition_id[4] = t_indicateur_condition_4.oid
			WHERE (t_rapports.is_cti = '1' OR t_rapports.code LIKE 'CTI_%') AND
                t_rapports.code <> 'CTI_PORTFO' AND
				t_rapports.code <> 'CTI_ESS_4'
			ORDER BY t_rapports.code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			
			ALTER TABLE w_dbsetup ADD COLUMN to_serie_entete text[];
			UPDATE w_dbsetup SET to_serie_entete = string_to_array(serie_entete,'||');
			
			
			ALTER TABLE w_dbsetup ADD COLUMN to_rapport_indicateur_condition_code text[];
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_code = string_to_array(rapport_indicateur_condition_code,'||');
			
			ALTER TABLE w_dbsetup ADD COLUMN to_rapport_indicateur_condition_id bigint[];
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[1] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[1] AND to_rapport_indicateur_condition_code[1] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[2] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[2] AND to_rapport_indicateur_condition_code[2] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[3] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[3] AND to_rapport_indicateur_condition_code[3] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[4] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[4] AND to_rapport_indicateur_condition_code[4] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id = array_remove(to_rapport_indicateur_condition_id,NULL);
			
			UPDATE pmsi.t_rapports SET 
				texte = w_dbsetup.texte, 
				is_cti = w_dbsetup.is_cti,
				is_essentiel = w_dbsetup.is_essentiel,
				essentiel_date_level = w_dbsetup.essentiel_date_level,
				colonnes_essentiel = w_dbsetup.colonnes_essentiel,
				entete = w_dbsetup.entete,
				entete_2 = w_dbsetup.entete_2,
				group_id  = w_dbsetup.group_id,
				rapport_indicateur_condition_id = to_rapport_indicateur_condition_id,
				label  = w_dbsetup.label,
				serie_entete  = to_serie_entete,
				colonnes_essentiel_style = w_dbsetup.colonnes_essentiel_style
			FROM w_dbsetup
			WHERE t_rapports.code = w_dbsetup.code;
			
			
			INSERT INTO pmsi.t_rapports(oid, code, texte, is_cti, is_essentiel)
			SELECT oid, code, texte, is_cti, is_essentiel 
			FROM w_dbsetup 
			WHERE 
				oid = 0 AND
				oid NOT IN (SELECT oid FROM pmsi.t_rapports);
			
			
			INSERT INTO pmsi.t_rapports(
				code, 
				texte, 
				is_cti, 
				is_essentiel, 
				essentiel_date_level, 
				colonnes_essentiel, 
				entete, 
				entete_2, 
				group_id, 
				is_hide, 
				rapport_indicateur_condition_id, 
				label, 
				serie_entete, 
				colonnes_essentiel_style
				)
			SELECT 
				code, 
				texte, 
				is_cti, 
				is_essentiel, 
				essentiel_date_level, 
				colonnes_essentiel, 
				entete, 
				entete_2, 
				group_id, 
				is_hide, 
				to_rapport_indicateur_condition_id, 
				label, 
				to_serie_entete, 
				colonnes_essentiel_style
			FROM w_dbsetup 
			WHERE 
				oid <> 0 AND
				code NOT IN (SELECT code FROM pmsi.t_rapports);
				
			-- Suppression du rapport 4
			DELETE FROM pmsi.t_rapports WHERE code = 'CTI_ESS_4'
					
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_rapports_rubriques" label="Rubriques des rapports" >
		<selectCmd><![CDATA[ 
			
			SELECT
				t_rapports.code AS rapport_code, 
				t_rapports_rubriques.numero_rubrique,
				t_indicateurs.code AS indicateur_code,
				COALESCE(t_indicateurs_associes.code, '') as indicateur_associe_code,
				t_rapports_rubriques.entete,
				t_rapports_rubriques.entete_etendue,
				t_rapports_rubriques.output_format,
				t_rapports_rubriques.width,
				t_rapports_rubriques.row_height,
				t_rapports_rubriques.padding,
				t_rapports_rubriques.font_size,
				t_rapports_rubriques.font_weight,
				t_rapports_rubriques.border_thickness,
				t_rapports_rubriques.color,
				t_rapports_rubriques.background_color,
				t_rapports_rubriques.base_nationale,
				t_rapports_rubriques.base_nationale_qualifiee,
				t_rapports_rubriques.viewlink,
				t_rapports_rubriques.viewlink_label,
				replace(replace(t_rapports_rubriques.viewlink_param,chr(10),'#CHR10#'),chr(13),'#CHR10#') AS viewlink_param,
				t_rapports_rubriques.picto_enabled,
				t_rapports_rubriques.positif_is_good,
				replace(replace(t_rapports_rubriques.description,chr(10),'#CHR10#'),chr(13),'#CHR10#') AS description,
				replace(replace(t_rapports_rubriques.description_2,chr(10),'#CHR10#'),chr(13),'#CHR10#') AS description_2,
				t_rapports_rubriques.entete_etendue_2,
				t_rapports_rubriques.output_format_2,
				t_rapports_rubriques.picto_enabled_2,
				t_rapports_rubriques.positif_is_good_2,
				t_rapports_rubriques.is_hide,
				t_rapports_rubriques.specific_viewlink,
				t_rapports_rubriques.specific_viewlink_label,
				t_rapports_rubriques.specific_viewlink_param,
				array_to_string(
					ARRAY[
						COALESCE(t_indicateur_condition_1.external_code::text,''),
						COALESCE(t_indicateur_condition_2.external_code::text,''),
						COALESCE(t_indicateur_condition_3.external_code::text,''),
						COALESCE(t_indicateur_condition_4.external_code::text,'')
					],'||')::text AS indicateur_condition_external_code,
				t_rapports_rubriques.erase_rapport_indicateur_condition,
				t_rapports_rubriques.calc_formula,
				t_rapports_rubriques.hide_on_zero_field,
				t_rapports_rubriques.field_tag,
				array_to_string(
					ARRAY[
						COALESCE(t_indicateurs_serie_1.code,''),
						COALESCE(t_indicateurs_serie_2.code,''),
						COALESCE(t_indicateurs_serie_3.code,''),
						COALESCE(t_indicateurs_serie_4.code,''),
						COALESCE(t_indicateurs_serie_5.code,''),
						COALESCE(t_indicateurs_serie_6.code,''),
						COALESCE(t_indicateurs_serie_7.code,''),
						COALESCE(t_indicateurs_serie_8.code,''),
						COALESCE(t_indicateurs_serie_9.code,'')
					],'||')::text AS serie_indicateur_code,
				array_to_string(
					ARRAY[
						COALESCE(t_indicateurs_serie_1.table_name,''),
						COALESCE(t_indicateurs_serie_2.table_name,''),
						COALESCE(t_indicateurs_serie_3.table_name,''),
						COALESCE(t_indicateurs_serie_4.table_name,''),
						COALESCE(t_indicateurs_serie_5.table_name,''),
						COALESCE(t_indicateurs_serie_6.table_name,''),
						COALESCE(t_indicateurs_serie_7.table_name,''),
						COALESCE(t_indicateurs_serie_8.table_name,''),
						COALESCE(t_indicateurs_serie_9.table_name,'')
					],'||')::text  AS serie_indicateur_table_name,
				array_to_string(t_rapports_rubriques.serie_entete_etendue,'||')::text AS serie_entete_etendue,
				array_to_string(t_rapports_rubriques.serie_description,'||')::text AS serie_description,
				array_to_string(t_rapports_rubriques.serie_output_format,'||')::text AS serie_output_format,
				array_to_string(t_rapports_rubriques.serie_picto_enabled,'||')::text AS serie_picto_enabled,
				array_to_string(t_rapports_rubriques.serie_positif_is_good,'||')::text AS serie_positif_is_good
			FROM 
				pmsi.t_rapports_rubriques
				JOIN pmsi.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
				JOIN pmsi.t_indicateurs t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_associes ON t_rapports_rubriques.indicateur_associe_id = t_indicateurs_associes.oid AND
					t_indicateurs_associes.is_cti IS DISTINCT FROM '0'
				LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_1 ON t_rapports_rubriques.indicateur_condition_id[1] = t_indicateur_condition_1.oid
				LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_2 ON t_rapports_rubriques.indicateur_condition_id[2] = t_indicateur_condition_2.oid
				LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_3 ON t_rapports_rubriques.indicateur_condition_id[3] = t_indicateur_condition_3.oid
				LEFT JOIN pmsi.t_indicateur_condition t_indicateur_condition_4 ON t_rapports_rubriques.indicateur_condition_id[4] = t_indicateur_condition_4.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_1 ON t_rapports_rubriques.serie_indicateur_id[1] = t_indicateurs_serie_1.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_2 ON t_rapports_rubriques.serie_indicateur_id[2] = t_indicateurs_serie_2.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_3 ON t_rapports_rubriques.serie_indicateur_id[3] = t_indicateurs_serie_3.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_4 ON t_rapports_rubriques.serie_indicateur_id[4] = t_indicateurs_serie_4.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_5 ON t_rapports_rubriques.serie_indicateur_id[5] = t_indicateurs_serie_5.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_6 ON t_rapports_rubriques.serie_indicateur_id[6] = t_indicateurs_serie_6.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_7 ON t_rapports_rubriques.serie_indicateur_id[7] = t_indicateurs_serie_7.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_8 ON t_rapports_rubriques.serie_indicateur_id[8] = t_indicateurs_serie_8.oid
				LEFT JOIN pmsi.t_indicateurs t_indicateurs_serie_9 ON t_rapports_rubriques.serie_indicateur_id[9] = t_indicateurs_serie_9.oid
			WHERE 1=1
				AND t_rapports.is_cti = '1'
				AND t_rapports.code <> 'CTI_ESS_4'
				AND t_rapports.code <> 'CTI_PORTFO'
				AND t_indicateurs.is_cti = '1' 
				AND t_indicateurs.code <> ''
			ORDER BY 
				t_rapports.code, 
				t_rapports_rubriques.numero_rubrique, 
				t_indicateurs.code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[

            -- Initialiser indicateurs condition
            SELECT pmsi.cti_reorganize_indicateur_condition();

            -- Sauvegarder les données sources pour rejouer le spécifique
            SELECT base.cti_verify_table_encoding('pmsi','t_rapports_rubriques')
            ;
            DROP TABLE IF EXISTS w_specific_rapports_rubriques ;
            CREATE TEMP TABLE w_specific_rapports_rubriques AS
            SELECT t_rapports_rubriques.* 
            FROM pmsi.t_rapports_rubriques
            JOIN pmsi.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
            WHERE t_rapports.is_essentiel = '1'
            ;
			
			ALTER TABLE w_dbsetup ADD COLUMN to_rapport_id bigint DEFAULT 0;
			UPDATE w_dbsetup SET to_rapport_id = t_rapports.oid FROM pmsi.t_rapports WHERE rapport_code = t_rapports.code;
			
			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_id bigint DEFAULT 0;
			UPDATE w_dbsetup SET to_indicateur_id = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE indicateur_code = t_indicateurs.code AND t_indicateurs.code <> '';

			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_associe_id bigint DEFAULT 0;
			UPDATE w_dbsetup SET to_indicateur_associe_id = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE indicateur_associe_code = t_indicateurs.code AND t_indicateurs.code <> '';

			ALTER TABLE w_dbsetup ADD COLUMN to_serie_indicateur_code text[];
			UPDATE w_dbsetup SET to_serie_indicateur_code = string_to_array(serie_indicateur_code,'||');

			ALTER TABLE w_dbsetup ADD COLUMN to_serie_indicateur_id bigint[];
			UPDATE w_dbsetup SET to_serie_indicateur_id[1] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[1] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[2] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[2] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[3] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[3] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[4] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[4] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[5] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[5] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[6] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[6] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[7] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[7] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[8] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[8] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[9] = t_indicateurs.oid FROM pmsi.t_indicateurs WHERE to_serie_indicateur_code[9] = t_indicateurs.code AND t_indicateurs.code <> '';
			
			
			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_condition_external_code text[];
			UPDATE w_dbsetup SET to_indicateur_condition_external_code = string_to_array(indicateur_condition_external_code,'||');


			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_condition_id bigint[];
			UPDATE w_dbsetup SET to_indicateur_condition_id[1] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[1] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[2] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[2] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[3] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[3] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[4] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[4] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[5] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[5] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[6] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[6] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[7] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[7] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[8] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[8] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[9] = t_indicateur_condition.oid FROM pmsi.t_indicateur_condition WHERE to_indicateur_condition_external_code[9] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';

            
            -- Installer les données CTI
			DELETE FROM pmsi.t_rapports_rubriques 
			WHERE 1=1
				AND rapport_id IN (SELECT to_rapport_id FROM w_dbsetup)
			;

			INSERT INTO pmsi.t_rapports_rubriques(
				rapport_id, 
				numero_rubrique,
				indicateur_id,
				indicateur_associe_id,
				entete,
				entete_etendue,
				output_format,
				width,
				row_height,
				padding,
				font_size,
				font_weight,
				border_thickness,
				color,
				background_color,
				base_nationale,
				base_nationale_qualifiee,
				viewlink,
				viewlink_label,
				viewlink_param,
				picto_enabled,
				positif_is_good,
				description,
				description_2,
				entete_etendue_2,
				output_format_2,
				picto_enabled_2,
				positif_is_good_2,
				is_hide,
				specific_viewlink,
				specific_viewlink_label,
				specific_viewlink_param,
				indicateur_condition_id,
				erase_rapport_indicateur_condition,
				calc_formula,
				hide_on_zero_field,
				field_tag,
				serie_indicateur_id,
				serie_entete_etendue,
				serie_description,
				serie_output_format,
				serie_picto_enabled,
				serie_positif_is_good
				)
			SELECT 
				to_rapport_id, 
				numero_rubrique,
				to_indicateur_id,
				to_indicateur_associe_id,
				entete,
				entete_etendue,
				output_format,
				width,
				row_height,
				padding,
				font_size,
				font_weight,
				border_thickness,
				color,
				background_color,
				base_nationale,
				base_nationale_qualifiee,
				viewlink,
				viewlink_label,
				replace(viewlink_param,'#CHR10#',chr(10)),
				picto_enabled,
				positif_is_good,
				replace(description,'#CHR10#',chr(10)),
				replace(description_2,'#CHR10#',chr(10)),
				entete_etendue_2,
				output_format_2,
				picto_enabled_2,
				positif_is_good_2,
				is_hide,
				specific_viewlink,
				specific_viewlink_label,
				replace(specific_viewlink_param,'#CHR10#',chr(10)),
				to_indicateur_condition_id,
				erase_rapport_indicateur_condition,
				calc_formula,
				hide_on_zero_field,
				field_tag,
				to_serie_indicateur_id,
				string_to_array(serie_entete_etendue,'||'),
				string_to_array(serie_description,'||'),
				string_to_array(serie_output_format,'||'),
				string_to_array(serie_picto_enabled,'||'),
				string_to_array(serie_positif_is_good,'||')
			FROM w_dbsetup
			WHERE to_rapport_id <> 0 AND to_indicateur_id <> 0
			;
            
            -- Rejouer le spécifique
            UPDATE pmsi.t_rapports_rubriques SET
                is_hide = w_specific_rapports_rubriques.is_hide,
                specific_viewlink = w_specific_rapports_rubriques.specific_viewlink,
                specific_viewlink_label = w_specific_rapports_rubriques.specific_viewlink_label,
                specific_viewlink_param = w_specific_rapports_rubriques.specific_viewlink_param
            FROM w_specific_rapports_rubriques
            WHERE w_specific_rapports_rubriques.rapport_id = t_rapports_rubriques.rapport_id AND
                w_specific_rapports_rubriques.indicateur_id = t_rapports_rubriques.indicateur_id AND
                w_specific_rapports_rubriques.entete_etendue = t_rapports_rubriques.entete_etendue AND
                (
                    w_specific_rapports_rubriques.is_hide = '1' OR
                    w_specific_rapports_rubriques.specific_viewlink <> '' OR
                    w_specific_rapports_rubriques.specific_viewlink_label <> '' OR
                    w_specific_rapports_rubriques.specific_viewlink_param <> ''
                )
            ;
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_datim_ccaminfo" label="t_datim_ccaminfo">
		<selectCmd><![CDATA[
			
			SELECT
                accouchement,
                acte,
				CASE WHEN acte_id = 0 THEN acte_id ELSE 1 END AS acte_id,
                atext,
                autre,
                brule,
                confort,
                date_debut,
                date_fin,
                dialyse,
                esthe,
                ffm,
                fsd,
                gestcomp,
                nbextat,
                neurochir,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
                ojatypique,
                reqz,
                se,
                seance,
                unichir
			FROM pmsi.t_datim_ccaminfo
			ORDER BY acte, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			
			
			DELETE FROM pmsi.t_datim_ccaminfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
			
			UPDATE pmsi.t_datim_ccaminfo SET
				ffm = w_dbsetup.ffm,
				se = w_dbsetup.se,
				fsd = w_dbsetup.fsd,
				autre = w_dbsetup.autre,
				unichir = w_dbsetup.unichir,
				gestcomp = w_dbsetup.gestcomp,
				reqz = w_dbsetup.reqz,
				dialyse = w_dbsetup.dialyse,
				esthe = w_dbsetup.esthe,
				confort = w_dbsetup.confort,
				brule = w_dbsetup.brule,
				neurochir = w_dbsetup.neurochir,
				ojatypique = w_dbsetup.ojatypique,
				date_fin = w_dbsetup.date_fin,
				accouchement = w_dbsetup.accouchement,
				seance = w_dbsetup.seance,
				atext = w_dbsetup.atext,
				nbextat = w_dbsetup.nbextat
			FROM w_dbsetup
			WHERE w_dbsetup.acte = t_datim_ccaminfo.acte AND
				w_dbsetup.date_debut = t_datim_ccaminfo.date_debut AND
				(
					t_datim_ccaminfo.ffm IS DISTINCT FROM w_dbsetup.ffm OR
					t_datim_ccaminfo.se IS DISTINCT FROM w_dbsetup.se OR
					t_datim_ccaminfo.fsd IS DISTINCT FROM w_dbsetup.fsd OR
					t_datim_ccaminfo.autre IS DISTINCT FROM w_dbsetup.autre OR
					t_datim_ccaminfo.unichir IS DISTINCT FROM w_dbsetup.unichir OR
					t_datim_ccaminfo.gestcomp IS DISTINCT FROM w_dbsetup.gestcomp OR
					t_datim_ccaminfo.reqz IS DISTINCT FROM w_dbsetup.reqz OR
					t_datim_ccaminfo.dialyse IS DISTINCT FROM w_dbsetup.dialyse OR
					t_datim_ccaminfo.esthe IS DISTINCT FROM w_dbsetup.esthe OR
					t_datim_ccaminfo.confort IS DISTINCT FROM w_dbsetup.confort OR
					t_datim_ccaminfo.brule IS DISTINCT FROM w_dbsetup.brule OR
					t_datim_ccaminfo.neurochir IS DISTINCT FROM w_dbsetup.neurochir OR
					t_datim_ccaminfo.ojatypique IS DISTINCT FROM w_dbsetup.ojatypique OR
					t_datim_ccaminfo.date_fin IS DISTINCT FROM w_dbsetup.date_fin OR
					t_datim_ccaminfo.accouchement IS DISTINCT FROM w_dbsetup.accouchement OR
					t_datim_ccaminfo.seance IS DISTINCT FROM w_dbsetup.seance OR
					t_datim_ccaminfo.atext IS DISTINCT FROM w_dbsetup.atext OR
					t_datim_ccaminfo.nbextat IS DISTINCT FROM w_dbsetup.nbextat
				);

			INSERT INTO pmsi.t_datim_ccaminfo (
				acte,
				ffm,
				se,
				fsd,
				autre,
				unichir,
				gestcomp,
				reqz,
				dialyse,
				esthe,
				confort,
				brule,
				neurochir,
				ojatypique,
				date_debut,
				date_fin,
				accouchement,
				seance,
				atext,
				nbextat
			)
			SELECT w_dbsetup.acte,
				w_dbsetup.ffm,
				w_dbsetup.se,
				w_dbsetup.fsd,
				w_dbsetup.autre,
				w_dbsetup.unichir,
				w_dbsetup.gestcomp,
				w_dbsetup.reqz,
				w_dbsetup.dialyse,
				w_dbsetup.esthe,
				w_dbsetup.confort,
				w_dbsetup.brule,
				w_dbsetup.neurochir,
				w_dbsetup.ojatypique,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin,
				w_dbsetup.accouchement,
				w_dbsetup.seance,
				w_dbsetup.atext,
				w_dbsetup.nbextat
			FROM w_dbsetup
				LEFT JOIN pmsi.t_datim_ccaminfo ON w_dbsetup.acte = t_datim_ccaminfo.acte AND
					w_dbsetup.date_debut = t_datim_ccaminfo.date_debut
			WHERE t_datim_ccaminfo.acte IS NULL;
			
			
			UPDATE pmsi.t_datim_ccaminfo SET oid = nextval('pmsi.s_datim_ccaminfo'::regclass) WHERE oid IS NULL;
			
			UPDATE pmsi.t_datim_ccaminfo
			SET acte_id = t_actes.oid
			FROM pmsi.t_actes
			WHERE substr(t_datim_ccaminfo.acte,1,7) = t_actes.code AND
				t_datim_ccaminfo.acte_id IS DISTINCT FROM t_actes.oid;
			
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_datim_dminfo" label="t_datim_dminfo">
		<selectCmd><![CDATA[
			
			SELECT 
                date_debut,
                date_fin,
                lpp,
                CASE WHEN lpp_id = 0 THEN lpp_id ELSE 1 END AS lpp_id,
                nblppat,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid
			FROM pmsi.t_datim_dminfo
			ORDER BY lpp, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			DELETE FROM pmsi.t_datim_dminfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
			
			UPDATE pmsi.t_datim_dminfo SET
				nblppat = w_dbsetup.nblppat
			FROM w_dbsetup
			WHERE w_dbsetup.lpp = t_datim_dminfo.lpp AND
				w_dbsetup.date_debut = t_datim_dminfo.date_debut AND
				t_datim_dminfo.nblppat IS DISTINCT FROM w_dbsetup.nblppat
			;

			INSERT INTO pmsi.t_datim_dminfo (
				lpp,
				nblppat,
				date_debut,
				date_fin
			)
			SELECT w_dbsetup.lpp,
				w_dbsetup.nblppat,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin
			FROM w_dbsetup
				LEFT JOIN pmsi.t_datim_dminfo ON w_dbsetup.lpp = t_datim_dminfo.lpp AND
					w_dbsetup.date_debut = t_datim_dminfo.date_debut
			WHERE t_datim_dminfo.lpp IS NULL;
			
			UPDATE pmsi.t_datim_dminfo SET oid = nextval('pmsi.s_datim_dminfo'::regclass) WHERE oid IS NULL;
			
			UPDATE pmsi.t_datim_dminfo
			SET lpp_id = t_lpp.oid
			FROM pmsi.t_lpp
			WHERE substr(t_datim_dminfo.lpp,1,7) = t_lpp.code AND
				t_datim_dminfo.lpp_id IS DISTINCT FROM t_lpp.oid;
			
			
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_datim_medinfo" label="t_datim_medinfo">
		<selectCmd><![CDATA[
			
			SELECT
                date_debut,
                date_fin,
                nbucdat,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
                ucd,
                CASE WHEN ucd_id = 0 THEN ucd_id ELSE 1 END AS ucd_id
			FROM pmsi.t_datim_medinfo
			ORDER BY ucd, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			DELETE FROM pmsi.t_datim_medinfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
			
			UPDATE pmsi.t_datim_medinfo SET
				nbucdat = w_dbsetup.nbucdat
			FROM w_dbsetup
			WHERE w_dbsetup.ucd = t_datim_medinfo.ucd AND
				w_dbsetup.date_debut = t_datim_medinfo.date_debut AND
				t_datim_medinfo.nbucdat IS DISTINCT FROM w_dbsetup.nbucdat
			;

			INSERT INTO pmsi.t_datim_medinfo (
				ucd,
				nbucdat,
				date_debut,
				date_fin
			)
			SELECT w_dbsetup.ucd,
				w_dbsetup.nbucdat,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin
			FROM w_dbsetup
				LEFT JOIN pmsi.t_datim_medinfo ON w_dbsetup.ucd = t_datim_medinfo.ucd AND
					w_dbsetup.date_debut = t_datim_medinfo.date_debut
			WHERE t_datim_medinfo.ucd IS NULL;
			
			UPDATE pmsi.t_datim_medinfo SET oid = nextval('pmsi.s_datim_dminfo'::regclass) WHERE oid IS NULL;
			
			UPDATE pmsi.t_datim_medinfo
			SET ucd_id = t_ucd.oid
			FROM pmsi.t_ucd
			WHERE substr(t_datim_medinfo.ucd,1,7) = t_ucd.code AND
				t_datim_medinfo.ucd_id IS DISTINCT FROM t_ucd.oid;
			
			
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_datim_diaginfo" label="t_datim_diaginfo">
		<selectCmd><![CDATA[
			
			SELECT
                affaig,
                autres,
                brulure,
                brulurebis,
                cl1v,
                cl2v,
                cl3v,
                cl4v,
                cl5v,
                cl6v,
                cma,
                codext,
                codseq,
                compimprecis,
                cti_cma_fils,
                cti_cma_frere,
                dague,
                date_debut,
                date_fin,
                dgcpt669,
                diag,
				CASE WHEN diagnostic_id = 0 THEN diagnostic_id ELSE 1 END AS diagnostic_id,
                dpacte,
                dpchir,
                esth,
                imprecis,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
                rare,
                seance,
                sexed,
                timprecis,
                tintox,
                zaffchr,
                zinhab
			FROM pmsi.t_datim_diaginfo
			ORDER BY diag, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			
			
			DELETE FROM pmsi.t_datim_diaginfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
						
			UPDATE pmsi.t_datim_diaginfo SET
				rare = w_dbsetup.rare,
				dpchir = w_dbsetup.dpchir,
				dague = w_dbsetup.dague,
				sexed = w_dbsetup.sexed,
				cl1v = w_dbsetup.cl1v,
				cl2v = w_dbsetup.cl2v,
				cl3v = w_dbsetup.cl3v,
				cl4v = w_dbsetup.cl4v,
				cl5v = w_dbsetup.cl5v,
				cl6v = w_dbsetup.cl6v,
				zinhab = w_dbsetup.zinhab,
				imprecis = w_dbsetup.imprecis,
				zaffchr = w_dbsetup.zaffchr,
				codext = w_dbsetup.codext,
				codseq = w_dbsetup.codseq,
				timprecis = w_dbsetup.timprecis,
				tintox = w_dbsetup.tintox,
				cma = w_dbsetup.cma,
				dpacte = w_dbsetup.dpacte,
				dgcpt669 = w_dbsetup.dgcpt669,
				brulure = w_dbsetup.brulure,
				esth = w_dbsetup.esth,
				affaig = w_dbsetup.affaig,
				date_fin = w_dbsetup.date_fin,
				compimprecis = w_dbsetup.compimprecis,
				autres = w_dbsetup.autres,
				brulurebis = w_dbsetup.brulurebis,
				cti_cma_fils = w_dbsetup.cti_cma_fils,
				cti_cma_frere = w_dbsetup.cti_cma_frere,
				seance = w_dbsetup.seance
			FROM w_dbsetup
			WHERE w_dbsetup.diag = t_datim_diaginfo.diag AND
				w_dbsetup.date_debut = t_datim_diaginfo.date_debut AND
				(
					t_datim_diaginfo.rare IS DISTINCT FROM w_dbsetup.rare OR
					t_datim_diaginfo.dpchir IS DISTINCT FROM w_dbsetup.dpchir OR
					t_datim_diaginfo.dague IS DISTINCT FROM w_dbsetup.dague OR
					t_datim_diaginfo.sexed IS DISTINCT FROM w_dbsetup.sexed OR
					t_datim_diaginfo.cl1v IS DISTINCT FROM w_dbsetup.cl1v OR
					t_datim_diaginfo.cl2v IS DISTINCT FROM w_dbsetup.cl2v OR
					t_datim_diaginfo.cl3v IS DISTINCT FROM w_dbsetup.cl3v OR
					t_datim_diaginfo.cl4v IS DISTINCT FROM w_dbsetup.cl4v OR
					t_datim_diaginfo.cl5v IS DISTINCT FROM w_dbsetup.cl5v OR
					t_datim_diaginfo.cl6v IS DISTINCT FROM w_dbsetup.cl6v OR
					t_datim_diaginfo.zinhab IS DISTINCT FROM w_dbsetup.zinhab OR
					t_datim_diaginfo.imprecis IS DISTINCT FROM w_dbsetup.imprecis OR
					t_datim_diaginfo.zaffchr IS DISTINCT FROM w_dbsetup.zaffchr OR
					t_datim_diaginfo.codext IS DISTINCT FROM w_dbsetup.codext OR
					t_datim_diaginfo.codseq IS DISTINCT FROM w_dbsetup.codseq OR
					t_datim_diaginfo.timprecis IS DISTINCT FROM w_dbsetup.timprecis OR
					t_datim_diaginfo.tintox IS DISTINCT FROM w_dbsetup.tintox OR
					t_datim_diaginfo.cma IS DISTINCT FROM w_dbsetup.cma OR
					t_datim_diaginfo.dpacte IS DISTINCT FROM w_dbsetup.dpacte OR
					t_datim_diaginfo.dgcpt669 IS DISTINCT FROM w_dbsetup.dgcpt669 OR
					t_datim_diaginfo.brulure IS DISTINCT FROM w_dbsetup.brulure OR
					t_datim_diaginfo.esth IS DISTINCT FROM w_dbsetup.esth OR
					t_datim_diaginfo.affaig IS DISTINCT FROM w_dbsetup.affaig OR
					t_datim_diaginfo.date_fin IS DISTINCT FROM w_dbsetup.date_fin OR
					t_datim_diaginfo.compimprecis IS DISTINCT FROM w_dbsetup.compimprecis OR
					t_datim_diaginfo.autres IS DISTINCT FROM w_dbsetup.autres OR
					t_datim_diaginfo.brulurebis IS DISTINCT FROM w_dbsetup.brulurebis OR
					t_datim_diaginfo.cti_cma_fils IS DISTINCT FROM w_dbsetup.cti_cma_fils OR
					t_datim_diaginfo.cti_cma_frere IS DISTINCT FROM w_dbsetup.cti_cma_frere OR
					t_datim_diaginfo.seance IS DISTINCT FROM w_dbsetup.seance
				);
			
			INSERT INTO pmsi.t_datim_diaginfo (
				diag,
				rare,
				dpchir,
				dague,
				sexed,
				cl1v,
				cl2v,
				cl3v,
				cl4v,
				cl5v,
				cl6v,
				zinhab,
				imprecis,
				zaffchr,
				codext,
				codseq,
				timprecis,
				tintox,
				cma,
				dpacte,
				dgcpt669,
				brulure,
				esth,
				affaig,
				date_debut,
				date_fin,
				compimprecis,
				autres,
				brulurebis,
				cti_cma_fils,
				cti_cma_frere,
				seance
			)
			SELECT w_dbsetup.diag,
				w_dbsetup.rare,
				w_dbsetup.dpchir,
				w_dbsetup.dague,
				w_dbsetup.sexed,
				w_dbsetup.cl1v,
				w_dbsetup.cl2v,
				w_dbsetup.cl3v,
				w_dbsetup.cl4v,
				w_dbsetup.cl5v,
				w_dbsetup.cl6v,
				w_dbsetup.zinhab,
				w_dbsetup.imprecis,
				w_dbsetup.zaffchr,
				w_dbsetup.codext,
				w_dbsetup.codseq,
				w_dbsetup.timprecis,
				w_dbsetup.tintox,
				w_dbsetup.cma,
				w_dbsetup.dpacte,
				w_dbsetup.dgcpt669,
				w_dbsetup.brulure,
				w_dbsetup.esth,
				w_dbsetup.affaig,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin,
				w_dbsetup.compimprecis,
				w_dbsetup.autres,
				w_dbsetup.brulurebis,
				w_dbsetup.cti_cma_fils,
				w_dbsetup.cti_cma_frere,
				w_dbsetup.seance
			FROM w_dbsetup
				LEFT JOIN pmsi.t_datim_diaginfo ON w_dbsetup.diag = t_datim_diaginfo.diag AND
					w_dbsetup.date_debut = t_datim_diaginfo.date_debut
			WHERE t_datim_diaginfo.diag IS NULL;
			
			
			UPDATE pmsi.t_datim_diaginfo SET oid = nextval('pmsi.s_datim_diaginfo'::regclass) WHERE oid IS NULL;
			
			UPDATE pmsi.t_datim_diaginfo
			SET diagnostic_id = t_diagnostics.oid
			FROM pmsi.t_diagnostics
			WHERE t_datim_diaginfo.diag = t_diagnostics.code AND
				t_datim_diaginfo.diagnostic_id IS DISTINCT FROM t_diagnostics.oid;
			
			
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_datim_ghminfo" label="t_datim_ghminfo">
		<selectCmd><![CDATA[
			
			SELECT
                binf,
                bpcdc,
                bsup,
                cma,
                cmas,
                cmd,
                cti_1orj,
                date_debut,
                date_fin,
                dms,
                dsatyp,
                ghm,
				CASE WHEN ghm_id = 0 THEN ghm_id ELSE 1 END AS ghm_id,
                mage,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
                pctautres,
                pctcma4,
                pctcourt,
                pctdc,
                pctvalo,
                pvalo,
                tarif,
                test9,
                tghm,
                typets
			FROM pmsi.t_datim_ghminfo
			ORDER BY ghm, typets, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			
			
			DELETE FROM pmsi.t_datim_ghminfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
				
			UPDATE pmsi.t_datim_ghminfo SET
				cma = w_dbsetup.cma,
				cmas = w_dbsetup.cmas,
				tghm = w_dbsetup.tghm,
				cmd = w_dbsetup.cmd,
				tarif = w_dbsetup.tarif,
				dms = w_dbsetup.dms,
				pctdc = w_dbsetup.pctdc,
				mage = w_dbsetup.mage,
				binf = w_dbsetup.binf,
				bsup = w_dbsetup.bsup,
				dsatyp = w_dbsetup.dsatyp,
				test9 = w_dbsetup.test9,
				bpcdc = w_dbsetup.bpcdc,
				pctcourt = w_dbsetup.pctcourt,
				pvalo = w_dbsetup.pvalo,
				pctvalo = w_dbsetup.pctvalo,
				pctcma4 = w_dbsetup.pctcma4,
				date_fin = w_dbsetup.date_fin,
				pctautres = w_dbsetup.pctautres,
				cti_1orj = w_dbsetup.cti_1orj 
			FROM w_dbsetup
			WHERE w_dbsetup.ghm = t_datim_ghminfo.ghm AND
				w_dbsetup.typets = t_datim_ghminfo.typets AND
				w_dbsetup.date_debut = t_datim_ghminfo.date_debut AND
				(
					t_datim_ghminfo.cma IS DISTINCT FROM w_dbsetup.cma OR
					t_datim_ghminfo.cmas IS DISTINCT FROM w_dbsetup.cmas OR
					t_datim_ghminfo.tghm IS DISTINCT FROM w_dbsetup.tghm OR
					t_datim_ghminfo.cmd IS DISTINCT FROM w_dbsetup.cmd OR
					t_datim_ghminfo.tarif IS DISTINCT FROM w_dbsetup.tarif OR
					t_datim_ghminfo.dms IS DISTINCT FROM w_dbsetup.dms OR
					t_datim_ghminfo.pctdc IS DISTINCT FROM w_dbsetup.pctdc OR
					t_datim_ghminfo.mage IS DISTINCT FROM w_dbsetup.mage OR
					t_datim_ghminfo.binf IS DISTINCT FROM w_dbsetup.binf OR
					t_datim_ghminfo.bsup IS DISTINCT FROM w_dbsetup.bsup OR
					t_datim_ghminfo.dsatyp IS DISTINCT FROM w_dbsetup.dsatyp OR
					t_datim_ghminfo.test9 IS DISTINCT FROM w_dbsetup.test9 OR
					t_datim_ghminfo.bpcdc IS DISTINCT FROM w_dbsetup.bpcdc OR
					t_datim_ghminfo.pctcourt IS DISTINCT FROM w_dbsetup.pctcourt OR
					t_datim_ghminfo.pvalo IS DISTINCT FROM w_dbsetup.pvalo OR
					t_datim_ghminfo.pctvalo IS DISTINCT FROM w_dbsetup.pctvalo OR
					t_datim_ghminfo.pctcma4 IS DISTINCT FROM w_dbsetup.pctcma4 OR
					t_datim_ghminfo.date_fin IS DISTINCT FROM w_dbsetup.date_fin OR
					t_datim_ghminfo.pctautres IS DISTINCT FROM w_dbsetup.pctautres OR
					t_datim_ghminfo.cti_1orj IS DISTINCT FROM w_dbsetup.cti_1orj 
				);

		
			INSERT INTO pmsi.t_datim_ghminfo (
				typets,
				ghm,
				cma,
				cmas,
				tghm,
				cmd,
				tarif,
				dms,
				pctdc,
				mage,
				binf,
				bsup,
				dsatyp,
				test9,
				bpcdc,
				pctcourt,
				pvalo,
				pctvalo,
				pctcma4,
				date_debut,
				date_fin,
				pctautres,
				cti_1orj
			)
			SELECT 
				w_dbsetup.typets,
				w_dbsetup.ghm,
				w_dbsetup.cma,
				w_dbsetup.cmas,
				w_dbsetup.tghm,
				w_dbsetup.cmd,
				w_dbsetup.tarif,
				w_dbsetup.dms,
				w_dbsetup.pctdc,
				w_dbsetup.mage,
				w_dbsetup.binf,
				w_dbsetup.bsup,
				w_dbsetup.dsatyp,
				w_dbsetup.test9,
				w_dbsetup.bpcdc,
				w_dbsetup.pctcourt,
				w_dbsetup.pvalo,
				w_dbsetup.pctvalo,
				w_dbsetup.pctcma4,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin,
				w_dbsetup.pctautres,
				w_dbsetup.cti_1orj
			FROM w_dbsetup
				LEFT JOIN pmsi.t_datim_ghminfo ON w_dbsetup.ghm = t_datim_ghminfo.ghm AND
					w_dbsetup.date_debut = t_datim_ghminfo.date_debut AND
					w_dbsetup.typets = t_datim_ghminfo.typets
			WHERE t_datim_ghminfo.ghm IS NULL;
			
			UPDATE pmsi.t_datim_ghminfo SET oid = nextval('pmsi.s_datim_ghminfo'::regclass) WHERE oid IS NULL;
			
			UPDATE pmsi.t_datim_ghminfo
			SET ghm_id = t_ghm.oid
			FROM pmsi.t_ghm
			WHERE t_datim_ghminfo.ghm = t_ghm.code AND
				t_datim_ghminfo.ghm_id IS DISTINCT FROM t_ghm.oid;
			
		]]></updateCmd>
	</TABLE>


	<TABLE name="t_datim_ghsinfo" label="t_datim_ghsinfo">
		<selectCmd><![CDATA[
			
			SELECT
                binf,
                bsup,
                date_debut,
                date_fin,
				CASE WHEN ghs_id = 0 THEN ghs_id ELSE 1 END AS ghs_id,
                numghs,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
                test44,
                typets
			FROM pmsi.t_datim_ghsinfo
			ORDER BY numghs, typets, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			
			
			DELETE FROM pmsi.t_datim_ghsinfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
				
			UPDATE pmsi.t_datim_ghsinfo SET
				test44 = w_dbsetup.test44,
				binf = w_dbsetup.binf,
				bsup = w_dbsetup.bsup,
				date_fin = w_dbsetup.date_fin
			FROM w_dbsetup
			WHERE w_dbsetup.numghs = t_datim_ghsinfo.numghs AND
				w_dbsetup.typets = t_datim_ghsinfo.typets AND
				w_dbsetup.date_debut = t_datim_ghsinfo.date_debut AND
				(
					t_datim_ghsinfo.test44 IS DISTINCT FROM w_dbsetup.test44 OR
					t_datim_ghsinfo.binf IS DISTINCT FROM w_dbsetup.binf OR
					t_datim_ghsinfo.bsup IS DISTINCT FROM w_dbsetup.bsup OR
					t_datim_ghsinfo.date_fin IS DISTINCT FROM w_dbsetup.date_fin
				);
			
			INSERT INTO pmsi.t_datim_ghsinfo (
				typets,
				numghs,
				test44,
				binf,
				bsup,
				date_debut,
				date_fin
			)
			SELECT 
				w_dbsetup.typets,
				w_dbsetup.numghs,
				w_dbsetup.test44,
				w_dbsetup.binf,
				w_dbsetup.bsup,
				w_dbsetup.date_debut,
				w_dbsetup.date_fin
			FROM w_dbsetup
				LEFT JOIN pmsi.t_datim_ghsinfo ON w_dbsetup.numghs = t_datim_ghsinfo.numghs AND
					w_dbsetup.date_debut = t_datim_ghsinfo.date_debut AND
					w_dbsetup.typets = t_datim_ghsinfo.typets
			WHERE t_datim_ghsinfo.numghs IS NULL;
			
			UPDATE pmsi.t_datim_ghsinfo SET oid = nextval('pmsi.s_datim_ghsinfo'::regclass) WHERE oid IS NULL;
			
			UPDATE pmsi.t_datim_ghsinfo
			SET ghs_id = t_ghs.oid
			FROM pmsi.t_ghs
			WHERE t_datim_ghsinfo.numghs = t_ghs.code AND
				t_datim_ghsinfo.ghs_id IS DISTINCT FROM t_ghs.oid;
			
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_datim_racineinfo" label="t_datim_racineinfo">
		<selectCmd><![CDATA[
			
			SELECT
                age2,
                age69,
                app,
                bpctcma,
                conf,
                date_debut,
                date_fin,
				CASE WHEN ghm5_id = 0 THEN ghm5_id ELSE 1 END AS ghm5_id,
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
                pctapp,
                pctcma,
                pctcma2,
                pctcma3,
                pctdc,
                pctexb,
                pctssacte,
                racine,
                typets
			FROM pmsi.t_datim_racineinfo
			ORDER BY racine, typets, date_debut;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
			
			
			
			DELETE FROM pmsi.t_datim_racineinfo WHERE date_debut IS NULL OR (date_debut < '20160301'::date AND date_fin < '2099-12-31'::date);
				
			UPDATE pmsi.t_datim_racineinfo SET
				pctdc = w_dbsetup.pctdc,
				conf = w_dbsetup.conf,
				age2 = w_dbsetup.age2,
				age69 = w_dbsetup.age69,
				app = w_dbsetup.app,
				pctapp = w_dbsetup.pctapp,
				bpctcma = w_dbsetup.bpctcma,
				pctcma = w_dbsetup.pctcma,
				pctcma2 = w_dbsetup.pctcma2,
				pctcma3 = w_dbsetup.pctcma3,
				pctexb = w_dbsetup.pctexb,
				pctssacte = w_dbsetup.pctssacte,
				date_fin = w_dbsetup.date_fin
			FROM w_dbsetup
			WHERE w_dbsetup.racine = t_datim_racineinfo.racine AND
				w_dbsetup.typets = t_datim_racineinfo.typets AND
				w_dbsetup.date_debut = t_datim_racineinfo.date_debut AND
				(
					t_datim_racineinfo.pctdc IS DISTINCT FROM w_dbsetup.pctdc OR
					t_datim_racineinfo.conf IS DISTINCT FROM w_dbsetup.conf OR
					t_datim_racineinfo.age2 IS DISTINCT FROM w_dbsetup.age2 OR
					t_datim_racineinfo.age69 IS DISTINCT FROM w_dbsetup.age69 OR
					t_datim_racineinfo.app IS DISTINCT FROM w_dbsetup.app OR
					t_datim_racineinfo.pctapp IS DISTINCT FROM w_dbsetup.pctapp OR
					t_datim_racineinfo.bpctcma IS DISTINCT FROM w_dbsetup.bpctcma OR
					t_datim_racineinfo.pctcma IS DISTINCT FROM w_dbsetup.pctcma OR
					t_datim_racineinfo.pctcma2 IS DISTINCT FROM w_dbsetup.pctcma2 OR
					t_datim_racineinfo.pctcma3 IS DISTINCT FROM w_dbsetup.pctcma3 OR
					t_datim_racineinfo.pctexb IS DISTINCT FROM w_dbsetup.pctexb OR
					t_datim_racineinfo.pctssacte IS DISTINCT FROM w_dbsetup.pctssacte OR
					t_datim_racineinfo.date_fin IS DISTINCT FROM w_dbsetup.date_fin
				);

		
				INSERT INTO pmsi.t_datim_racineinfo (
					typets,
					racine,
					pctdc,
					conf,
					age2,
					age69,
					app,
					pctapp,
					bpctcma,
					pctcma,
					pctcma2,
					pctcma3,
					pctexb,
					pctssacte,
					date_debut,
					date_fin
				)
				SELECT 
					w_dbsetup.typets,
					w_dbsetup.racine,
					w_dbsetup.pctdc,
					w_dbsetup.conf,
					w_dbsetup.age2,
					w_dbsetup.age69,
					w_dbsetup.app,
					w_dbsetup.pctapp,
					w_dbsetup.bpctcma,
					w_dbsetup.pctcma,
					w_dbsetup.pctcma2,
					w_dbsetup.pctcma3,
					w_dbsetup.pctexb,
					w_dbsetup.pctssacte,
					w_dbsetup.date_debut,
					w_dbsetup.date_fin
				FROM w_dbsetup
					LEFT JOIN pmsi.t_datim_racineinfo ON w_dbsetup.racine = t_datim_racineinfo.racine AND
						w_dbsetup.date_debut = t_datim_racineinfo.date_debut AND
						w_dbsetup.typets = t_datim_racineinfo.typets
				WHERE t_datim_racineinfo.racine IS NULL;
				
				
				UPDATE pmsi.t_datim_racineinfo SET oid = nextval('pmsi.s_datim_racineinfo'::regclass) WHERE oid IS NULL;
				
				UPDATE pmsi.t_datim_racineinfo
				SET ghm5_id = t_ghm5.oid
				FROM pmsi.t_ghm5
				WHERE t_datim_racineinfo.racine = t_ghm5.code AND
					t_datim_racineinfo.ghm5_id IS DISTINCT FROM t_ghm5.oid;
			
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_type_uf_consultation" label="Type d'unité fonctionnelle de consultations" schema="pmsi">
		<selectCmd>
			<![CDATA[ 
			
			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court
			FROM pmsi.t_type_uf_consultation
			ORDER BY code
			;
			
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[ 
			
			UPDATE pmsi.t_type_uf_consultation
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 
				t_type_uf_consultation.oid = w_dbsetup.oid AND w_dbsetup.oid = 0
			;
			
			UPDATE pmsi.t_type_uf_consultation
			SET
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE 
				t_type_uf_consultation.code = w_dbsetup.code
				AND (
						t_type_uf_consultation.texte IS DISTINCT FROM w_dbsetup.texte OR
						t_type_uf_consultation.texte_court IS DISTINCT FROM w_dbsetup.texte_court
					)
			;
			
			INSERT INTO pmsi.t_type_uf_consultation(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_type_uf_consultation)
			;
			
			INSERT INTO pmsi.t_type_uf_consultation(code, texte, texte_court)
				SELECT code, texte, texte_court
				FROM w_dbsetup
				WHERE oid <> 0 AND code NOT IN (SELECT code FROM pmsi.t_type_uf_consultation)
			;
			
			]]>
		</updateCmd>
	</TABLE>
	<TABLE name="t_regles_non_valorisation" label="Règles non valorisation">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM pmsi.t_regles_non_valorisation 
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_regles_non_valorisation SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE t_regles_non_valorisation.oid = w_dbsetup.oid AND w_dbsetup.oid = 0;
			
			
			UPDATE pmsi.t_regles_non_valorisation SET 
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 1=1
				AND t_regles_non_valorisation.code = w_dbsetup.code
				AND t_regles_non_valorisation.texte IS DISTINCT FROM w_dbsetup.texte;
			
			
			INSERT INTO pmsi.t_regles_non_valorisation(
				oid,code,texte)
			SELECT 
				oid,code,texte
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM pmsi.t_regles_non_valorisation);
			
			
			INSERT INTO pmsi.t_regles_non_valorisation(code,texte)
			SELECT 
				code,texte
			FROM w_dbsetup 
			WHERE 
				oid <> 0 
				AND code NOT IN (SELECT code FROM pmsi.t_regles_non_valorisation);
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_conversion_hp_hc" label="Conversion HP-HC">
		<selectCmd><![CDATA[
		
			SELECT 
				t_conversion_hp_hc.oid as keepoid,
				t_conversion_hp_hc.code,
				t_conversion_hp_hc.texte
			FROM
				pmsi.t_conversion_hp_hc
			WHERE 
				t_conversion_hp_hc.code <> t_conversion_hp_hc.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_conversion_hp_hc
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_conversion_hp_hc.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_conversion_hp_hc.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_conversion_hp_hc.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_conversion_hp_hc (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_conversion_hp_hc ON t_conversion_hp_hc.oid = w_dbsetup.keepoid
            WHERE 
                t_conversion_hp_hc.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_raac" label="Conversion HP-HC">
		<selectCmd><![CDATA[
		
			SELECT 
				t_raac.oid as keepoid,
				t_raac.code,
				t_raac.texte
			FROM
				pmsi.t_raac
			WHERE 
				t_raac.code <> t_raac.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_raac
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_raac.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_raac.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_raac.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_raac (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_raac ON t_raac.oid = w_dbsetup.keepoid
            WHERE 
                t_raac.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_np" label="Non programmé">
		<selectCmd><![CDATA[
		
			SELECT 
				t_np.oid as keepoid,
				t_np.code,
				t_np.texte
			FROM
				pmsi.t_np
			WHERE 
				t_np.code <> t_np.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_np
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_np.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_np.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_np.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_np (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_np ON t_np.oid = w_dbsetup.keepoid
            WHERE 
                t_np.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_etat_sejour" label="Etat des séjours">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM pmsi.t_etat_sejour
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_etat_sejour SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_etat_sejour.oid AND w_dbsetup.oid = 0;
			
			UPDATE pmsi.t_etat_sejour SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 1=1
				AND pmsi.t_etat_sejour.code = w_dbsetup.code
				AND pmsi.t_etat_sejour.texte IS DISTINCT FROM w_dbsetup.texte
			;

			INSERT INTO pmsi.t_etat_sejour(oid, code, texte)
				SELECT oid, code, texte
				FROM w_dbsetup 
				WHERE 1=1
					AND oid = 0 
					AND oid NOT IN (SELECT oid FROM pmsi.t_etat_sejour)
			;
				
			INSERT INTO pmsi.t_etat_sejour(code, texte)
				SELECT code, texte
				FROM w_dbsetup 
				WHERE 1=1
					AND oid <> 0 
					AND code NOT IN (SELECT code FROM pmsi.t_etat_sejour)
			;
			
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_severites_cti" label="Sévérité CTI">
		<selectCmd><![CDATA[ 
			
			SELECT 
				severite_id AS keepoid,
				severite_code,
				severite_texte
			FROM pmsi.t_severites_cti
			ORDER BY severite_code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE pmsi.t_severites_cti SET 
				severite_code = w_dbsetup.severite_code,
				severite_texte = w_dbsetup.severite_texte
			FROM w_dbsetup
			WHERE w_dbsetup.keepoid = t_severites_cti.severite_id AND w_dbsetup.keepoid = 0;
            
            UPDATE pmsi.t_severites_cti SET
				severite_texte = w_dbsetup.severite_texte
			FROM w_dbsetup
			WHERE 1=1
				AND pmsi.t_severites_cti.severite_code = w_dbsetup.severite_code
				AND pmsi.t_severites_cti.severite_texte IS DISTINCT FROM w_dbsetup.severite_texte
			;

			INSERT INTO pmsi.t_severites_cti(severite_id, severite_code, severite_texte)
				SELECT keepoid, severite_code, severite_texte
				FROM w_dbsetup 
				WHERE 1=1
					AND keepoid = 0 
					AND keepoid NOT IN (SELECT severite_id FROM pmsi.t_severites_cti)
			;
				
			INSERT INTO pmsi.t_severites_cti(severite_id, severite_code, severite_texte)
				SELECT keepoid, severite_code, severite_texte
				FROM w_dbsetup 
				WHERE 1=1
					AND keepoid <> 0 
					AND severite_code NOT IN (SELECT severite_code FROM pmsi.t_severites_cti)
			;
			
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_vue_parametre" label="Listes des données prédéfinis">
		<selectCmd><![CDATA[ 
			
		SELECT
			CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
			code, 
			texte, 
			array_to_string(data_code, '@') as data_code, 
			array_to_string(data_entete, '@') as data_entete, 
			array_to_string(data_format, '@') as data_format, 
			array_to_string(data_description, '@') as data_description,
			position_ratio,
			rang
		FROM pmsi.t_vue_parametre 
			ORDER BY 1,2;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO pmsi.t_vue_parametre(code, texte, data_code, data_entete, data_format, data_description,position_ratio,rang)
				SELECT  code, 
						texte, 
						string_to_array(data_code, '@') as data_code, 
						string_to_array(data_entete, '@') as data_entete, 
						string_to_array(data_format, '@') as data_format, 
						string_to_array(data_description, '@') as data_description,
                        position_ratio,
                        rang
				FROM w_dbsetup 
				WHERE 1=1 
					AND code NOT IN (SELECT code FROM pmsi.t_vue_parametre)
			;
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_rescrit_tarif" label="rescrit tarifaire">
		<selectCmd><![CDATA[
		
			SELECT 
				t_rescrit_tarif.oid as keepoid,
				t_rescrit_tarif.code,
				t_rescrit_tarif.texte
			FROM
				pmsi.t_rescrit_tarif
			WHERE 
				t_rescrit_tarif.code <> t_rescrit_tarif.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_rescrit_tarif
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_rescrit_tarif.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_rescrit_tarif.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_rescrit_tarif.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_rescrit_tarif (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_rescrit_tarif ON t_rescrit_tarif.oid = w_dbsetup.keepoid
            WHERE 
                t_rescrit_tarif.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_adm_prod_reshospi" label="variable administration de produits de la réserve hospitalière">
		<selectCmd><![CDATA[
		
			SELECT 
				t_adm_prod_reshospi.oid as keepoid,
				t_adm_prod_reshospi.code,
				t_adm_prod_reshospi.texte
			FROM
				pmsi.t_adm_prod_reshospi
			WHERE 
				t_adm_prod_reshospi.code <> t_adm_prod_reshospi.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_adm_prod_reshospi
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_adm_prod_reshospi.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_adm_prod_reshospi.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_adm_prod_reshospi.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_adm_prod_reshospi (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_adm_prod_reshospi ON t_adm_prod_reshospi.oid = w_dbsetup.keepoid
            WHERE 
                t_adm_prod_reshospi.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_contexte_pat" label="La variable contexte du patient">
		<selectCmd><![CDATA[
		
			SELECT 
				t_contexte_pat.oid as keepoid,
				t_contexte_pat.code,
				t_contexte_pat.texte
			FROM
				pmsi.t_contexte_pat
			WHERE 
				t_contexte_pat.code <> t_contexte_pat.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_contexte_pat
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_contexte_pat.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_contexte_pat.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_contexte_pat.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_contexte_pat (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_contexte_pat ON t_contexte_pat.oid = w_dbsetup.keepoid
            WHERE 
                t_contexte_pat.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_nb_interventions" label="Nombre d'intervention">
		<selectCmd><![CDATA[
		
			SELECT 
				t_nb_interventions.oid as keepoid,
				t_nb_interventions.code,
				t_nb_interventions.texte
			FROM
				pmsi.t_nb_interventions
			WHERE 
				t_nb_interventions.code <> t_nb_interventions.texte
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
		
			UPDATE pmsi.t_nb_interventions
			SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_nb_interventions.oid = w_dbsetup.keepoid
				AND (1 != 1
					OR t_nb_interventions.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_nb_interventions.code IS DISTINCT FROM w_dbsetup.code
					)
            ;
			
			INSERT INTO pmsi.t_nb_interventions (oid,code,texte)
            SELECT 
                w_dbsetup.keepoid,
                w_dbsetup.code,
                w_dbsetup.texte
            FROM
                w_dbsetup
                LEFT JOIN pmsi.t_nb_interventions ON t_nb_interventions.oid = w_dbsetup.keepoid
            WHERE 
                t_nb_interventions.oid IS NULL
            ;
		
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_indicateur_serie" label="Liste serie disponibles pour les graphiques">
		<selectCmd><![CDATA[ 
		
		WITH flatSeries as (
			SELECT 
				oid,
				cast(jsonb_array_elements(replace('[' || serie_id || ']', '|', ',')::jsonb)->>'pos' as bigint) as position,
				cast(jsonb_array_elements(replace('[' || serie_id || ']', '|', ',')::jsonb)->>'id' as bigint) as id
			FROM pmsi.t_indicateur_serie
			WHERE serie_id != '{}' AND serie_id != ''
			ORDER BY oid, position
		),

		flatCodes as (SELECT 
			flatSeries.oid,
			array_agg(position::text || ':' || t_indicateur_serie.code) as codes
		FROM flatSeries
		LEFT JOIN pmsi.t_indicateur_serie on t_indicateur_serie.oid = id
		GROUP BY 1)

		SELECT
			t_indicateur_serie.code,
			coalesce(t_indicateurs.code, '') as indicateur_code,
			t_indicateur_serie.name,
			t_indicateur_serie.color,
			t_indicateur_serie.formula,
			t_indicateur_serie.calc,
			t_indicateur_serie.total,
			t_indicateur_serie.format,
			case when array_agg(DISTINCT t_indicateur_condition.external_code2) = ARRAY[null] then ARRAY[]::text[] else array_agg(DISTINCT t_indicateur_condition.external_code2) end  as external_code2,
    		CASE WHEN flatCodes.codes = ARRAY[null] THEN '{}'::text[] ELSE flatCodes.codes::text[] END as serie_code
		FROM pmsi.t_indicateur_serie
		LEFT JOIN pmsi.t_indicateurs ON t_indicateurs.oid = indicateur_id
		LEFT JOIN pmsi.t_indicateur_condition ON ARRAY[t_indicateur_condition.oid] <@ t_indicateur_serie.condition_id
		LEFT JOIN flatCodes on flatCodes.oid = t_indicateur_serie.oid
		GROUP BY 1,2,3,4,5,6,7,8, flatCodes.codes
		ORDER BY 2,1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			SELECT pmsi.cti_reorganize_indicateur_condition();

			TRUNCATE pmsi.t_indicateur_serie;

			INSERT INTO pmsi.t_indicateur_serie(indicateur_id, condition_id, name, color, formula, calc, serie_id, total, format, code)
				SELECT
					coalesce(t_indicateurs.oid, 0),
					case when array_agg(DISTINCT t_indicateur_condition.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT t_indicateur_condition.oid) end,
					w_dbsetup.name,
					w_dbsetup.color,
					w_dbsetup.formula,
					w_dbsetup.calc,
					case when array_agg(DISTINCT rec.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT rec.oid) end,
					w_dbsetup.total,
					w_dbsetup.format,
					w_dbsetup.code
			FROM w_dbsetup
			LEFT JOIN pmsi.t_indicateurs ON t_indicateurs.code = w_dbsetup.indicateur_code
			LEFT JOIN pmsi.t_indicateur_condition ON ARRAY[t_indicateur_condition.external_code2] <@ w_dbsetup.external_code2
			LEFT JOIN pmsi.t_indicateur_serie as rec ON ARRAY[rec.code] <@ w_dbsetup.serie_code
			GROUP BY 1,3,4,5,6,8,9,10
			;

			WITH series_id as (
			SELECT
					w_dbsetup.code,
					split_part(unnest(w_dbsetup.serie_code), ':', 1) as pos,
					split_part(unnest(w_dbsetup.serie_code), ':', 2) as serie_code
			FROM w_dbsetup
			), 

			full_series as (
			SELECT
				series_id.code,
				coalesce(string_agg('{"pos": ' || pos || ', "id": ' || t_indicateur_serie.oid || '}', '|'), '') as serie_id
			FROM series_id
			LEFT JOIN pmsi.t_indicateur_serie ON t_indicateur_serie.code = serie_code
			GROUP BY 1
			)
			

			UPDATE pmsi.t_indicateur_serie SET
				serie_id = CASE WHEN full_series.serie_id IS NULL THEN '' ELSE full_series.serie_id END
				FROM w_dbsetup
				LEFT JOIN full_series ON full_series.code = w_dbsetup.code
			WHERE t_indicateur_serie.code = w_dbsetup.code
			;

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_chart" label="Liste des graphiques">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_chart.type,
			t_chart.category,
			t_chart.name,
			t_chart.default_time_calc,
			t_chart.label_view_code,
			t_chart.code,
			array_agg(DISTINCT t_indicateur_serie.code) as serie_code
		FROM pmsi.t_chart
		LEFT JOIN pmsi.t_indicateur_serie ON ARRAY[t_indicateur_serie.oid] <@ t_chart.serie_id
		GROUP BY 1,2,3,4,5,6
		ORDER BY 1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_chart;
			
			INSERT INTO pmsi.t_chart(type, serie_id, category, name, default_time_calc, label_view_code, code)
				SELECT
					type,
					array_agg(DISTINCT t_indicateur_serie.oid),
					w_dbsetup.category,
					w_dbsetup.name,
					w_dbsetup.default_time_calc,
					w_dbsetup.label_view_code,
					w_dbsetup.code
				FROM w_dbsetup
				LEFT JOIN pmsi.t_indicateur_serie ON ARRAY[t_indicateur_serie.code] <@ w_dbsetup.serie_code
				GROUP BY 1,3,4,5,6,7
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard" label="Liste des dashboards">
		<selectCmd><![CDATA[ 
		
		SELECT
			code, label
		FROM pmsi.t_dashboard
		ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_dashboard;
			
			INSERT INTO pmsi.t_dashboard(code, label)
				SELECT
					w_dbsetup.code,
					w_dbsetup.label
				FROM w_dbsetup
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard_widget" label="Association des graphiques avec les dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			CASE WHEN t_dashboard_widget.oid = 0 THEN t_dashboard_widget.oid ELSE 1 END AS oid, 
			t_chart.code as chart_code,
			t_dashboard.code as dashboard_code,
			t_dashboard_widget.deprecated
		FROM pmsi.t_dashboard_widget
		LEFT JOIN pmsi.t_chart ON t_chart.oid = chart_id
		LEFT JOIN pmsi.t_dashboard ON t_dashboard.oid = dashboard_id
		ORDER BY 1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_dashboard_widget;
			
			INSERT INTO pmsi.t_dashboard_widget(dashboard_id, chart_id, deprecated)
				SELECT
					t_dashboard.oid,
					t_chart.oid,
					w_dbsetup.deprecated
				FROM w_dbsetup
				LEFT JOIN pmsi.t_chart ON t_chart.code = chart_code
				LEFT JOIN pmsi.t_dashboard ON t_dashboard.code = dashboard_code
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard_filter" label="Liste des filtres disponibles pour un dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			CASE WHEN t_dashboard_filter.oid = 0 THEN t_dashboard_filter.oid ELSE 1 END AS oid,
			t_dashboard.code as dashboard_code,
			t_indicateur_condition.external_code2
		FROM pmsi.t_dashboard_filter
		LEFT JOIN pmsi.t_dashboard ON t_dashboard.oid = dashboard_id
		LEFT JOIN pmsi.t_indicateur_condition ON t_indicateur_condition.oid = condition_id
		ORDER BY 2, 3;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_dashboard_filter;
			
			INSERT INTO pmsi.t_dashboard_filter(dashboard_id,condition_id)
				SELECT
					t_dashboard.oid,
					t_indicateur_condition.oid
				FROM w_dbsetup
				LEFT JOIN pmsi.t_dashboard ON t_dashboard.code = w_dbsetup.dashboard_code
				LEFT JOIN pmsi.t_indicateur_condition ON t_indicateur_condition.external_code2 = w_dbsetup.external_code2
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard_onglet" label="Onglets pour un meme dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			CASE WHEN t_dashboard_onglet.oid = 0 THEN t_dashboard_onglet.oid ELSE 1 END AS oid,
			t_dashboard.code as dashboard_code,
			name,
			index
		FROM pmsi.t_dashboard_onglet
		LEFT JOIN pmsi.t_dashboard ON t_dashboard.oid = dashboard_id
		ORDER BY 2, 4;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_dashboard_onglet;
			
			INSERT INTO pmsi.t_dashboard_onglet(dashboard_id, name, index)
				SELECT
					t_dashboard.oid,
					w_dbsetup.name,
					w_dbsetup.index
				FROM w_dbsetup
				LEFT JOIN pmsi.t_dashboard ON t_dashboard.code = dashboard_code
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_widget_user_parameter" label="Configs utilisateur des dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_widget_user_parameter.oid as uuid,
			t_dashboard.code || '|' || t_chart.code as dashboard_widget_code,
			username,
			title,
			x,
			y,
			w,
			h,
			time_calc,
			visible,
			t_dashboard.code || '|' || t_dashboard_onglet.index as tab_code,
			max_cat,
			array_agg(DISTINCT t_indicateur_serie.code) as include_series_code
		FROM pmsi.t_widget_user_parameter
		JOIN pmsi.t_dashboard_widget ON t_dashboard_widget.oid = dashboard_widget_id
		JOIN pmsi.t_dashboard ON t_dashboard.oid= t_dashboard_widget.dashboard_id
			JOIN pmsi.t_chart on t_chart.oid = t_dashboard_widget.chart_id
		JOIN pmsi.t_dashboard_onglet on t_dashboard_onglet.oid = tab_id
		LEFT JOIN pmsi.t_indicateur_serie ON ARRAY[t_indicateur_serie.oid] <@ include_series_id
		WHERE username = 'cti'
		GROUP BY 1,2,3,4,5,6,7,8,9,10,11, t_dashboard_onglet.index
		ORDER BY 2, 6, 7;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			DELETE FROM pmsi.t_widget_user_parameter WHERE username = 'cti';
			
			INSERT INTO pmsi.t_widget_user_parameter(
				oid, 
				dashboard_widget_id,
				username,
				title,
				x,
				y,
				w,
				h,
				time_calc,
				visible,
				tab_id,
				max_cat,
				include_series_id
			)
				SELECT
					w_dbsetup.uuid,
					t_dashboard_widget.oid,
					w_dbsetup.username,
					w_dbsetup.title,
					w_dbsetup.x,
					w_dbsetup.y,
					w_dbsetup.w,
					w_dbsetup.h,
					w_dbsetup.time_calc,
					w_dbsetup.visible,
					pmsi.t_dashboard_onglet.oid,
					w_dbsetup.max_cat,
					case when array_agg(DISTINCT t_indicateur_serie.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT t_indicateur_serie.oid) end
				FROM w_dbsetup
				JOIN pmsi.t_dashboard ON t_dashboard.code = SPLIT_PART(dashboard_widget_code, '|', 1)
				JOIN pmsi.t_chart on t_chart.code = SPLIT_PART(dashboard_widget_code, '|', 2)
				JOIN pmsi.t_dashboard_widget ON t_dashboard_widget.dashboard_id = t_dashboard.oid  and t_dashboard_widget.chart_id = t_chart.oid
				JOIN pmsi.t_dashboard_onglet ON t_dashboard_onglet.dashboard_id = t_dashboard.oid and t_dashboard_onglet.index = SPLIT_PART(tab_code, '|', 2)
				LEFT JOIN pmsi.t_indicateur_serie ON ARRAY[t_indicateur_serie.code] <@ include_series_code
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12
				ORDER BY 1
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude" label="Liste des études">
		<selectCmd><![CDATA[ 
		
		SELECT
			code, texte, base_request, clear_request, post_class
		FROM pmsi.t_etude
		ORDER BY 1
		;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_etude;
			
			INSERT INTO pmsi.t_etude(code, texte, base_request, clear_request, post_class)
				SELECT
					w_dbsetup.code,
					w_dbsetup.texte,
					w_dbsetup.base_request,
					w_dbsetup.clear_request,
					w_dbsetup.post_class
				FROM w_dbsetup
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_rule" label="Liste des règles des études">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_etude_rule.code, t_etude_rule.texte, t_etude_rule.json, t_etude.code as table_code
		FROM pmsi.t_etude_rule
		JOIN pmsi.t_etude ON t_etude.oid = t_etude_rule.etude_id
		ORDER BY 1
		;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE pmsi.t_etude_rule;
			
			INSERT INTO pmsi.t_etude_rule(code, texte, json, etude_id)
				SELECT
					w_dbsetup.code,
					w_dbsetup.texte,
					w_dbsetup.json,
					t_etude.oid
				FROM w_dbsetup
				JOIN pmsi.t_etude ON t_etude.code = w_dbsetup.table_code
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_etude_bariatrique" label="Liste des études bariatrique">
		<selectCmd><![CDATA[

			SELECT
				t_etude_bariatrique.cdc, t_etude_bariatrique.seuil, t_etude_bariatrique.section_code, t_etude_bariatrique.liste_code
			FROM pmsi.t_etude_bariatrique
			ORDER BY 1
			;

		]]></selectCmd>
		<updateCmd><![CDATA[

			TRUNCATE pmsi.t_etude_bariatrique;

			INSERT INTO pmsi.t_etude_bariatrique(cdc, seuil, section_code, liste_code)
				SELECT
					w_dbsetup.cdc,
					w_dbsetup.seuil,
					w_dbsetup.section_code,
					w_dbsetup.liste_code
				FROM w_dbsetup
			;
		]]></updateCmd>
	</TABLE>
</DBSETUPDATA>
