return: SETOF record
lang: plpgsql
parameters:
  p0:
    type: text
    name: i_option
src: |
  DECLARE	
  
  	_view_type text;
  	_view_name text;
  	_view_name_default text;
  	_columns_array text[];
  	_columns_array_justif text[];
  
  	row  RECORD;
  BEGIN
  
  	--RAISE NOTICE '%' , 'DEBUG 0 : ' || clock_timestamp()::text;  
  
  	DROP TABLE IF EXISTS w_cti_get_columns_options_decoup;
  	CREATE temp TABLE w_cti_get_columns_options_decoup AS
  	SELECT i, substr(replace(replace(replace(i_option,chr(9),' '),chr(10),' '),chr(13),' '),i,1) AS car, 0::integer AS level, 0::integer AS key_index
  	FROM generate_series(1, length(i_option)) i 
  	;
  
  	ANALYSE w_cti_get_columns_options_decoup;
  
  	UPDATE w_cti_get_columns_options_decoup
  	SET level = subview.level
  	FROM
  	(
  	SELECT w_cti_get_columns_options_decoup.i, w_cti_get_columns_options_decoup.car,
  		SUM(CASE WHEN w_cti_get_columns_options_decoup_1.car = '[' THEN 1 ELSE 0 END) AS level
  	FROM w_cti_get_columns_options_decoup
  	LEFT JOIN w_cti_get_columns_options_decoup w_cti_get_columns_options_decoup_1 ON w_cti_get_columns_options_decoup_1.car = '[' AND w_cti_get_columns_options_decoup_1.i <= w_cti_get_columns_options_decoup.i
  	GROUP BY 1,2
  	ORDER BY 1
  	) subview
  	WHERE w_cti_get_columns_options_decoup.i = subview.i
  	;
  
  	UPDATE w_cti_get_columns_options_decoup
  	SET level = w_cti_get_columns_options_decoup.level - subview.level
  	FROM
  	(
  	SELECT w_cti_get_columns_options_decoup.i, w_cti_get_columns_options_decoup.car,
  		SUM(CASE WHEN w_cti_get_columns_options_decoup_1.car = ']' THEN 1 ELSE 0 END) AS level
  	FROM w_cti_get_columns_options_decoup
  	LEFT JOIN w_cti_get_columns_options_decoup w_cti_get_columns_options_decoup_1 ON w_cti_get_columns_options_decoup_1.car = ']' AND w_cti_get_columns_options_decoup_1.i < w_cti_get_columns_options_decoup.i
  	GROUP BY 1,2
  	ORDER BY 1
  	) subview
  	WHERE w_cti_get_columns_options_decoup.i = subview.i
  	;
  
  
  	UPDATE w_cti_get_columns_options_decoup
  	SET key_index = subview.key_index
  	FROM
  	(
  	SELECT w_cti_get_columns_options_decoup.i, w_cti_get_columns_options_decoup.car,
  		COALESCE(SUM(CASE WHEN w_cti_get_columns_options_decoup_1.car = '[' THEN 1 ELSE 0 END),0) AS key_index
  	FROM w_cti_get_columns_options_decoup
  	LEFT JOIN w_cti_get_columns_options_decoup w_cti_get_columns_options_decoup_1 ON w_cti_get_columns_options_decoup_1.level = 1 AND w_cti_get_columns_options_decoup_1.i <= w_cti_get_columns_options_decoup.i AND w_cti_get_columns_options_decoup_1.car = '['
  	GROUP BY 1,2
  	) subview
  	WHERE w_cti_get_columns_options_decoup.i = subview.i
  	;
  
  
  
  	DROP TABLE IF EXISTS w_cti_get_columns_options;
  	CREATE TEMP TABLE w_cti_get_columns_options AS
  	SELECT
  			trim(substr(option,1,position('=' IN option)-1)) AS property,
  			trim(substr(option,position('=' IN option)+1)) AS property_value
  	FROM
  	(
  		SELECT key_index, array_to_string(base.cti_array_accum(car),'') AS option
  		FROM
  		(
  			SELECT *
  			FROM w_cti_get_columns_options_decoup
  			WHERE key_index <> 0 AND
  				(car NOT IN ('[',']') OR level <> 1)
  			ORDER BY i
  		) subview
  		group by 1
  		order by 1
  	) subview
  	WHERE option LIKE '%=%'
  	;
  
  	ANALYSE w_cti_get_columns_options
  	;
  
  
  	_view_type = (SELECT COALESCE(MAX(property_value),'ANZ') FROM w_cti_get_columns_options WHERE property = 'VIEWTYPE')
  	;
  	_view_name = (SELECT COALESCE(MAX(property_value),'*') FROM w_cti_get_columns_options WHERE property = 'VIEWNAME')
  	;
  	IF _view_name <> '*' AND _view_name NOT LIKE '%|%' AND NOT EXISTS (SELECT code FROM eco.t_rapports WHERE code = _view_name) THEN
  		_view_name = (
  			SELECT COALESCE(MIN(t_rapports.code),'*')
  			FROM w_cti_get_columns_options
  			JOIN eco.t_rapports ON t_rapports.code LIKE 'CTI\_'||CASE _view_type WHEN 'JUSTIF' THEN 'LIST' ELSE 'VIEW' END || '\_'||property_value||'%'
  			WHERE property = 'CONTEXTE'
  			)
  		;
  	END IF;
  
  	_columns_array = string_to_array((SELECT COALESCE(MAX(property_value),'') FROM w_cti_get_columns_options WHERE property = 'COLUMNS'),'|')
  	;
  
  
  
  	IF _view_type = 'JUSTIF' THEN
  		_columns_array_justif = string_to_array((SELECT COALESCE(MAX(property_value),'') FROM w_cti_get_columns_options WHERE property = 'COLUMNS'),'|')
  		;
  	END IF;
  
  
  	DROP TABLE IF EXISTS w_data_column_condition;
  	CREATE TEMP TABLE w_data_column_condition AS
  	SELECT 
  		1::numeric AS condition_index, 
  		''::text AS condition_code, 
  		''::text AS condition_formula,
  		0::numeric AS condition_nb
  	WHERE _view_type <> 'COMP'
  	UNION
  	SELECT ROW_NUMBER() OVER (ORDER BY i) + 1 AS condition_index, 
  		to_char(i, 'FM00'), 
  		CASE WHEN i = 0 THEN 'columnby_sequence < 1' ELSE 'columnby_sequence = '||i END::text AS condition_formula,
  		0::numeric AS condition_nb
  	FROM generate_series(0,(SELECT base.cti_to_number(property_value) FROM w_cti_get_columns_options WHERE property = 'SQL_COLUMNBY_COUNT')::integer) AS i
  	WHERE _view_type = 'EVOL'
  	UNION
  	SELECT ROW_NUMBER() OVER (ORDER BY i) + 1 AS condition_index, 
  		to_char(i, 'FM00'), 
  		'columnby_sequence = '||i AS condition_formula,
  		0::numeric AS condition_nb
  	FROM generate_series(1,(SELECT base.cti_to_number(property_value)+1 FROM w_cti_get_columns_options WHERE property = 'SQL_COLUMNBY_COUNT')::integer) AS i
  	WHERE _view_type = 'CROSS'
  	UNION
  	SELECT ROW_NUMBER() OVER (ORDER BY i) + 1 AS condition_index, 
  		to_char(i, 'FM00'), 
  		'columnby_sequence = '||i AS condition_formula,
  		0::numeric AS condition_nb
  	FROM generate_series(1,(SELECT base.cti_to_number(property_value) FROM w_cti_get_columns_options WHERE property = 'SQL_COLUMNBY_COUNT')::integer) AS i
  	WHERE _view_type = 'EVOLCOMP'
  	UNION
  	SELECT ROW_NUMBER() OVER (ORDER BY i)  AS condition_index, 
  		to_char(i, 'FM00'), 
  		CASE WHEN i <= 8 THEN 'columnby_sequence = '||i ELSE '' END AS condition_formula,
  		0::numeric AS condition_nb
  	FROM generate_series(1,20) AS i
  	WHERE _view_type = 'COMP'
  	ORDER BY 1
  	;
  
  	UPDATE w_data_column_condition
  	SET condition_nb = (SELECT count(*) FROM w_data_column_condition)
  	;
  
  
  	DROP TABLE IF EXISTS w_data_column_selected_code;
  
  	IF _view_name = '*' THEN
  		CREATE TEMP TABLE w_data_column_selected_code AS
  		SELECT 0::bigint AS rapport_id,
  			-1::bigint AS rapport_index,
  			i AS rapport_numero_rubrique, 
  			CASE WHEN column_codes[i] NOT LIKE '%::%' THEN column_codes[i] WHEN split_part(column_codes[i],'::',2) NOT LIKE '*CALC%' THEN split_part(column_codes[i],'::',2) ELSE 'CTI_RAPPORTCALC' END AS selected_column_code, 
  			CASE WHEN split_part(column_codes[i],'::',3) <> '' THEN string_to_array(split_part(column_codes[i],'::',3),',')::bigint[] ELSE NULL END AS selected_column_condition_array,
  			CASE 
  				WHEN column_codes[i] LIKE '*CALC%' THEN substr(column_codes[i],6) ELSE ''::text END AS dg_rapport_calc_formula,
  			CASE WHEN column_codes[i] LIKE '*CALC%' THEN 'CTI_RAPPORTCALC' ELSE column_codes[i] END ||CASE WHEN _view_type = 'COMP' THEN to_char(condition_index,'FM000') ELSE '' END AS dg_rapport_field_tag,
  			condition_index, 
  			((i-1)*condition_nb)+condition_index AS dg_column_index, 
  			((row_number() OVER (ORDER BY i)-1)*condition_nb) + condition_index AS dg_column_index_old,
  			0::bigint AS indicateur_id,
  			0::text AS is_hide
  		FROM
  		(SELECT _columns_array AS column_codes) subview1
  		JOIN generate_series(1,30) AS i ON column_codes[i] <> '' AND column_codes[i] <> '-1' AND column_codes[i] <> '*' AND column_codes[i] <> ''
  		JOIN w_data_column_condition ON 1=1
  		ORDER BY 3
  		;
  
  		INSERT INTO w_data_column_selected_code
  		SELECT rapport_id, 
  			-1,
  			rapport_numero_rubrique, 
  			t_indicateurs.code, 
  			NULL::bigint[] AS selected_column_condition_array, 
  			'', 
  			t_indicateurs.code, 
  			condition_index, 
  			0, 
  			0,
  			t_indicateurs.oid AS indicateur_id,
  			NULL::text AS is_hide
  		FROM w_data_column_selected_code
  		JOIN eco.t_indicateurs ON dg_rapport_calc_formula LIKE '%['||t_indicateurs.code||']%'
  		WHERE dg_rapport_calc_formula <> '' AND
  			t_indicateurs.code NOT IN (SELECT selected_column_code FROM w_data_column_selected_code WHERE dg_rapport_calc_formula = '')
  		;
  
  	ELSE 
  		IF _view_name NOT LIKE '%|%' THEN
  			CREATE TEMP TABLE w_data_column_selected_code AS
  			SELECT t_rapports.oid AS rapport_id,
  				0::bigint AS rapport_index,
  				numero_rubrique AS rapport_numero_rubrique, 
  				t_indicateurs.code AS selected_column_code, 
  				array_cat(CASE WHEN erase_rapport_indicateur_condition IS DISTINCT FROM '1' THEN t_rapports.rapport_indicateur_condition_id ELSE NULL END,indicateur_condition_id) selected_column_condition_array,
  				t_rapports_rubriques.calc_formula AS dg_rapport_calc_formula,
  				CASE WHEN t_rapports_rubriques.field_tag <> '' THEN t_rapports_rubriques.field_tag ELSE t_indicateurs.code END AS dg_rapport_field_tag,
  				condition_index, 
  				((ROW_NUMBER() OVER (ORDER BY rapport_id,NUMERO_RUBRIQUE, condition_index))) AS dg_column_index,
  				((ROW_NUMBER() OVER (ORDER BY rapport_id,NUMERO_RUBRIQUE)-1)*condition_nb) + condition_index AS dg_column_index_old,
  				t_indicateurs.oid AS indicateur_id,
  				CASE 
  					WHEN t_rapports_rubriques.is_hide = '1' THEN '1'
  					WHEN _view_type IN ('EVOL','EVOLCOMP','COMP') AND t_rapports_rubriques.calc_formula LIKE '%\%T(%' THEN '1'
  					WHEN _view_type IN ('EVOL','EVOLCOMP','COMP') AND t_rapports_rubriques.calc_formula LIKE '%T.%' THEN '1'
  					ELSE '0' END AS is_hide
  			FROM eco.t_rapports
  			JOIN eco.t_rapports_rubriques ON rapport_id = t_rapports.oid
  			JOIN eco.t_indicateurs ON indicateur_id = t_indicateurs.oid
  			JOIN w_data_column_condition ON 1=1
  			WHERE t_rapports.code = _view_name 
  			;
  		ELSE 
  			CREATE TEMP TABLE w_data_column_selected_code AS
  			SELECT t_rapports.oid AS rapport_id,
  				array_position(string_to_array(_view_name,'|'), t_rapports.code) AS rapport_index,
  				numero_rubrique AS rapport_numero_rubrique, 
  				t_indicateurs.code AS selected_column_code, 
  				array_cat(CASE WHEN erase_rapport_indicateur_condition IS DISTINCT FROM '1' THEN t_rapports.rapport_indicateur_condition_id ELSE NULL END,indicateur_condition_id) selected_column_condition_array,
  				t_rapports_rubriques.calc_formula AS dg_rapport_calc_formula,
  				CASE WHEN t_rapports_rubriques.field_tag <> '' THEN t_rapports_rubriques.field_tag ELSE t_indicateurs.code END AS dg_rapport_field_tag,
  				condition_index, 
  				(array_position(string_to_array(_view_name,'|'), t_rapports.code) - 1) * 30 + ((ROW_NUMBER() OVER (PARTITION BY rapport_id ORDER BY NUMERO_RUBRIQUE, condition_index))) AS dg_column_index,
  				(array_position(string_to_array(_view_name,'|'), t_rapports.code) - 1) * 30 + ((ROW_NUMBER() OVER (PARTITION BY rapport_id ORDER BY NUMERO_RUBRIQUE)-1)*condition_nb) + condition_index AS dg_column_index_old,
  				t_indicateurs.oid AS indicateur_id,
  				CASE 
  					WHEN t_rapports_rubriques.is_hide = '1' THEN '1'
  					WHEN _view_type IN ('EVOL','EVOLCOMP','COMP') AND t_rapports_rubriques.calc_formula LIKE '%\%T(%' THEN '1'
  					WHEN _view_type IN ('EVOL','EVOLCOMP','COMP') AND t_rapports_rubriques.calc_formula LIKE '%T.%' THEN '1'
  					ELSE '0' END AS is_hide
  			FROM eco.t_rapports
  			JOIN eco.t_rapports_rubriques ON rapport_id = t_rapports.oid
  			JOIN eco.t_indicateurs ON indicateur_id = t_indicateurs.oid
  			JOIN w_data_column_condition ON 1=1
  			WHERE t_rapports.code = ANY(string_to_array(_view_name,'|'))
  			ORDER BY t_rapports.code, rapport_numero_rubrique
  			;
  		END IF ;
  
  	END IF ;
  
  
  
  	-- Si justificatif et une indicateur donné, enlever les indicateurs et ajouter l'indicateur donné
  	/*IF _view_type = 'JUSTIF' THEN
  		IF _columns_array_justif[1] != '' AND _columns_array_justif[1] != '-1' AND _columns_array_justif[1] != '*DFT' THEN
  			DELETE FROM w_data_column_selected_code
  			USING eco.t_indicateurs
  			WHERE indicateur_id = t_indicateurs.oid AND
  				t_indicateurs.total_function <> '*LIST'
  			;
  
  			INSERT INTO w_data_column_selected_code
  			SELECT rapport_id, 0 ,MAX(rapport_numero_rubrique) + i, t_indicateurs.code, NULL, '', t_indicateurs.code, MAX(condition_index), MAX(rapport_numero_rubrique) + i, MAX(rapport_numero_rubrique) + i,
  				t_indicateurs.oid AS indicateur_id, '0'
  			FROM w_data_column_selected_code
  			JOIN (SELECT _columns_array_justif AS column_codes) subview1 ON 1=1
  			JOIN generate_series(1,30) AS i ON column_codes[i] <> '' 
  			JOIN eco.t_indicateurs ON t_indicateurs.code = column_codes[i]
  			GROUP BY rapport_id,i,t_indicateurs.code,t_indicateurs.oid
  			;
  		END IF;
  	END IF;*/
  
  	-- Si filtre indicateur et vue justificative ou analyse, insérer l'indicateur donné en filtre ou le décaler en premier
  	/*IF _view_type = 'JUSTIF' OR _view_type = 'ANZ' THEN
  		UPDATE w_data_column_selected_code
  		SET 
  			rapport_numero_rubrique = CASE 
  				WHEN sub.rapport_id = w_data_column_selected_code.rapport_id AND COALESCE(sub.rapport_numero_rubrique,30) > w_data_column_selected_code.rapport_numero_rubrique THEN w_data_column_selected_code.rapport_numero_rubrique + 1
  				WHEN sub.rapport_id = w_data_column_selected_code.rapport_id AND COALESCE(sub.rapport_numero_rubrique,30) = w_data_column_selected_code.rapport_numero_rubrique THEN 1
  				ELSE w_data_column_selected_code.rapport_numero_rubrique END,
  			dg_column_index = CASE 
  				WHEN sub.rapport_id = w_data_column_selected_code.rapport_id AND COALESCE(sub.rapport_numero_rubrique,30) > w_data_column_selected_code.rapport_numero_rubrique THEN w_data_column_selected_code.dg_column_index + 1
  				WHEN sub.rapport_id = w_data_column_selected_code.rapport_id AND COALESCE(sub.rapport_numero_rubrique,30) = w_data_column_selected_code.rapport_numero_rubrique THEN w_data_column_selected_code.dg_column_index/30 +1
  				ELSE w_data_column_selected_code.dg_column_index END
  		FROM w_data_column_selected_code ref LEFT JOIN
  		(
  			SELECT rapport_id, rapport_numero_rubrique 
  			FROM w_data_column_selected_code 
  			WHERE selected_column_code = (SELECT property_value FROM w_cti_get_columns_options WHERE property = 'FILTER_IND')
  		) sub ON sub.rapport_id = ref.rapport_id
  		WHERE ref.rapport_id = w_data_column_selected_code.rapport_id AND ref.rapport_numero_rubrique = w_data_column_selected_code.rapport_numero_rubrique 
  		;
  
  		INSERT INTO w_data_column_selected_code
  			SELECT rapport_id,
  				rapport_index ,
  				1, 
  				t_indicateurs.code, 
  				NULL,
  				'',
  				t_indicateurs.code, 
  				0, 
  				0, 
  				0,
  				t_indicateurs.oid AS indicateur_id,
  				NULL::text AS is_hide
  			FROM w_data_column_selected_code
  			JOIN eco.t_indicateurs ON selected_column_code = t_indicateurs.code
  			WHERE (rapport_id, t_indicateurs.code) NOT IN (SELECT rapport_id,selected_column_code FROM w_data_column_selected_code )
  			;
  	END IF;*/
  
  	DROP TABLE IF EXISTS temp.w_data_column_selected_code;
  	CREATE TABLE temp.w_data_column_selected_code AS SELECT * FROM w_data_column_selected_code;
  
  
  
  	DROP TABLE IF EXISTS w_data_column_code;
  	CREATE TEMP TABLE w_data_column_code AS
  	SELECT subview.rapport_id,
  		subview.rapport_index,
  		column_code_portee[i] AS column_code,
  		subview.condition_index,
  		indicateur_condition_array,
  		COALESCE(dg_column_index,0) AS dg_column_index,
  		COALESCE(rapport_numero_rubrique,0) AS rapport_numero_rubrique,
  		0::integer AS db_column_index,
  		MAX(w_data_column_selected_code.is_hide) AS is_hide,
  		MAX(CASE WHEN i = 21 AND table_name  = '*CALC' THEN column_code_portee ELSE NULL END) AS dg_portee,
  		''::text AS table_name,
  		''::text AS dimension_date,
  		''::text AS total_function,
  		''::text AS db_column_name,
  		''::text AS dg_head,
  		''::text AS dg_largehead,
  		''::text AS dg_description,
  		80::numeric AS dg_width,
  		''::text AS dg_color,
  		''::text AS dg_background_color,
  		''::text AS dg_format,
  		''::text AS dg_column_formula,
  		MAX(dg_rapport_calc_formula) AS dg_rapport_calc_formula,
  		MAX(dg_rapport_field_tag) AS dg_rapport_field_tag,
  		''::text AS dg_option,
  
  		''::text AS chart_def,
  		''::text AS chart_type,
  		''::text AS chart_subtype,
  		''::text AS chart_cellule,
  		''::text AS chart_serie
  
  	FROM
  	(
  	SELECT 
  			rapport_id, 
  			rapport_index,
  			condition_index, 
  			t_indicateurs.table_name,
  			t_indicateurs.column_name,
  			w_data_column_selected_code.selected_column_condition_array AS indicateur_condition_array,
  			ARRAY[
  			t_indicateurs_associes_1.code, 
  			t_indicateurs_associes_2.code, 
  			t_indicateurs_associes_3.code, 
  			t_indicateurs_associes_4.code, 
  			t_indicateurs_associes_5.code, 
  			t_indicateurs_associes_6.code, 
  			t_indicateurs_associes_7.code, 
  			t_indicateurs_associes_8.code, 
  			t_indicateurs_associes_9.code, 
  			t_indicateurs_associes_10.code, 
  			t_indicateurs_associes_11.code, 
  			t_indicateurs_associes_12.code, 
  			t_indicateurs_associes_13.code, 
  			t_indicateurs_associes_14.code, 
  			t_indicateurs_associes_15.code, 
  			t_indicateurs_associes_16.code, 
  			t_indicateurs_associes_17.code, 
  			t_indicateurs_associes_18.code, 
  			t_indicateurs_associes_19.code, 
  			t_indicateurs_associes_20.code,
  			t_indicateurs.code
  		] AS column_code_portee
  	FROM eco.t_indicateurs
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_1 ON t_indicateurs.indicateur_associe_1_id = t_indicateurs_associes_1.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_2 ON t_indicateurs.indicateur_associe_2_id = t_indicateurs_associes_2.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_3 ON t_indicateurs.indicateur_associe_3_id = t_indicateurs_associes_3.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_4 ON t_indicateurs.indicateur_associe_4_id = t_indicateurs_associes_4.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_5 ON t_indicateurs.indicateur_associe_5_id = t_indicateurs_associes_5.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_6 ON t_indicateurs.indicateur_associe_6_id = t_indicateurs_associes_6.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_7 ON t_indicateurs.indicateur_associe_7_id = t_indicateurs_associes_7.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_8 ON t_indicateurs.indicateur_associe_8_id = t_indicateurs_associes_8.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_9 ON t_indicateurs.indicateur_associe_9_id = t_indicateurs_associes_9.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_10 ON t_indicateurs.indicateur_associe_10_id = t_indicateurs_associes_10.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_11 ON t_indicateurs.indicateur_associe_11_id = t_indicateurs_associes_11.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_12 ON t_indicateurs.indicateur_associe_12_id = t_indicateurs_associes_12.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_13 ON t_indicateurs.indicateur_associe_13_id = t_indicateurs_associes_13.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_14 ON t_indicateurs.indicateur_associe_14_id = t_indicateurs_associes_14.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_15 ON t_indicateurs.indicateur_associe_15_id = t_indicateurs_associes_15.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_16 ON t_indicateurs.indicateur_associe_16_id = t_indicateurs_associes_16.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_17 ON t_indicateurs.indicateur_associe_17_id = t_indicateurs_associes_17.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_18 ON t_indicateurs.indicateur_associe_18_id = t_indicateurs_associes_18.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_19 ON t_indicateurs.indicateur_associe_19_id = t_indicateurs_associes_19.oid
  	LEFT JOIN eco.t_indicateurs t_indicateurs_associes_20 ON t_indicateurs.indicateur_associe_20_id = t_indicateurs_associes_20.oid
  	JOIN w_data_column_selected_code ON w_data_column_selected_code.selected_column_code = t_indicateurs.code
  	) subview
  	JOIN generate_series(1,100) AS i ON column_code_portee[i] <> ''
  	LEFT JOIN w_data_column_selected_code ON 
  		w_data_column_selected_code.selected_column_code = column_code_portee[i] AND
  		w_data_column_selected_code.condition_index = subview.condition_index AND
  		w_data_column_selected_code.selected_column_condition_array IS NOT DISTINCT FROM subview.indicateur_condition_array
  	GROUP BY 1,2,3,4,5,6,7
  	;
  
  	/*UPDATE w_data_column_code SET
  		dg_column_index = sub.offset * 100 + dg_column_index
  	FROM (SELECT rapport_id,row_number() OVER (ORDER BY code) -1 as offset FROM (SELECT rapport_id, t_rapports.code FROM w_data_column_code JOIN eco.t_rapports ON rapport_id = t_rapports.oid GROUP BY 1,2) sub) sub
  	WHERE sub.rapport_id = w_data_column_code.rapport_id
  	;
  */
  	-- Recuperation des indicateurs issus de la formule du rapport
  	UPDATE w_data_column_code SET
  		dg_portee= subview.dg_portee,
  		table_name = '*CALC',
  		total_function ='*CALC'
  	FROM
  	(
  	SELECT calc_CTID,
  		ARRAY[
  			MAX(CASE WHEN i = 01 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 02 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 03 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 04 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 05 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 06 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 07 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 08 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 09 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 10 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 11 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 12 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 13 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 14 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 15 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 16 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 17 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 18 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 19 THEN indicateur_code ELSE NULL END),
  			MAX(CASE WHEN i = 20 THEN indicateur_code ELSE NULL END)
  		] AS dg_portee
  	FROM
  	(
  	SELECT
  		calc_CTID,
  		row_number() OVER (PARTITION BY calc_CTID ORDER BY i) AS i,
  		split_part(dg_rapport_calc_formula_array[i],']',1) AS indicateur_code
  	FROM
  	(
  	SELECT w_data_column_code.CTID AS calc_CTID, condition_index, string_to_array(dg_rapport_calc_formula,'[') AS dg_rapport_calc_formula_array
  	FROM w_data_column_code
  	WHERE column_code = 'CTI_RAPPORTCALC'
  	) subview
  	JOIN generate_series(1,20) i ON dg_rapport_calc_formula_array[i] <> ''
  	JOIN w_data_column_code ON w_data_column_code.dg_rapport_field_tag = split_part(dg_rapport_calc_formula_array[i],']',1) AND
  		w_data_column_code.condition_index = subview.condition_index
  	) subview
  	GROUP BY 1
  	) subview
  	WHERE w_data_column_code.CTID = calc_CTID
  	;
  
  	PERFORM base.cti_execute('
  	UPDATE w_data_column_code 
  	SET dg_rapport_calc_formula = replace(dg_rapport_calc_formula,''[''||dg_portee['||i::text||']||'']'',''['||i::text||']'')
  	WHERE column_code = ''CTI_RAPPORTCALC'' AND 
  		dg_rapport_calc_formula NOT LIKE ''\%T(%'' AND
  		dg_portee['||i::text||'] IS NOT NULL',1)
  	FROM generate_series(1,20) i
  	;
  
  
  	UPDATE w_data_column_code SET
  		db_column_index = subview.column_index,
  		dg_portee[1] = w_data_column_code.column_code
  	FROM
  	(
  	SELECT column_code,
  		condition_index,
  		indicateur_condition_array,
  		row_number() OVER(ORDER BY column_code, indicateur_condition_array, condition_index) AS column_index
  	FROM w_data_column_code
  	JOIN eco.t_indicateurs ON column_code = t_indicateurs.code
  	WHERE t_indicateurs.table_name <> '*CALC' AND t_indicateurs.table_name <> '*RAPPORTCALC'
  	GROUP BY 1,2,3
  	) subview 
  	WHERE w_data_column_code.column_code = subview.column_code AND
  		w_data_column_code.indicateur_condition_array IS NOT DISTINCT FROM subview.indicateur_condition_array AND
  		w_data_column_code.condition_index = subview.condition_index
  	;
  
  	UPDATE w_data_column_code SET
  		table_name = CASE WHEN t_indicateurs.table_name <> '*RAPPORTCALC' THEN t_indicateurs.table_name ELSE '*CALC' END,
  		dimension_date = 
  			CASE 
  			WHEN t_indicateurs.table_name = 'V_LIGNES_COMMANDES_X' AND t_indicateurs.dimension_date = '' THEN '2'
  			WHEN t_indicateurs.table_name <> '*RAPPORTCALC' THEN t_indicateurs.dimension_date 
  			ELSE '*CALC' END,
  		total_function = CASE WHEN t_indicateurs.table_name <> '*RAPPORTCALC' THEN t_indicateurs.total_function ELSE '*CALC' END,
  		db_column_name = 
  			CASE
  			WHEN t_indicateurs.table_name = '*RAPPORTCALC' THEN dg_rapport_calc_formula
  			WHEN condition_formula = '' THEN t_indicateurs.column_name
  			WHEN _view_type <> 'COMP' OR _view_type = 'COMP' AND w_data_column_code.condition_index <= 8 THEN
  				CASE 
  					/*indicateurs sans fonction (peut être à affiner avec MAX, AVG,...)*/
  					WHEN t_indicateurs.total_function = '*LIST' AND t_indicateurs.column_name NOT LIKE '%(%' THEN 'CASE WHEN ' || condition_formula || ' THEN ' || t_indicateurs.column_name || ' ELSE '''' END'
  					/*indicateurs sans fonction */
  					WHEN t_indicateurs.total_function = '*LIST' THEN regexp_replace(regexp_replace(regexp_replace(
  											t_indicateurs.column_name,'COUNT\(DISTINCT ([^\)]*)','COUNT(DISTINCT CASE WHEN ' || condition_formula || ' THEN \1 ELSE null END'),
  											'MAX\(([^\)]*id)\)','MAX(CASE WHEN ' || condition_formula || ' THEN \1 ELSE 0 END)','g'),
  												'MAX\(([^\)]*code)\)','MAX(CASE WHEN ' || condition_formula || ' THEN \1 ELSE '''' END)','g')
  					/* case default */
  					WHEN condition_formula = '' THEN t_indicateurs.column_name
  					/* cas base.cti_sum_distinct_on */
  					WHEN t_indicateurs.column_name ILIKE '%DISTINCT ARRAY%' THEN regexp_replace(t_indicateurs.column_name,'DISTINCT ARRAY\[([^,]*),([^\]]*)\]','DISTINCT ARRAY[\\1,CASE WHEN ' || condition_formula || ' THEN \2 ELSE null END')
  					/* cas MAX(ARRAY[ */
  					WHEN t_indicateurs.column_name ILIKE '%MAX\(ARRAY\[%' THEN regexp_replace(t_indicateurs.column_name,'MAX\(ARRAY\[(date[^,]*)','MAX(ARRAY[(CASE WHEN ' || condition_formula || ' THEN \1 ELSE ''1900-01-01'' END)')
  					/* case DISTINCT */
  					WHEN t_indicateurs.column_name ILIKE 'DISTINCT %' THEN regexp_replace(t_indicateurs.column_name,'DISTINCT (.*)','DISTINCT CASE WHEN ' || condition_formula || ' THEN \1 ELSE null END')
  					/* case texte */
  					ELSE 'CASE WHEN ' || condition_formula || ' THEN ' || t_indicateurs.column_name || ' ELSE 0 END'
  				END
  			ELSE '0' END,
  		dg_column_formula = CASE WHEN t_indicateurs.table_name = '*CALC' THEN t_indicateurs.column_name WHEN t_indicateurs.table_name = '*RAPPORTCALC' THEN dg_rapport_calc_formula ELSE '[1]' END,
  		dg_head = t_indicateurs.entete,
  		dg_largehead = t_indicateurs.entete_etendue,
  		dg_description = trim(CASE WHEN t_indicateurs.description <> '' THEN t_indicateurs.description ELSE entete_etendue END),
  		dg_format = CASE WHEN t_indicateurs.output_format <> '' THEN t_indicateurs.output_format ELSE '#' END,
  		dg_width = CASE WHEN t_indicateurs.width <> 0 THEN t_indicateurs.width ELSE 80 END
  	FROM eco.t_indicateurs,
  		w_data_column_condition
  	WHERE column_code = t_indicateurs.code AND
  		w_data_column_code.condition_index = w_data_column_condition.condition_index
  	;
  	/* remplacement mots clés de période */
  	UPDATE w_data_column_code SET
  		db_column_name = CASE WHEN _view_type = 'COMP' OR (_view_type = 'EVOL' AND dg_column_index != 1) THEN 
  					replace(
  						replace(db_column_name,'toperiode','columnby_to_date'),
  						'periode','columnby_from_date')
  					ELSE replace(
  						replace(db_column_name,'toperiode','''' || (SELECT property_value FROM w_cti_get_columns_options WHERE property = 'SQL_WHERE_TOPERIODE') || ''''),
  						'periode','''' || (SELECT property_value FROM w_cti_get_columns_options WHERE property = 'SQL_WHERE_PERIODE')|| '''')
  					END
  
  	;
  	/* remplacement liste et classes */
  	UPDATE w_data_column_code SET
  		db_column_name = regexp_replace(
  								regexp_replace(
  								db_column_name
  							   , '\[LISTE:([^\]]*)\]', '(select to_id from eco.t_listes_contenu JOIN eco.t_listes on liste_id = t_listes.oid WHERE code = ''\1'')', 'g')
  							   , '\[CLASSE:([^\:]*):([^\]]*)\]', '(select to_id from eco.t_classes JOIN eco.t_classes_sections on classe_id = t_classes.oid JOIN eco.t_classes_sections_elements ON section_id = t_classes_sections.oid WHERE t_classes.code = ''\1'' AND t_classes_sections.code = ''\2'')', 'g')
  	WHERE db_column_name ILIKE '%[LISTE%' OR db_column_name ILIKE '%[CLASSE%'
  	;
  	/* remplacement fonction SUMXXXXX */
  	UPDATE w_data_column_code SET
  		total_function = CASE WHEN total_function like 'SUM%' THEN 'SUM' ELSE total_function END
  	;
  
  	UPDATE w_data_column_code SET
  		dg_head = dg_head || '@LF@% / Total',
  		dg_largehead = dg_largehead || ' Pourcentage / Total',
  		dg_format = '0,0%'
  	WHERE dg_rapport_calc_formula LIKE '%\%T(%'
  	;
  
  	UPDATE w_data_column_code SET
  		dg_head = CASE WHEN t_rapports_rubriques.entete <> '' THEN t_rapports_rubriques.entete ELSE w_data_column_code.dg_head END,
  		dg_largehead = CASE WHEN t_rapports_rubriques.entete_etendue <> '' THEN t_rapports_rubriques.entete_etendue ELSE w_data_column_code.dg_largehead END,
  		dg_width = CASE WHEN t_rapports_rubriques.width <> 0 THEN t_rapports_rubriques.width ELSE w_data_column_code.dg_width END,
  		dg_description = CASE WHEN t_rapports_rubriques.description <> '' THEN t_rapports_rubriques.description ELSE w_data_column_code.dg_description END,
  		dg_format = CASE WHEN t_rapports_rubriques.output_format <> '' THEN t_rapports_rubriques.output_format ELSE w_data_column_code.dg_format END,
  		dg_color = CASE WHEN t_rapports_rubriques.color <> '' THEN t_rapports_rubriques.color ELSE w_data_column_code.dg_color END,
  		dg_background_color = CASE WHEN t_rapports_rubriques.background_color <> '' THEN t_rapports_rubriques.background_color ELSE w_data_column_code.dg_background_color END,
  		dg_option = t_rapports_rubriques.option
  	FROM eco.t_rapports_rubriques
  	WHERE w_data_column_code.rapport_id = t_rapports_rubriques.rapport_id AND
  		w_data_column_code.rapport_numero_rubrique = t_rapports_rubriques.numero_rubrique AND
  		(
  			t_rapports_rubriques.entete <> '' OR
  			t_rapports_rubriques.width <> 0 OR
  			t_rapports_rubriques.description <> '' OR
  			t_rapports_rubriques.output_format <> '' OR
  			t_rapports_rubriques.color <> '' OR
  			t_rapports_rubriques.background_color <> '' OR
  			t_rapports_rubriques.calc_formula <> '' OR
  			t_rapports_rubriques.field_tag <> '' OR
  			t_rapports_rubriques.option <> ''
  		)
  	;
  
  	-- Conditions des indicateurs du rapport
  	UPDATE w_data_column_code
  	SET db_column_name = subview.db_column_name_condition
  	FROM
  	(
  	SELECT table_name, 
  		indicateur_condition_array,
  		db_column_name,
  		total_function,
  		CASE WHEN db_column_name ILIKE 'DISTINCT %' THEN 'DISTINCT ' ELSE '' END ||
  		'CASE WHEN '||
  		array_to_string(base.cti_array_accum(DISTINCT CASE WHEN check_where <> '' THEN check_where ELSE '1=1' END),' AND ') || ' '
  		'THEN '||CASE WHEN db_column_name ILIKE 'DISTINCT %' THEN trim(substr(db_column_name,9)) ELSE db_column_name END||' ELSE '||CASE WHEN total_function = 'SUM' THEN '0' ELSE 'NULL' END || ' END' AS db_column_name_condition
  	FROM
  	(
  	SELECT w_data_column_code.table_name, 
  		w_data_column_code.indicateur_condition_array,
  		w_data_column_code.db_column_name,
  		w_data_column_code.total_function,
  		COALESCE(t_indicateur_condition_table.check_where, t_indicateur_condition_table_dft.check_where) AS check_where
  	FROM w_data_column_code
  	JOIN eco.t_indicateur_condition ON t_indicateur_condition.oid = ANY(indicateur_condition_array)
  	LEFT JOIN eco.t_indicateur_condition_table ON 
  		t_indicateur_condition_table.indicateur_condition_id = t_indicateur_condition.oid AND 
  		t_indicateur_condition_table.table_name = w_data_column_code.table_name
  	LEFT JOIN eco.t_indicateur_condition_table t_indicateur_condition_table_dft ON 
  		t_indicateur_condition_table_dft.indicateur_condition_id = t_indicateur_condition.oid AND 
  		t_indicateur_condition_table_dft.table_name = ''
  	) subview
  	GROUP BY 1,2,3,4
  	) subview
  	WHERE w_data_column_code.table_name = subview.table_name AND
  		w_data_column_code.indicateur_condition_array = subview.indicateur_condition_array AND
  		w_data_column_code.db_column_name = subview.db_column_name AND
  		w_data_column_code.total_function = subview.total_function
  	;
  
  
  
  
  
  
  	PERFORM base.cti_execute('
  	UPDATE w_data_column_code w_data_column_code_dg
  	SET dg_column_formula = replace(w_data_column_code_dg.dg_column_formula,''['||i||']'',''w_table.work_indicateurs[00''||w_data_column_code_db.db_column_index||'']'')
  	FROM w_data_column_code w_data_column_code_db
  	WHERE w_data_column_code_dg.dg_portee['||i||'] = w_data_column_code_db.dg_rapport_field_tag AND
  		w_data_column_code_dg.condition_index = w_data_column_code_db.condition_index AND
  		(
  		w_data_column_code_dg.dg_column_formula <> ''['||i||']'' OR
  		w_data_column_code_dg.indicateur_condition_array IS NOT DISTINCT FROM w_data_column_code_db.indicateur_condition_array
  		)
  	',1)
  	FROM generate_series(1,20) AS i
  	;
  
  
  	PERFORM base.cti_execute('
  	UPDATE w_data_column_code w_data_column_code_dg
  	SET dg_column_formula = replace(w_data_column_code_dg.dg_column_formula,''['||i||']'',''w_table.work_indicateurs[00''||w_data_column_code_db.db_column_index||'']'')
  	FROM w_data_column_code w_data_column_code_db
  	WHERE w_data_column_code_dg.dg_portee['||i||'] = w_data_column_code_db.column_code AND
  		w_data_column_code_dg.condition_index = w_data_column_code_db.condition_index AND
  		w_data_column_code_dg.dg_column_formula LIKE ''%[%'' AND
  		(
  		w_data_column_code_dg.dg_column_formula <> ''['||i||']'' OR
  		w_data_column_code_dg.indicateur_condition_array IS NOT DISTINCT FROM w_data_column_code_db.indicateur_condition_array
  		)
  	',1)
  	FROM generate_series(1,20) AS i
  	;
  
  	-- Remplacement des formules si pourcentage du total
  	UPDATE w_data_column_code
  	SET db_column_name = 0,
  		dg_column_formula = 'base.cti_division('||w_data_column_code_tag.dg_column_formula||','||replace(w_data_column_code_tag.dg_column_formula,'w_table.','w_tablet.')||')*100'
  	FROM w_data_column_code w_data_column_code_tag
  	WHERE w_data_column_code.dg_rapport_calc_formula <> '' AND
  		w_data_column_code_tag.dg_rapport_field_tag <> '' AND
  		w_data_column_code.dg_rapport_calc_formula ILIKE '%['||w_data_column_code_tag.dg_rapport_field_tag||']%' AND
  		w_data_column_code.dg_rapport_calc_formula LIKE '\%T(%'
  	;
  
  	UPDATE w_data_column_code
  	SET dg_column_formula = replace(dg_column_formula,'T.w_table.','w_tablet.')
  	WHERE dg_column_formula LIKE '%T.w_table.%'
  	;
  
  	UPDATE w_data_column_code
  	SET dg_column_formula = 'base.cti_calcul('''||replace(replace(dg_column_formula,']',']::text || ''::numeric'),'w_table',''' || w_table')||''')' 
  	WHERE dg_column_formula <> '' AND
  		table_name = '*CALC'
  	;
  
  	-- mise en place chart par défaut sur première donnée en cas de sélection libre
  	UPDATE w_data_column_code SET
  		dg_option = '[CHART]'
  	WHERE rapport_numero_rubrique = 1 AND rapport_id = 0
  	;
  
  
  	-- Identification des charts
  	UPDATE w_data_column_code SET
  		chart_def = 
  			replace(
  			trim(split_part(split_part(dg_option, '['||tag||'CHART',2),']',1)) ||
  			trim(split_part(split_part(dg_option, '[S'||tag||'CHART',2),']',1)) ||
  			trim(split_part(split_part(dg_option, '[O'||tag||'CHART',2),']',1))
  			,'=',''),
  		chart_type = subview.chart_type,
  		chart_subtype = CASE WHEN dg_option LIKE '%[S'||tag||'CHART%' THEN 'stacked' WHEN dg_option LIKE '%[O'||tag||'CHART%' THEN 'overlaid' ELSE '' END
  	FROM (
  		SELECT ''::text AS tag, 'column'::text AS chart_type
  		UNION
  		SELECT 'COLUMN'::text AS tag, 'column'::text AS chart_type
  		UNION
  		SELECT 'PIE'::text AS tag, 'pie'::text AS chart_type
  		UNION
  		SELECT 'LINE'::text AS tag, 'line'::text AS chart_type
  		) subview
  	WHERE dg_option LIKE '%['||tag||'CHART%' OR dg_option LIKE '%[S'||tag||'CHART%' OR dg_option LIKE '%[O'||tag||'CHART%'
  	;
  
  
  	UPDATE w_data_column_code SET
  		chart_cellule = substr(chart_def,1,2),
  		chart_serie = CASE WHEN substr(chart_def,3) <> '' THEN lpad(substr(chart_def,3),2,'0') ELSE '01' END
  	WHERE chart_type <> ''
  	;
  
  	-- Attribution automatique prochaine cellule
  	UPDATE w_data_column_code
  	SET chart_cellule = cel
  	FROM
  	(
  	SELECT row_number() OVER (PARTITION BY rapport_index ORDER BY dg_column_index) AS r1, w_data_column_code.CTID AS toCTID
  	FROM w_data_column_code
  	WHERE chart_type <> '' AND chart_cellule = ''
  	) subview1
  	JOIN (
  	SELECT row_number() OVER (ORDER BY cel) AS r2, cel
  	FROM unnest(ARRAY['1A','1B','1C','2A','2B','2C','3A','3B','3C']) cel
  	LEFT JOIN w_data_column_code ON cel = chart_cellule
  	WHERE chart_cellule IS NULL AND
  		(
  		(SELECT count(DISTINCT chart_cellule) FROM w_data_column_code WHERE chart_type <> '') <= 4 AND cel NOT LIKE '%C' OR 
  		(SELECT count(DISTINCT chart_cellule) FROM w_data_column_code WHERE chart_type <> '') > 4 
  		)
  	) subview2 ON r1 = r2
  	WHERE w_data_column_code.CTID = subview1.toCTID
  	;
  
  
  	DROP TABLE IF EXISTS temp.w_data_column_code;
  	CREATE TABLE temp.w_data_column_code AS SELECT * FROM w_data_column_code;
  
  	DROP TABLE IF EXISTS cti_get_columns_from_indicateurs_properties;
  	CREATE TEMP TABLE cti_get_columns_from_indicateurs_properties AS
  	SELECT 'IN_'||property AS property, property_value 
  	FROM w_cti_get_columns_options
  
  	UNION
  	SELECT 'DB_COLUMNS' AS property, array_to_string(base.cti_array_accum(db_column_name),',,') AS property_value
  	FROM
  	(
  	SELECT db_column_index, MAX(CASE WHEN db_column_name <> '0' AND total_function <> '*LIST' THEN total_function||'('||db_column_name||')' ELSE '0' END) AS db_column_name
  	FROM w_data_column_code
  	WHERE db_column_index <> 0
  	GROUP BY 1
  	ORDER BY 1
  	) subview
  	UNION
  	SELECT 'DB_COLUMNS-'||v_rapports_tables_indicateurs_1.contexte || '#' || substr(dimensions_date,i,1) AS property, 
  	COALESCE(array_to_string(base.cti_array_accum(CASE WHEN table_name = v_rapports_tables_indicateurs_1.code AND (dimension_date = substr(dimensions_date,i,1) OR dimension_date = '') THEN db_column_name ELSE '0' END),',,'),'') AS property_value
  	FROM eco.v_rapports_tables_indicateurs_1
  	JOIN (SELECT generate_series(1,5) AS i) subview_dimension ON substr(dimensions_date,i,1) <> ''
  	LEFT JOIN
  	(
  	SELECT db_column_index, table_name, dimension_date, MAX(CASE WHEN db_column_name <> '0' AND total_function <> '*LIST' THEN total_function||'('||db_column_name||')' ELSE '0' END) AS db_column_name
  	FROM w_data_column_code
  	WHERE db_column_index <> 0
  	GROUP BY 1,2,3
  	ORDER BY 1
  	) subview
  	ON 1=1
  	WHERE v_rapports_tables_indicateurs_1.code NOT LIKE '*%' AND
  		v_rapports_tables_indicateurs_1.code NOT LIKE 'TAB%'
  	GROUP BY 1
  	UNION
  	SELECT 'DB_QCOLUMNS-'||v_rapports_tables_indicateurs_1.contexte || '#' || substr(dimensions_date,i,1) AS property, 
  	MAX(CASE WHEN table_name = v_rapports_tables_indicateurs_1.code AND  (dimension_date = substr(dimensions_date,i,1) OR dimension_date = '')  THEN '1' ELSE '0' END) AS property_value
  	FROM eco.v_rapports_tables_indicateurs_1
  	JOIN (SELECT generate_series(1,5) AS i) subview_dimension ON substr(dimensions_date,i,1) <> ''
  	LEFT JOIN
  	(
  	SELECT db_column_index, table_name, dimension_date, MAX(CASE WHEN db_column_name <> '0' AND total_function <> '*LIST' THEN total_function||'('||db_column_name||')' ELSE '0' END) AS db_column_name
  	FROM w_data_column_code
  	WHERE db_column_index <> 0 AND
  		total_function <> '*LIST'
  	GROUP BY 1,2,3
  	ORDER BY 1
  	) subview
  	ON 1=1
  	WHERE v_rapports_tables_indicateurs_1.code NOT LIKE '*%' AND
  		v_rapports_tables_indicateurs_1.code NOT LIKE 'TAB%'
  	GROUP BY 1
  	UNION
  	SELECT 'DB_TEXTCOLUMNS', array_to_string(base.cti_array_accum(db_column_name),',,')
  	FROM
  	(
  	SELECT dg_column_index, MAX(CASE WHEN db_column_name <> '0' AND total_function = '*LIST' THEN ''||'('||db_column_name||')::text' ELSE '''''' END) AS db_column_name
  	FROM w_data_column_code
  	WHERE dg_column_index <> 0
  	GROUP BY 1
  	ORDER BY 1
  	) subview
  	UNION
  	SELECT 'DB_TEXTCOLUMNSEMPTY', array_to_string(base.cti_array_accum(db_column_name),',,')
  	FROM
  	(
  	SELECT dg_column_index, ''''''::text AS db_column_name
  	FROM w_data_column_code
  	WHERE dg_column_index <> 0
  	GROUP BY 1
  	ORDER BY 1
  	) subview
  	UNION
  	SELECT 'DB_COLUMNS001', MAX(total_function||'('||db_column_name||')') AS db_column_name
  	FROM w_data_column_code
  	WHERE db_column_index = 1
  	GROUP BY 1
  	UNION
  	SELECT 'FILTER_IND', CASE WHEN total_function != '*LIST' THEN 
  		 CASE WHEN total_function like 'SUM%' THEN 'SUM'
  		 ELSE total_function END || '('||
  		 CASE WHEN _view_type = 'COMP' THEN 
  					replace(
  						replace(column_name,'toperiode','columnby_to_date'),
  						'periode','columnby_from_date')
  					ELSE replace(
  						replace(column_name,'toperiode','''' || (SELECT property_value FROM w_cti_get_columns_options WHERE property = 'SQL_WHERE_TOPERIODE') || ''''),
  						'periode','''' || (SELECT property_value FROM w_cti_get_columns_options WHERE property = 'SQL_WHERE_PERIODE')|| '''')
  					END
  					|| ')' ELSE '' END
  	FROM eco.t_indicateurs
  	WHERE code = (SELECT property_value FROM w_cti_get_columns_options WHERE property = 'FILTER_IND')
  	UNION
  	SELECT 'DG_FIELD'||to_char(all_index,'FM000'), CASE WHEN total_function IS DISTINCT FROM '*LIST' THEN 'NB' ELSE 'TX' END||to_char(all_index,'FM0000')
  	FROM generate_series(1,30*(SELECT COUNT(DISTINCT rapport_index) FROM w_data_column_code)) all_index
  	LEFT JOIN w_data_column_code ON dg_column_index = all_index
  	WHERE _view_type <> 'COMP'
  	GROUP BY 1,2
  	UNION
  	SELECT 'DG_FIELDSEJ', MAX(CASE WHEN column_code = 'CTI_SEJ_LIST_COD' THEN 'TX'||to_char(dg_column_index,'FM0000') ELSE '' END)
  	FROM w_data_column_code
  	UNION
  	SELECT 'DG_FTYPE'||to_char(all_index,'FM000'), 
  		CASE 
  		WHEN total_function IS DISTINCT FROM '*LIST' THEN 'Number' 
  		WHEN dg_format LIKE '%MM%' THEN 'Date'
  		ELSE 'Char' END
  	FROM generate_series(1,30*(SELECT COUNT(DISTINCT rapport_index) FROM w_data_column_code)) all_index
  	LEFT JOIN w_data_column_code ON dg_column_index = all_index
  	WHERE _view_type <> 'COMP'
  	GROUP BY 1,2
  	UNION
  	SELECT 'DG_FALIGN'||to_char(all_index,'FM000'), 
  		CASE WHEN total_function IS DISTINCT FROM '*LIST' THEN 'right' 
  		WHEN dg_format LIKE '%MM%' THEN 'center'
  		WHEN dg_column_formula LIKE '%00:00%' THEN 'center'
  		ELSE 'left' END
  	FROM generate_series(1,30*(SELECT COUNT(DISTINCT rapport_index) FROM w_data_column_code)) all_index
  	LEFT JOIN w_data_column_code ON dg_column_index = all_index
  	WHERE _view_type <> 'COMP'
  	GROUP BY 1,2
  	UNION
  	SELECT 'DG_COLUMNS', array_to_string(base.cti_array_accum('0'::text),',,')
  	FROM generate_series(1,(SELECT GREATEST(MAX(dg_column_index),30*(SELECT COUNT(DISTINCT rapport_index) FROM w_data_column_code)) FROM w_data_column_code)::integer)
  	UNION
  	SELECT 'DG_SETCOLUMNS', array_to_string(base.cti_array_accum(dg_column_formula),',')
  	FROM
  	(
  	SELECT dg_column_index, 
  		MAX(
  			CASE WHEN _view_type <> 'COMP' OR _view_type = 'COMP' AND condition_index <= 8 THEN 'indicateurs['||dg_column_index||']='||dg_column_formula
  			ELSE 'indicateurs['||dg_column_index||']=0' END
  			) AS dg_column_formula
  	FROM w_data_column_code
  	WHERE dg_column_index <> 0 AND
  		total_function <> '*LIST' AND
  		(_view_type <> 'COMP' OR _view_type = 'COMP' AND condition_index <= 8)
  	GROUP BY 1
  	ORDER BY 1
  	) subview
  	UNION
  
  	SELECT 'DG_VISIBLE'||to_char(all_index,'FM000'), CASE WHEN dg_column_index IS NOT NULL AND is_hide <> '1' THEN 'true' ELSE 'false' END
  	FROM generate_series(1,30*(SELECT COUNT(DISTINCT rapport_index) FROM w_data_column_code)) all_index
  	LEFT JOIN w_data_column_code ON dg_column_index = all_index
  	WHERE _view_type <> 'COMP'
  	GROUP BY 1,2
  	UNION
  	SELECT 'DG_HEAD'||to_char(dg_column_index,'FM000'), dg_head
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND _view_type <> 'EVOLCOMP' AND
  		dg_column_index <> 0 AND
  		is_hide <> '1'
  	UNION
  	SELECT 'DG_CODE'||to_char(dg_column_index,'FM000'), CASE WHEN column_code NOT LIKE '%RAPPORTCALC%' THEN column_code ELSE '' END
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND _view_type <> 'EVOLCOMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_SELECT'||to_char(dg_column_index,'FM000'), CASE WHEN column_code NOT LIKE '%RAPPORTCALC%' THEN 'true' ELSE 'false' END
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_LHEAD'||to_char(dg_column_index,'FM000'), dg_largehead
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND _view_type <> 'EVOLCOMP' AND
  		dg_column_index <> 0 AND
  		is_hide <> '1'
  	UNION
  	SELECT 'DG_DESC'||to_char(dg_column_index,'FM000'), dg_description
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND _view_type <> 'EVOLCOMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_FMT'||to_char(dg_column_index,'FM000'), dg_format
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND _view_type <> 'EVOLCOMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_WIDTH'||to_char(dg_column_index,'FM000'), CASE WHEN dg_width >= 150 AND total_function = '*LIST' THEN '' ELSE dg_width::text END
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND _view_type <> 'EVOLCOMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_MWIDTH'||to_char(dg_column_index,'FM000'), CASE WHEN dg_width >= 150 AND total_function = '*LIST' THEN dg_width::text ELSE '' END
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_COLOR'||to_char(dg_column_index,'FM000'), dg_color
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'DG_BGCOLOR'||to_char(dg_column_index,'FM000'), dg_background_color
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' AND
  		dg_column_index <> 0
  
  
  
  	UNION
  	SELECT 'DG_VISIBLE'||to_char(COALESCE(rapport_numero_rubrique,all_index),'FM000'), MAX(CASE WHEN dg_column_index IS NOT NULL AND is_hide <> '1' THEN 'true' ELSE 'false' END)
  	FROM generate_series(1,(SELECT MAX(dg_column_index) FROM w_data_column_code)::integer) all_index
  	LEFT JOIN w_data_column_code ON dg_column_index = all_index
  	WHERE _view_type = 'COMP'
  	GROUP BY 1
  	UNION
  	SELECT 'DG_CODE'||to_char(rapport_numero_rubrique,'FM000'), MAX(CASE WHEN column_code NOT LIKE '%RAPPORTCALC%' THEN column_code ELSE '' END)
  	FROM w_data_column_code
  	WHERE (_view_type = 'COMP' OR _view_type = 'EVOLCOMP') AND
  		dg_column_index <> 0
  	GROUP BY 1
  	UNION
  	SELECT 'DG_HEAD'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_head)
  	FROM w_data_column_code
  	WHERE (_view_type = 'COMP' OR _view_type = 'EVOLCOMP') AND
  		dg_column_index <> 0 AND
  		is_hide <> '1'
  	GROUP BY 1
  	UNION
  	SELECT 'DG_HEAD'||to_char(rapport_numero_rubrique,'FM000')||'_'||suf, MAX(dg_head || chr(10) || CASE WHEN suf LIKE 'E%' THEN 'Evol.' WHEN suf LIKE 'P%' THEN '% Evol.' ELSE '' END || chr(10) || '<font color=''#DB1702''>' || cpl || '</font>')
  	FROM w_data_column_code
  	JOIN (
  		SELECT 'NP1'::text AS suf, 'N'::text AS cpl UNION
  		SELECT 'NP2'::text AS suf, 'N-1'::text AS cpl UNION
  		SELECT 'NP3'::text AS suf, 'N-2'::text AS cpl UNION
  		SELECT 'NP4'::text AS suf, 'N-3'::text AS cpl UNION
  		SELECT 'NM1'::text AS suf, 'M'::text AS cpl UNION
  		SELECT 'NM2'::text AS suf, 'M-12'::text AS cpl UNION
  		SELECT 'NM3'::text AS suf, 'M-24'::text AS cpl UNION
  		SELECT 'NM4'::text AS suf, 'M-36'::text AS cpl UNION
  		SELECT 'EP21'::text AS suf, 'N vs N-1'::text AS cpl UNION
  		SELECT 'EP32'::text AS suf, 'N-1 vs N-2'::text AS cpl UNION
  		SELECT 'EP43'::text AS suf, 'N-2 vs N-3'::text AS cpl UNION
  		SELECT 'EM21'::text AS suf, 'M vs M-12'::text AS cpl UNION
  		SELECT 'EM32'::text AS suf, 'M-12 vs M-24'::text AS cpl UNION
  		SELECT 'EM43'::text AS suf, 'M-24 vs M-36'::text AS cpl UNION
  		SELECT 'PP21'::text AS suf, 'N vs N-1'::text AS cpl UNION
  		SELECT 'PP32'::text AS suf, 'N-1 vs N-2'::text AS cpl UNION
  		SELECT 'PP43'::text AS suf, 'N-2 vs N-3'::text AS cpl UNION
  		SELECT 'PM21'::text AS suf, 'M vs M-12'::text AS cpl UNION
  		SELECT 'PM32'::text AS suf, 'M-12 vs M-24'::text AS cpl UNION
  		SELECT 'PM43'::text AS suf, 'M-24 vs M-36'::text AS cpl
  		) subview ON 1=1
  	WHERE _view_type = 'COMP' AND
  		dg_column_index <> 0 AND
  		is_hide <> '1'
  	GROUP BY 1
  	UNION
  	SELECT 'CH_HEAD'||to_char(rapport_numero_rubrique,'FM000')||'_'||suf, MAX(cpl)
  	FROM w_data_column_code
  	JOIN (
  		SELECT 'NP1'::text AS suf, 'N'::text AS cpl UNION
  		SELECT 'NP2'::text AS suf, 'N-1'::text AS cpl UNION
  		SELECT 'NP3'::text AS suf, 'N-2'::text AS cpl UNION
  		SELECT 'NP4'::text AS suf, 'N-3'::text AS cpl 
  		) subview ON 1=1
  	WHERE (_view_type = 'COMP' OR _view_type = 'EVOLCOMP') AND
  		dg_column_index <> 0 AND
  		is_hide <> '1'
  	GROUP BY 1
  	UNION
  	SELECT 'DG_LHEAD'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_largehead)
  	FROM w_data_column_code
  	WHERE (_view_type = 'COMP' OR _view_type = 'EVOLCOMP') AND
  		dg_column_index <> 0 AND
  		is_hide <> '1'
  	GROUP BY 1
  	UNION
  	SELECT 'DG_DESC'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_description)
  	FROM w_data_column_code
  	WHERE (_view_type = 'COMP' OR _view_type = 'EVOLCOMP') AND
  		dg_column_index <> 0
  	GROUP BY 1
  	UNION
  	SELECT 'DG_FMT'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_format)
  	FROM w_data_column_code
  	WHERE (_view_type = 'COMP' OR _view_type = 'EVOLCOMP') AND
  		dg_column_index <> 0
  	GROUP BY 1
  	UNION
  	SELECT 'DG_WIDTH'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_width::text)
  	FROM w_data_column_code
  	WHERE _view_type = 'COMP' AND
  		dg_column_index <> 0
  	GROUP BY 1
  	UNION
  	SELECT 'DG_COLOR'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_color)
  	FROM w_data_column_code
  	WHERE _view_type = 'COMP' AND
  		dg_column_index <> 0
  	GROUP BY 1
  	UNION
  	SELECT 'DG_BGCOLOR'||to_char(rapport_numero_rubrique,'FM000'), MAX(dg_background_color)
  	FROM w_data_column_code
  	WHERE _view_type = 'COMP' AND
  		dg_column_index <> 0
  	GROUP BY 1
  
  
  
  	UNION
  	SELECT 'DG_SORTFIELD', CASE WHEN _view_type <> 'COMP' THEN COALESCE(MAX(CASE WHEN dg_option LIKE '%[SORT=%' THEN 'indicateurs['||to_char(dg_column_index,'FM000') || ']' ELSE NULL END),'code') ELSE 'DEFAULT' END
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' OR
  		_view_type = 'COMP' AND (dg_column_index - ((rapport_numero_rubrique-1)*8)) = 1
  	UNION
  	SELECT 'DG_SORTDIRECTION', CASE WHEN _view_type <> 'COMP' THEN COALESCE(MAX(CASE WHEN dg_option LIKE '%[SORT=D]%' THEN 'DESC' WHEN dg_option LIKE '%[SORT=A]%' THEN 'ASC' ELSE 'ASC' END),'DESC') ELSE 'DESC' END
  	FROM w_data_column_code
  	WHERE _view_type <> 'COMP' OR
  		_view_type = 'COMP' AND (dg_column_index - ((rapport_numero_rubrique-1)*8)) = 1
  	UNION
  	SELECT 'DG_WIDTH', MAX(dg_width)::text
  	FROM w_data_column_code
  	WHERE _view_type = 'EVOLCOMP' AND
  		dg_column_index <> 0
  	UNION
  	SELECT 'CH_CHARTS', MAX(CASE WHEN chart_type <> '' THEN '50' ELSE '00' END)
  	FROM w_data_column_code
  	WHERE _view_type IN ('','ANZ')
  	UNION
  	SELECT 'CH_VISIBLE'||substr(pos,1,1), MIN(CASE WHEN w_data_column_code.chart_cellule IS NULL THEN 'false' ELSE '' END)
  	FROM (SELECT unnest(ARRAY['1A','1B','1C','2A','2B','2C','3A','3B','3C']) pos) subview
  	LEFT JOIN w_data_column_code ON pos = chart_cellule
  	WHERE _view_type IN ('','ANZ')
  	GROUP BY 1
  	UNION
  	SELECT 'CH_VISIBLE'||pos, MIN(CASE WHEN w_data_column_code.chart_cellule IS NULL THEN 'false' ELSE '' END)
  	FROM (SELECT unnest(ARRAY['1A','1B','1C','2A','2B','2C','3A','3B','3C']) pos) subview
  	LEFT JOIN w_data_column_code ON pos = chart_cellule
  	WHERE _view_type IN ('','ANZ')
  	GROUP BY 1
  	UNION
  	SELECT 'CH_LHEAD'||chart_cellule, array_to_string(base.cti_array_accum(dg_largehead),' + ')
  	FROM 
  	(SELECT chart_cellule, dg_largehead FROM w_data_column_code WHERE chart_type <> '' AND is_hide <> '1' ORDER BY chart_cellule, chart_serie) subview
  	WHERE _view_type IN ('','ANZ')
  	GROUP BY 1
  	UNION
  	SELECT 'CH_TYPE'||chart_cellule, MIN(chart_type)
  	FROM w_data_column_code
  	WHERE _view_type IN ('','ANZ') AND chart_type <> '' 
  	GROUP BY 1
  	UNION
  	SELECT 'CH_SUBTYPE'||chart_cellule, MIN(chart_subtype)
  	FROM w_data_column_code
  	WHERE _view_type IN ('','ANZ') AND chart_type <> ''
  	GROUP BY 1
  	UNION
  	SELECT 'CH_SERIE'||subview.chart_cellule||'-'||to_char(i,'FM00'), COALESCE(MAX(to_char(dg_column_index,'FM000')),'001')
  	FROM
  	(SELECT unnest(ARRAY['1A','1B','1C','2A','2B','2C','3A','3B','3C']) AS chart_cellule) subview
  	JOIN generate_series(1,3) i ON 1=1
  	LEFT JOIN w_data_column_code ON 
  		subview.chart_cellule = w_data_column_code.chart_cellule AND
  		to_char(i,'FM00') = w_data_column_code.chart_serie
  	WHERE _view_type IN ('','ANZ')
  	GROUP BY 1
  	UNION
  	SELECT 'CH_SERIETYPE'||chart_cellule||'-'||chart_serie, MAX(chart_type)
  	FROM w_data_column_code
  	WHERE _view_type IN ('','ANZ') AND chart_type <> ''
  	GROUP BY 1
  	UNION
  	SELECT 'CH_SERIELHEAD'||chart_cellule||'-'||chart_serie, MAX(dg_largehead)
  	FROM w_data_column_code
  	WHERE _view_type IN ('','ANZ') AND chart_type <> '' AND
  		is_hide <> '1'
  	GROUP BY 1
  	UNION
  	SELECT 'CH_SERIEVISIBLE'||subview.chart_cellule||'-'||to_char(i,'FM00'), MAX(CASE WHEN w_data_column_code.chart_serie IS NOT NULL THEN 'true' ELSE 'false' END)
  	FROM
  	(
  	SELECT chart_cellule
  	FROM w_data_column_code
  	WHERE chart_type <> ''
  	GROUP BY 1
  	) subview
  	JOIN generate_series(1,3) i ON 1=1
  	LEFT JOIN w_data_column_code ON 
  		subview.chart_cellule = w_data_column_code.chart_cellule AND
  		to_char(i,'FM00') = w_data_column_code.chart_serie
  	WHERE _view_type IN ('','ANZ')
  	GROUP BY 1
  
  	UNION
  
  	SELECT property, property_value
  	FROM w_cti_get_columns_options
  	WHERE property LIKE 'SQL%'
  
  	ORDER BY 1
  	;
  
  
  
  	-- Corps du SQL 
  	INSERT INTO cti_get_columns_from_indicateurs_properties(property, property_value)
  	SELECT 'SQLCORPS-' || v_rapports_tables_indicateurs_1.contexte || '#' || substr(dimensions_date,i,1),
  		'CTIPREPAR_TREE WORKTABLE{w_table,, ADD} ' ||
  		'GROUPBY1{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_OID_FIELD' THEN property_value ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_COD_FIELD' THEN property_value ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_TXT_FIELD' THEN property_value ELSE '' END) || '} ' ||
  		'GROUPBY2{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_2_OID_FIELD' THEN property_value ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_2_COD_FIELD' THEN property_value ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_2_TXT_FIELD' THEN property_value ELSE '' END) || '} ' ||
  		'GROUPBY3{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_3_OID_FIELD' THEN property_value ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_3_COD_FIELD' THEN property_value ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_3_TXT_FIELD' THEN property_value ELSE '' END) || '} ' ||
  		'GROUPBY1MODE{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY1MODE' THEN property_value ELSE '' END) || '} ' ||
  
  		CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'EVOL' 
  			THEN
  				'COLUMNBY' ||MAX(CASE WHEN property = 'SQL_COLUMNBY' THEN property_value ELSE '' END) ||
  				'{' || MAX(dimensions_date_texte[substr(dimensions_date,i,1)::int]) || ',, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_COUNT' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_OPTION' THEN replace(property_value,'''','''''') ELSE '' END) || 
  				'} '
  			ELSE '' END ||
  
  		CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'CROSS' 
  			THEN
  				'COLUMNBYOID{' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_OID_FIELD' THEN property_value ELSE '' END) || ',,' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_COD_FIELD' THEN property_value ELSE '' END) || ',,' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_TXT_FIELD' THEN property_value ELSE '' END) || ',,' ||
  				'[VIEW.DB_COLUMNS001]' || ',,' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_COUNT' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_OPTION' THEN replace(property_value,'''','''''') ELSE '' END) || 
  				'} '
  			ELSE '' END ||
  
  		CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'EVOLCOMP' 
  			THEN
  				'COLUMNBYOID{' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_OID_FIELD' THEN property_value ELSE '' END) || ',,' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_COD_FIELD' THEN property_value ELSE '' END) || ',,' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_TXT_FIELD' THEN property_value ELSE '' END) || ',,' ||
  				'[VIEW.DB_COLUMNS001]' || ',,' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_COUNT' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_OPTION' THEN replace(property_value,'''','''''') ELSE '' END) || 
  				'} '
  			ELSE '' END ||
  
  		CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'COMP' 
  			THEN
  				'COLUMNBYDATECOMP{' || MAX(dimensions_date_texte[substr(dimensions_date,i,1)::int]) || ',, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_PERIODE_D' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE_D' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				' ,, ,, ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COMP_LASTMONTH' THEN replace(property_value,'''','''''') ELSE '' END) || 
  				'} '
  			ELSE '' END ||
  
  
  		'ORDERBY{' ||
  			MAX(CASE WHEN property = 'DG_SORTFIELD' THEN replace(property_value,'''','''''') ELSE '' END) || ',, ' ||
  			MAX(CASE WHEN property = 'DG_SORTDIRECTION' THEN replace(property_value,'''','''''') ELSE '' END) || '} ' ||
  		'OPTIONS{' ||
  			'NOTZERO=' || MAX(CASE WHEN property = 'SQL_NOTZERO' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' || 
  			'NOTTOTAL=' || MAX(CASE WHEN property = 'SQL_NOTTOTAL' THEN replace(property_value,'''','''''') ELSE '' END) || 
  			'} ' ||
  		'SIGNIFDATA{' ||
  			MAX(CASE WHEN property = 'SQL_SIGNIFDATA' THEN replace(property_value,'''','''''') ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_SIGNIFMODE' THEN replace(property_value,'''','''''') ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_SIGNIFNUMBER' THEN replace(property_value,'''','''''') ELSE '' END) || '} ' ||
  		'INDICATORS{' ||
  			MAX(CASE WHEN property = 'DG_COLUMNS' THEN property_value ELSE '' END) || '} ' ||
  		'INDICATORS_TEXT{' ||
  			MAX(CASE WHEN property = 'DB_TEXTCOLUMNS' THEN property_value ELSE '' END) || '} ' ||
  		'WORKINDICATORS{' ||
  			MAX(CASE WHEN property LIKE 'DB_COLUMNS-%' THEN property_value ELSE '' END) || '} ' ||
  		'FROM ' || 
  			MAX(v_schema_settings_1.property_array[1]) || ' ' ||
  		'WHERE ' || 
  			CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) <> 'COMP' THEN
  				' (' ||
  				/* [1] a remplacer par du dynamique en fonction de la date choisie */
  				MAX(v_schema_settings_1.property_array[2]) || ' ' ||
  				CASE WHEN (SELECT data_type::text FROM information_schema.columns WHERE column_name = MAX(v_schema_settings_1.property_array[2])
  					AND CASE WHEN MAX(v_schema_settings_1.property_array[1]) LIKE '%MENSUEL%' THEN 'v_stock_1' 
  					ELSE left(split_part(lower(MAX(v_schema_settings_1.property_array[1])),'#',2),-1) || '2'  END = table_name AND table_schema = 'eco') = 'date'::text
  					THEN 'BETWEEN ' ||
  						MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') ELSE '' END) || '''' || ' AND ' ||
  						MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) ||
  
  						CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'EVOLCOMP' THEN
  							CASE WHEN MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN property_value ELSE '' END) >= '2' THEN
  								' OR ' || MAX(v_schema_settings_1.property_array[2]) || ' BETWEEN ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''1 year'' - interval ''1 day'')'  || ' AND ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''1 year'' - interval ''1 day'')'
  							ELSE '' END ||
  							CASE WHEN MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN property_value ELSE '' END) >= '3' THEN
  								' OR ' || MAX(v_schema_settings_1.property_array[2]) || ' BETWEEN ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''2 year'' - interval ''1 day'')'  || ' AND ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''2 year'' - interval ''1 day'')'
  							ELSE '' END ||
  							CASE WHEN MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN property_value ELSE '' END) >= '4' THEN
  								' OR ' || MAX(v_schema_settings_1.property_array[2]) || ' BETWEEN ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''3 year'' - interval ''1 day'')'  || ' AND ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''3 year'' - interval ''1 day'')'
  							ELSE '' END
  						ELSE '' END || 
  						') '
  					/*date range */
  					ELSE '&& daterange(' || 
  						MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') ELSE '' END) || '''' || ' , ' ||
  						MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ')' ||
  
  						CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'EVOLCOMP' THEN
  							CASE WHEN MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN property_value ELSE '' END) >= '2' THEN
  								' OR ' || MAX(v_schema_settings_1.property_array[2]) || ' && daterange(' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''1 year'' - interval ''1 day'')'  || ' , ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''1 year'' - interval ''1 day'')' || ')'
  							ELSE '' END ||
  							CASE WHEN MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN property_value ELSE '' END) >= '3' THEN
  								' OR ' || MAX(v_schema_settings_1.property_array[2]) || ' && daterange(' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''2 year'' - interval ''1 day'')'  || ' , ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''2 year'' - interval ''1 day'')' || ')'
  							ELSE '' END ||
  							CASE WHEN MAX(CASE WHEN property = 'SQL_COMP_TYPE' THEN property_value ELSE '' END) >= '4' THEN
  								' OR ' || MAX(v_schema_settings_1.property_array[2]) || ' daterange(' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''3 year'' - interval ''1 day'')'  || ' , ' ||
  									'date('||MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || '::date + interval ''1 day'' - interval ''3 year'' - interval ''1 day'')' || ')'
  							ELSE '' END
  						ELSE '' END || 
  						') '
  					END
  			ELSE '1=1'  END || ' ' ||
  			MAX(CASE WHEN property = 'SQL_WHERE_FILTER' THEN property_value ELSE '' END) ||
  			CASE WHEN MAX(CASE WHEN property = 'FILTER_IND' THEN property_value ELSE '' END) != '' THEN 'HAVING ' || MAX(CASE WHEN property = 'FILTER_IND' THEN property_value ELSE '' END) || '!= 0' ELSE '' END
  	FROM eco.v_rapports_tables_indicateurs_1
  	JOIN (SELECT generate_series(1,5) AS i) subview_dimension ON substr(dimensions_date,i,1) <> ''
  	JOIN eco.v_schema_settings_1 ON (v_rapports_tables_indicateurs_1.contexte || '#' || substr(dimensions_date,i,1)) = v_schema_settings_1.code AND
  		v_schema_settings_1.setting_type = 'FROM'
  	LEFT JOIN cti_get_columns_from_indicateurs_properties ON
  		property NOT LIKE 'DB_COLUMNS-%' OR
  		property = 'FILTER_IND' OR
  		property = ('DB_COLUMNS-' || v_rapports_tables_indicateurs_1.contexte || '#' || substr(dimensions_date,i,1))
  	GROUP BY 1
  	;
  
  	INSERT INTO cti_get_columns_from_indicateurs_properties(property, property_value)
  	SELECT 'SQLCORPS-INIT',
  		'CTIPREPAR_TREE WORKTABLE{w_table} ' ||
  		'GROUPBY1MODE{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY1MODE' THEN replace(property_value,'''','''''') ELSE '' END) || '} '
  		FROM cti_get_columns_from_indicateurs_properties
  		;
  
  	INSERT INTO cti_get_columns_from_indicateurs_properties(property, property_value)
  	SELECT 'SQLCORPS-FINALIZE',
  		'CTISELECT_TREE WORKTABLE{w_table,, ADD} ' ||
  		'GROUPBY1{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_OID_FIELD' THEN CASE WHEN property_value <> '0' THEN 'oid' ELSE '0' END ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_COD_FIELD' THEN CASE WHEN property_value <> '0' THEN 'code' ELSE '0' END ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_TXT_FIELD' THEN CASE WHEN property_value <> '0' THEN 'texte' ELSE '0' END ELSE '' END) || '} ' ||
  		'GROUPBY2{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_2_OID_FIELD' THEN CASE WHEN property_value <> '0' THEN 'oid' ELSE '0' END ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_2_COD_FIELD' THEN CASE WHEN property_value <> '0' THEN 'code' ELSE '0' END ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_2_TXT_FIELD' THEN CASE WHEN property_value <> '0' THEN 'texte' ELSE '0' END ELSE '' END) || '} ' ||
  		'GROUPBY3{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_3_OID_FIELD' THEN CASE WHEN property_value <> '0' THEN 'oid' ELSE '0' END ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_3_COD_FIELD' THEN CASE WHEN property_value <> '0' THEN 'code' ELSE '0' END ELSE '' END) || ',,' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY_3_TXT_FIELD' THEN CASE WHEN property_value <> '0' THEN 'texte' ELSE '0' END ELSE '' END) || '} ' ||
  		'GROUPBY1MODE{' ||
  			MAX(CASE WHEN property = 'SQL_GROUPBY1MODE' THEN replace(property_value,'''','''''') ELSE '' END) || '} ' ||
  		CASE WHEN MAX(CASE WHEN property = 'IN_VIEWTYPE' THEN property_value ELSE '' END) = 'EVOL' 
  			THEN
  				'COLUMNBY' || MAX(CASE WHEN property = 'SQL_COLUMNBY' THEN replace(property_value,'''','''''') ELSE '' END) ||
  				'{' || 'date_commande' || ',, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_PERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_WHERE_TOPERIODE' THEN '''' || replace(property_value,'''','''''') || '''' ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_COUNT' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' ||
  				MAX(CASE WHEN property = 'SQL_COLUMNBY_OPTION' THEN replace(property_value,'''','''''') ELSE '' END) || 
  				'} '
  			ELSE '' END ||
  		'ORDERBY{' ||
  			MAX(CASE WHEN property = 'DG_SORTFIELD' THEN replace(property_value,'''','''''') ELSE '' END) || ',, ' ||
  			MAX(CASE WHEN property = 'DG_SORTDIRECTION' THEN replace(property_value,'''','''''') ELSE '' END) || '} ' ||
  		'OPTIONS{' ||
  			'NOTZERO=' || MAX(CASE WHEN property = 'SQL_NOTZERO' THEN replace(property_value,'''','''''') ELSE '' END) || ' ,, ' || 
  			'NOTTOTAL=' || MAX(CASE WHEN property = 'SQL_NOTTOTAL' THEN replace(property_value,'''','''''') ELSE '' END) || 
  			'} ' ||
  		'INDICATORS{' ||
  			MAX(CASE WHEN property = 'DG_COLUMNS' THEN replace(property_value,'''','''''') ELSE '' END) || '} ' ||
  		'INDICATORS_TEXT{' ||
  			MAX(CASE WHEN property = 'DB_TEXTCOLUMNSEMPTY' THEN property_value ELSE '' END) || '} ' ||
  		'FROM (SELECT 0::bigint AS oid, ''''::text AS code, ''''::text AS texte LIMIT 0) subview ' || '
  			AFTER{
  				DROP TABLE IF EXISTS temp.w_table_before., CREATE TABLE temp.w_table_before AS SELECT * FROm w_table.,
  				DROP TABLE IF EXISTS temp.cti_get_columns_from_indicateurs_properties., CREATE TABLE temp.cti_get_columns_from_indicateurs_properties AS SELECT * FROm cti_get_columns_from_indicateurs_properties.,
  
  				SELECT eco.cti_view_after('''', '''')
  				.,
  				DROP TABLE IF EXISTS temp.w_table_after., CREATE TABLE temp.w_table_after AS SELECT * FROm w_table.,
  				UPDATE w_table SET oid1=oid1 WHERE oid1<>oid1
  				.,
  			}'
  
  	FROM cti_get_columns_from_indicateurs_properties
  	;
  
  
  
  	FOR row IN 
  		SELECT * 
  		FROM cti_get_columns_from_indicateurs_properties
  		ORDER BY 1
  	LOOP
  		RETURN NEXT row;
  	END LOOP;
  
  	RETURN ;
  END;