return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	sqlcmd text;
  	def_cursor refcursor;
  	_schemaname text;
  	result TEXT;
  BEGIN
  	-- Unités médicales
  	DROP TABLE IF EXISTS w_indicateur_condition;
  	CREATE TEMP TABLE w_indicateur_condition AS
  	SELECT 
  		'CTI_UM#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_UM#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'UM ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'unite_medicale_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'UM='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_unites_medicales_1 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'UM%' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')AND
  		v_source.oid <> 'UM0'
  	;
  
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_UMP#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_UMP#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'UM ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'unite_medicale_principale_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'UM_PRINCIPALE='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_unites_medicales_1 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'UM%' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')AND
  		v_source.oid <> 'UM0'
  	;
  
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_POLE#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_POLE#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Pole ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'pole_principal_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'POLE='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_pole_2 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_pole t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' 
  	;
  
  	-- Médecin RSS
    	INSERT INTO w_indicateur_condition
    	SELECT 
    		'CTI_MEDRSS#' || v_source.oid::text AS code,
    	v_source.code as condition_code,
    	'CTI_MEDRSS#' ||  
    		CASE 
    				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
    				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
    				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
    				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
    				ELSE '###' END AS category_id,
    		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Médecin ' || v_source.texte::text) AS texte,
    		v_source.texte AS texte_court,
    		CASE WHEN v_source.oid LIKE 'DT%' THEN 'medecin_reference_rss_id'::text ELSE 'medecin_rss_id' END AS column_name,
    		''::text AS rule,
    		v_source.oid::text AS value,
    		'MEDECIN_RSS='||v_source.oid AS view_select_rule,
    		ARRAY[
    			CASE 
    				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
    				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste ETS' 
    				ELSE '9' END,
    			CASE 
    				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
    				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
    				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
    				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
    				ELSE '###'||v_source.oid END
    				
    		] AS arbre_code,
    		ARRAY[
    			CASE 
    				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
    				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
    				ELSE 'Valeurs' END,
    			v_source.texte
    		] AS arbre_texte
    	FROM pmsi.v_medecins_3 v_source
    		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
    		t_classes_sections.oid::text = substr(v_source.oid,5)
    		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
    		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
    		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
    			t_listes.oid::text = substr(v_source.oid,3) AND
    			t_listes.code <> ''
    		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
    		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
    	WHERE v_source.oid NOT LIKE 'LC%' AND
    		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
    		v_source.oid <> '-1' AND
    		(v_source.oid LIKE 'DT%' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')AND
    		v_source.oid <> 'DT0'
    	;
  
  	-- Spécialité médecin RSS
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SPEMEDRSS#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_SPEMEDRSS#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Spécialité Médecin ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'specialite_interne_medecin_reference_rss_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'MEDECIN_RSS='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste ETS' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_medecins_2 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'SP%') AND
  		v_source.oid <> 'SP0'
  	;
  
  	-- Acte
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_ACTE#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_ACTE#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Acte ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'acte_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'ACTE='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_actes_1 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')
  	;
  
  	-- Médecin Exécutant
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_MEDEXE#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_MEDEXE#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Exécutant ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		CASE WHEN v_source.oid LIKE 'DT%' THEN 'medecin_reference_id'::text ELSE 'medecin_id'::text END AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'MEDECIN='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste ETS' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_medecins_4 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'DT%' OR v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')AND
  		v_source.oid <> 'DT0'
  	;
  
  	-- Spécialité médecin exécutant
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SPEMEDEXE#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_SPEMEDEXE#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Spécialité Exécutant ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'specialite_interne_medecin_reference_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'MEDECIN='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste ETS' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_medecins_4 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'D2%') AND
  		v_source.oid <> 'D20'
  	;
  
      -- GHM
      INSERT INTO w_indicateur_condition
      SELECT 
  		'CTI_GHM#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_GHM#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Acte ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'ghm_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'GHM='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_ghm_2 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')
  	UNION ALL
  	 SELECT 
  		'CTI_GHM#G' || t_source.oid::text AS code,
  	t_source.code as condition_code,
  	'CTI_GHM#G' AS category_id,
  		t_source.code || ' ' || t_source.texte AS texte,
  		t_source.code || ' ' || t_source.texte AS texte_court,
  		'ghm_id'::text AS column_name,
  		''::text AS rule,
  		'G' || t_source.oid AS value,
  		'GHM5=G'||t_source.oid AS view_select_rule,
  		ARRAY[
  			'8',
  			'###G'||t_source.code
  				
  		] AS arbre_code,
  		ARRAY[
  			'Racine',
  			t_source.code || ' ' || t_source.texte
  		] AS arbre_texte
  	FROM pmsi.t_ghm5 t_source
  UNION ALL
  	 SELECT 
  		'CTI_GHM#C' || t_source.cas_id::text AS code,
  		t_source.cas_code::text AS condition_code,
  		'CTI_GHM#C' AS category_id,
  		t_source.cas_code || ' ' || t_source.cas_texte AS texte,
  		t_source.cas_code || ' ' || t_source.cas_texte AS texte_court,
  		'ghm_id'::text AS column_name,
  		''::text AS rule,
  		'C' || t_source.cas_id::text AS value,
  		'CAS='||t_source.cas_id AS view_select_rule,
  		ARRAY[
  			'9',
  			'###C'||t_source.cas_code
  		] AS arbre_code,
  		ARRAY[
  			'C.A.S.',
  			t_source.cas_code || ' ' || t_source.cas_texte
  		] AS arbre_texte
  	FROM pmsi.v_ghm_5 t_source
  	group by 1,2,3,4,5,6,7,8,9
  	;
  
      -- Ages
      INSERT INTO w_indicateur_condition
      SELECT 
  		'CTI_AGE#' || v_source.oid::text AS code,
  	v_source.code as condition_code,
  	'CTI_AGE#' ||  
  		CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'DT%' THEN 'DT#'
  				ELSE '###' END AS category_id,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Acte ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'age'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'AGE='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN 'LI#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'SL%' THEN 'SL#'||t_listes_tables_listes.code||':'||CASE WHEN t_listes.code <> '' THEN t_listes.code ELSE '*TXT:'||upper(t_listes.texte) END
  				WHEN v_source.oid LIKE 'UM%' THEN COALESCE('UM#'||t_source.code,v_source.texte)
  				ELSE '###'||v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM pmsi.v_ages_2 v_source
  		LEFT JOIN pmsi.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN pmsi.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN pmsi.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN pmsi.t_listes_tables t_listes_tables_listes ON t_listes.table_id = t_listes_tables_listes.oid
  		LEFT JOIN pmsi.t_unites_medicales t_source ON v_source.oid LIKE 'UM%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		(v_source.oid LIKE 'LS%' OR v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%')
  	;
  
  	-- Sexe
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SEXE#DT1'::text AS code,
  	'DT1' as condition_code,
  		'CTI_SEXE#DT'::text AS category_id,
  		'Sexe masculin' AS texte,
  		'Sexe masculin'::text AS texte_court,
  		'sexe'::text AS column_name,
  		''::text AS rule,
  		'DT1'::text AS value,
  		'SEXE=DT1' AS view_select_rule,
  		ARRAY[
  			'9' ,
  			'DT#1'
  		] AS arbre_code,
  		ARRAY[
  			'Valeurs',
  			'Sexe masculin'
  		] AS arbre_texte
  	
  	;
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SEXE#DT2'::text AS code,
  		'DT2' as condition_code,
  		'CTI_SEXE#DT'::text AS category_id,
  		'Sexe féminin' AS texte,
  		'Sexe féminin'::text AS texte_court,
  		'sexe'::text AS column_name,
  		''::text AS rule,
  		'DT2'::text AS value,
  		'SEXE=DT2' AS view_select_rule,
  		ARRAY[
  			'9' ,
  			'DT#2'
  		] AS arbre_code,
  		ARRAY[
  			'Valeurs',
  			'Sexe féminin'
  		] AS arbre_texte
  	
  	;	
  
  	--Champ e-PMSI
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_CHAMP#EX'::text AS code,
  		'EX' as condition_code,
  		'CTI_CHAMP#EX'::text AS category_id,
  		'Transmis à e-PMSI' AS texte,
  		'Transmis à e-PMSI'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'EX0'::text AS value,
  		'v_rss_1.en_cours = ''0''' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EX#0'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			''
  		] AS arbre_texte
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXTR'::text AS code,
  		'EXTR' as condition_code,
  		'CTI_CHAMP#EXTR'::text AS category_id,
  		'Traités par e-PMSI' AS texte,
  		'Traités par e-PMSI'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''21'', ''22'', ''23'', ''24'', ''25'', ''26'', ''30'', ''31'', ''32'', ''33'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''21'', ''22'', ''23'', ''24'', ''25'', ''26'', ''30'', ''31'', ''32'', ''33'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXTR#0'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Traités par e-PMSI'
  		] AS arbre_texte	
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXVA'::text AS code,
  		'EXVA' as condition_code,
  		'CTI_CHAMP#EXVA'::text AS category_id,
  		'Valorisés par e-PMSI' AS texte,
  		'Valorisés par e-PMSI'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''30'', ''31'', ''32'', ''33'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''30'', ''31'', ''32'', ''33'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXVA#0'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Valorisés par e-PMSI'
  		] AS arbre_texte	
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXVA30'::text AS code,
  		'EXVA30' as condition_code,
  		'CTI_CHAMP#EXVA30'::text AS category_id,
  		'Valorisés par e-PMSI (AMO)' AS texte,
  		'Valorisés par e-PMSI (AMO)'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''30'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''30'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXVA#30'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Valorisés par e-PMSI (AMO)'
  		] AS arbre_texte
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXVA3032'::text AS code,
  		'EXVA3032' as condition_code,
  		'CTI_CHAMP#EXVA3032'::text AS category_id,
  		'Valorisés par e-PMSI (AMO+IVG)' AS texte,
  		'Valorisés par e-PMSI (AMO+IVG)'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''30'',''32'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''30'',''32'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXVA#3032'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Valorisés par e-PMSI (AMO+IVG)'
  		] AS arbre_texte	
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXVA31'::text AS code,
  		'EXVA31' as condition_code,
  		'CTI_CHAMP#EXVA31'::text AS category_id,
  		'Valorisés par e-PMSI (AME)' AS texte,
  		'Valorisés par e-PMSI (AME)'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''31'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''31'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXVA#31'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Valorisés par e-PMSI (AME)'
  		] AS arbre_texte
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXVA32'::text AS code,
  		'EXVA32' as condition_code,
  		'CTI_CHAMP#EXVA32'::text AS category_id,
  		'Valorisés par e-PMSI (IVG)' AS texte,
  		'Valorisés par e-PMSI (IVG)'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''32'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''32'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXVA#32'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Valorisés par e-PMSI (IVG)'
  		] AS arbre_texte
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXVA33'::text AS code,
  		'EXVA33' as condition_code,
  		'CTI_CHAMP#EXVA33'::text AS category_id,
  		'Valorisés par e-PMSI (SU)' AS texte,
  		'Valorisés par e-PMSI (SU)'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''33'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''33'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXVA#33'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Valorisés par e-PMSI (su)'
  		] AS arbre_texte
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXNVA'::text AS code,
  		'EXNVA' as condition_code,
  		'CTI_CHAMP#EXNVA'::text AS category_id,
  		'Traités non valorisés par e-PMSI' AS texte,
  		'Traités non valorisés par e-PMSI'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''21'', ''22'', ''23'', ''24'', ''25'', ''26'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''21'', ''22'', ''23'', ''24'', ''25'', ''26'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXNVA'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Traités non valorisés par e-PMSI'
  		] AS arbre_texte
  	UNION 
  	
  		SELECT 
  		'CTI_CHAMP#EXNTR'::text AS code,
  		'EXNTR' as condition_code,
  		'CTI_CHAMP#EXNTR'::text AS category_id,
  		'Transmis Non traités par e-PMSI' AS texte,
  		'Transmis Non traités par e-PMSI'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''11'', ''12'', ''13'', ''14'', ''15'')'::text AS value,
  		'v_rss_1.en_cours = ''0'' AND v_rss_1.traitement_epmsi IN (''11'', ''12'', ''13'', ''14'', ''15'')' AS view_select_rule,
  		ARRAY[
  			'10' ,
  			'EXNTR'
  		] AS arbre_code,
  		ARRAY[
  			'Transmis à e-PMSI',
  			' - Non traités par e-PMSI'
  		] AS arbre_texte
  	;
  
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_CHAMP#NEX'::text AS code,
  		'NEX' as condition_code,
  		'CTI_CHAMP#NEX'::text AS category_id,
  		'Non Transmis à e-PMSI' AS texte,
  		'Non Transmis à e-PMSI'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'SF0'::text AS value,
  		'v_rss_1.en_cours <> ''0''' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'SF#0'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			''
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXSO'::text AS code,
  		'NEXSO' as condition_code,
  		'CTI_CHAMP#NEXSO'::text AS category_id,
  		'Uniquement sortis non groupés non transmis' AS texte,
  		'Sortis non groupés non transmis'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''S'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''S'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXSO'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Sortis non groupés non transmis'
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXGR'::text AS code,
  		'NEXGR' as condition_code,
  		'CTI_CHAMP#NEXGR'::text AS category_id,
  		'Uniquement groupés non transmis' AS texte,
  		'groupés non transmis'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''G'',''V'',''F'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''G'',''V'',''F'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXGR'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Groupés non transmis'
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXVANFA'::text AS code,
  		'NEXVANFA' as condition_code,
  		'CTI_CHAMP#NEXVANFA'::text AS category_id,
  		'Uniquement validés non facturés' AS texte,
  		'Validés non facturés'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''V'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''V'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXVANFA'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Validés non facturés'
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXVA'::text AS code,
  		'NEXVA' as condition_code,
  		'CTI_CHAMP#NEXVA'::text AS category_id,
  		'Uniquement validés non transmis' AS texte,
  		'Validés non transmis'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''V'' ,''F'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''V'',''F'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXVA'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Validés non transmis'
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXFA'::text AS code,
  		'NEXFA' as condition_code,
  		'CTI_CHAMP#NEXFA'::text AS category_id,
  		'Uniquement facturés non transmis' AS texte,
  		'Facturés non transmis'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''F'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''F'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXFA'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Facturés non transmis'
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXGRNFA'::text AS code,
  		'NEXGRNFA' as condition_code,
  		'CTI_CHAMP#NEXGRNFA'::text AS category_id,
  		'Uniquement groupés non facturés non transmis' AS texte,
  		'Groupés non facturés non transmis'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''G'',''V'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''G'',''V'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXGRNFA'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Groupés non facturés non transmis'
  		] AS arbre_texte
  	UNION
  		SELECT 
  		'CTI_CHAMP#NEXNFA'::text AS code,
  		'NEXNFA' as condition_code,
  		'CTI_CHAMP#NEXNFA'::text AS category_id,
  		'Uniquement non facturés non transmis' AS texte,
  		'Non facturés non transmis'::text AS texte_court,
  		'v_rss_1.en_cours'::text AS column_name,
  		''::text AS rule,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''S'',''G'',''V'')'::text AS value,
  		'v_rss_1.en_cours <> ''0'' AND v_rss_1.en_cours_etat IN (''S'',''G'',''V'')' AS view_select_rule,
  		ARRAY[
  			'20' ,
  			'NEXGRNFA'
  		] AS arbre_code,
  		ARRAY[
  			'Non Transmis à e-PMSI',
  			'- Non facturés non transmis'
  		] AS arbre_texte
  	;
  
  	-- Changement de condition sur specialites et medecins
    	UPDATE pmsi.t_indicateur_condition
    	SET code = replace(code, '#SP','#D2')
    	WHERE code LIKE '%SPEMEDRSS%' AND
    		code LIKE '%#SP%'
    	;
  
    	UPDATE pmsi.t_indicateur_condition
    	SET code = replace(code, '#MD','#DT')
    	WHERE code LIKE '%MEDRSS%' AND
    		code LIKE '%#MD%'
    	;	
  		
  	INSERT INTO pmsi.t_indicateur_condition(code, texte,texte_court,column_name,rule,value,view_select_rule, arbre_code, arbre_texte, condition_code, category_id)
  	SELECT code, texte,texte_court,column_name,rule,value,view_select_rule, arbre_code, arbre_texte, condition_code, category_id
  	FROM w_indicateur_condition
  	WHERE code NOT IN (SELECT code FROM pmsi.t_indicateur_condition WHERE code IS NOT NULL)
  	;
  
  	UPDATE pmsi.t_indicateur_condition SET
  		texte = w_indicateur_condition.texte,
  		texte_court = w_indicateur_condition.texte_court,
  		column_name = w_indicateur_condition.column_name,
  		rule = w_indicateur_condition.rule,
  		value = w_indicateur_condition.value,
  		view_select_rule = w_indicateur_condition.view_select_rule,
  		arbre_code = w_indicateur_condition.arbre_code,
  		arbre_texte = w_indicateur_condition.arbre_texte,
  		condition_code = w_indicateur_condition.condition_code,
  		category_id = w_indicateur_condition.category_id
  	FROM w_indicateur_condition
  	WHERE w_indicateur_condition.code = t_indicateur_condition.code AND
  		(
  		t_indicateur_condition.texte IS DISTINCT FROM w_indicateur_condition.texte OR
  		t_indicateur_condition.texte_court IS DISTINCT FROM w_indicateur_condition.texte_court OR
  		t_indicateur_condition.column_name IS DISTINCT FROM w_indicateur_condition.column_name OR
  		t_indicateur_condition.rule IS DISTINCT FROM w_indicateur_condition.rule OR
  		t_indicateur_condition.value IS DISTINCT FROM w_indicateur_condition.value OR
  		t_indicateur_condition.view_select_rule IS DISTINCT FROM w_indicateur_condition.view_select_rule OR
  		t_indicateur_condition.arbre_code IS DISTINCT FROM w_indicateur_condition.arbre_code OR
  		t_indicateur_condition.arbre_texte IS DISTINCT FROM w_indicateur_condition.arbre_texte OR
  		t_indicateur_condition.condition_code IS DISTINCT FROM w_indicateur_condition.condition_code OR
  		t_indicateur_condition.category_id IS DISTINCT FROM w_indicateur_condition.category_id
  		)
  	;
  
  	DELETE FROM pmsi.t_indicateur_condition
  	WHERE code LIKE 'CTI%' AND
  		code NOT IN (SELECT code FROM w_indicateur_condition)
  		;
  
  	UPDATE pmsi.t_indicateur_condition SET
  		column_texte = CASE column_name
  		        WHEN 'unite_medicale_id' THEN 'Unité médicale'
  		        WHEN 'unite_medicale_principale_id' THEN 'Unité médicale principale'
  		        WHEN 'medecin_rss_id' THEN 'Médecin RSS'
  		        WHEN 'medecin_reference_rss_id' THEN 'Médecin RSS'
  		        WHEN 'specialite_interne_medecin_reference_rss_id' THEN 'Spécialité Médecin RSS'
  		        WHEN 'medecin_reference_id' THEN 'Exécutant'
  		        WHEN 'medecin_id' THEN 'Exécutant'
  		        WHEN 'specialite_interne_medecin_reference_id' THEN 'Spécialité Exécutant'
  		        WHEN 'ghm5_id' THEN 'Ghm'
  		        WHEN 'v_rss_1.en_cours' THEN 'Champ e-PMSI'
                  WHEN 'sexe' THEN 'Sexe'
                  WHEN 'age' THEN 'Age'
  			ELSE replace(Upper(substr(split_part(column_name,'_id',1),1,1)) || substr(split_part(column_name,'_id',1),2),'_',' ') END
  	WHERE 
  		column_texte IS DISTINCT FROM 
  			CASE column_name
  			WHEN 'unite_medicale_id' THEN 'Unité médicale'
  			WHEN 'unite_medicale_principale_id' THEN 'Unité médicale principale'
  			WHEN 'medecin_rss_id' THEN 'Médecin RSS'
  		        WHEN 'specialite_interne_medecin_reference_rss_id' THEN 'Spécialité Médecin RSS'
  		        WHEN 'medecin_reference_id' THEN 'Exécutant'
  		        WHEN 'specialite_interne_medecin_reference_id' THEN 'Spécialité Exécutant'
  		        WHEN 'ghm5_id' THEN 'Ghm'
  		        WHEN 'v_rss_1.en_cours' THEN 'Champ e-PMSI'
                  WHEN 'sexe' THEN 'Sexe'
  			ELSE replace(Upper(substr(split_part(column_name,'_id',1),1,1)) || substr(split_part(column_name,'_id',1),2),'_',' ') END
  	;
  
  	UPDATE pmsi.t_indicateur_condition
  	SET external_code = split_part(code,'#',1)||'#'||arbre_code[2]
  	WHERE external_code IS DISTINCT FROM (split_part(code,'#',1)||'#'||arbre_code[2])
  	;
  
  WITH external_code2 as (
      SELECT
        oid,
        split_part(code, '#', 1)
          ||'#'
          ||arbre_code[2]
          as new_external_code2
      FROM pmsi.t_indicateur_condition
    )
    UPDATE pmsi.t_indicateur_condition SET
      external_code2 = new_external_code2
    FROM external_code2
    WHERE 1=1
      and t_indicateur_condition.oid = external_code2.oid
      and external_code2 IS DISTINCT FROM new_external_code2
    ;
  
  	
  	DROP TABLE IF EXISTS w_indicateur_condition_table;
  	CREATE TEMP TABLE w_indicateur_condition_table AS
  	SELECT t_indicateur_condition.oid AS indicateur_condition_id,
  		''::text As table_name,
  	CASE
  		WHEN t_indicateur_condition.value LIKE 'G%' THEN 'ghm5_id'
  		WHEN t_indicateur_condition.value LIKE 'C%' THEN 'cas_id'
  		WHEN t_indicateur_condition.value LIKE 'v_rss_1.%' THEN ''
  		ELSE t_indicateur_condition.column_name END || ' ' ||
  	CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN '= ANY(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN '= ANY(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN '<> ALL(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
  			WHEN t_indicateur_condition.value LIKE 'SF%' THEN '<>'
  			WHEN t_indicateur_condition.value LIKE 'v_rss_1.%' THEN ''
  			ELSE '='
  		END || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
  				'(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'pmsi' ELSE 'base' END ||'.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
  				'(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'pmsi' ELSE 'base' END ||'.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
  				'(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'pmsi' ELSE 'base' END ||'.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
  			WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
  				substr(value,3)
  			WHEN t_indicateur_condition.value LIKE 'G%' THEN 
  				substr(value,2)
  		WHEN t_indicateur_condition.value LIKE 'C%' THEN
  					substr(value,2)
  		WHEN t_indicateur_condition.value LIKE 'v_rss_1.%' THEN
  					trim(value)
  		ELSE substr(value,3)
  			END || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN ')'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN ')'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN ')'
  			ELSE ''
  		END AS check_where
  	FROM pmsi.t_indicateur_condition
  	WHERE t_indicateur_condition.code LIKE 'CTI%' 
  	;
  
  	INSERT INTO w_indicateur_condition_table
  	SELECT t_indicateur_condition.oid AS indicateur_condition_id,
  		'V_RSS_1'::text As table_name,
  		'unite_medicale_principale_id' || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN '= ANY(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN '= ANY(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN '<> ALL(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
  			ELSE '='
  		END || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
  				'(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'pmsi' ELSE 'base' END ||'.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
  				'(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'pmsi' ELSE 'base' END ||'.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
  				'(SELECT to_id FROM ' || CASE WHEN t_indicateur_condition.column_name <> 'finess_id' THEN 'pmsi' ELSE 'base' END ||'.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
  			WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
  				substr(value,3)
  			ELSE substr(value,3)
  		END || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN ')'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN ')'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN ')'
  			ELSE ''
  		END AS check_where
  	FROM pmsi.t_indicateur_condition
  	WHERE t_indicateur_condition.code LIKE 'CTI%' AND column_name = 'unite_medicale_id'
  	;
  
    INSERT INTO w_indicateur_condition_table
    SELECT indicateur_condition_id, 'V_RSS_RUM_1' AS table_name, replace(check_where,'pole_principal_id','pole_rum_id')
    FROM w_indicateur_condition_table
    WHERE check_where LIKE 'pole_principal%'
    ;
          
  
  	INSERT INTO pmsi.t_indicateur_condition_table(indicateur_condition_id, table_name, check_where)
  	SELECT indicateur_condition_id, table_name, check_where
  	FROM w_indicateur_condition_table
  	WHERE (indicateur_condition_id::text || '-' || table_name) NOT IN
  		(SELECT indicateur_condition_id::text || '-' || table_name FROM pmsi.t_indicateur_condition_table WHERE table_name IS NOT NULL)
  	;
  
  	UPDATE pmsi.t_indicateur_condition_table SET
  		check_where = w_indicateur_condition_table.check_where
  	FROM w_indicateur_condition_table
  	WHERE t_indicateur_condition_table.indicateur_condition_id = w_indicateur_condition_table.indicateur_condition_id AND
  		t_indicateur_condition_table.table_name = w_indicateur_condition_table.table_name AND
  		(
  		t_indicateur_condition_table.check_where IS DISTINCT FROM w_indicateur_condition_table.check_where 
  		)
  	;
  
  	DELETE FROM pmsi.t_indicateur_condition_table
  	USING pmsi.t_indicateur_condition
  	WHERE t_indicateur_condition.oid = t_indicateur_condition_table.indicateur_condition_id AND
  		t_indicateur_condition.code LIKE 'CTI%' AND
  		(indicateur_condition_id::text || '-' || table_name) NOT IN
  		(SELECT indicateur_condition_id::text || '-' || table_name FROM w_indicateur_condition_table WHERE table_name IS NOT NULL)
  	;
  
  	RETURN 'OK';
  
  END;