return: SETOF record
lang: plpgsql
parameters:
  p0:
    type: bigint
    name: i_rapport_id
  p1:
    type: text
    name: i_table_name
  p2:
    type: text
    name: i_option
src: |
  DECLARE	
  	sqlcmd text;
  	
  	temp_cursor refcursor;
  	_oid integer;
  	_count integer;
  	_table_count integer;
  	_maxind integer;
  	_indicateur_name text;
  	
  	row  RECORD;
  BEGIN
  	
  	_maxind = 100;
  _table_count = 0;
  	
  	OPEN temp_cursor FOR 
  	SELECT MAX(oid), SUM(CASE WHEN i_table_name = table_name THEN 1 ELSE 0 END)
  		FROM pmsi.t_indicateurs
  		WHERE oid IN (
  		SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id])
  		FROM pmsi.t_rapports 
  			JOIN pmsi.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  			JOIN pmsi.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid);
  	
  	FETCH temp_cursor INTO _oid,_count;
  	WHILE FOUND LOOP
  		_maxind = _oid;
  		_table_count = _count;
  	FETCH temp_cursor INTO _oid, _count;
  	END LOOP;
  	CLOSE temp_cursor;
  	_indicateur_name = CASE i_table_name 
  				WHEN 'v_rss_1' THEN 'INDICATEURS_RSS'
  				WHEN 'v_rss_rum_1' THEN 'INDICATEURS_RUM'
  				WHEN 'v_rsf_detail_1' THEN 'INDICATEURS_PRESTA'
  				WHEN 'v_rsf_detail_6' THEN 'INDICATEURS_PRESTAH'
  				WHEN 'v_rss_lpp_1' THEN 'INDICATEURS_LPP'
  				WHEN 'v_rss_ucd_1' THEN 'INDICATEURS_UCD'
  				WHEN 'v_rss_actes_1' THEN 'INDICATEURS_ACTE'
  				WHEN 'v_rss_diagnostics_1' THEN 'INDICATEURS_DIAG'
  				WHEN 'v_rss_cancero_1' THEN 'INDICATEURS_CANCERO'
  				WHEN 'v_expert_rss_controle_1' THEN 'INDICATEURS_EXPERT'
  				WHEN 'v_rss_src_1' THEN 'INDICATEURS_SRC'
  				WHEN 'v_rsf_detail_rum_1' THEN 'INDICATEURS_PRESTA_RUM'
  				WHEN 'v_rss_sae_1' THEN 'INDICATEURS_RSS_SAE'
  				ELSE 'INDICATEURS_OTHERS' END;
  	IF (_table_count = 0) THEN
  		FOR row IN SELECT _indicateur_name::text, ''::text
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	END IF;
  	
  	IF (i_option <> 'MONTH' AND i_option <> 'COMP' AND _table_count > 0) THEN
  		FOR row IN 
  			SELECT 
  				_indicateur_name::text, base.cti_group_concat(CASE WHEN subview.oid IS NOT NULL THEN total_function || '(' || pmsi.cti_prepare_rapport_get_column(column_name) || ')' ELSE 'NULL::numeric' END)
  			FROM
  			(
  				SELECT generate_series, subview.oid, total_function, column_name
  				FROM generate_series(1, _maxind) 
  				LEFT JOIN 
  				(
  				SELECT *
  				FROM pmsi.t_indicateurs
  				WHERE oid IN (
  				SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id])
  				FROM pmsi.t_rapports 
  					JOIN pmsi.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  					JOIN pmsi.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid)
  				) subview ON generate_series = subview.oid AND subview.table_name  = i_table_name
  			ORDER BY 1
  			) subview
  			UNION
  		
  			SELECT 'RUBRIQUES_RAPPORTXX',
  				base.cti_group_concat(
  				CASE WHEN table_name <> '*CALC' THEN 'SUM(indicateurs[' || indicateur_id || '])'
  				ELSE 
  						replace(
  							replace(
  								column_name,
  								'[1]', 
  								'SUM(indicateurs[000' || indicateur_associe_1_id || '])'
  							),
  							'[2]', 
  							'SUM(indicateurs[000' || indicateur_associe_2_id || '])'
  						)
  				END)
  			FROM (SELECT * FROM pmsi.t_rapports_rubriques 
  				JOIN pmsi.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid
  				WHERE rapport_id = i_rapport_id ORDER BY numero_rubrique
  			) subview
  			
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	
  	END IF;
  	IF (i_option = 'MONTH' AND _table_count > 0) THEN
  		FOR row IN 
  			SELECT 
  			_indicateur_name::text,  
  			base.cti_group_concat(
  				CASE WHEN subview.oid IS NOT NULL THEN 
  					'ARRAY[' 
  					|| '(' || total_function || '(' || pmsi.cti_prepare_rapport_get_column(column_name) || ')::numeric),' 
  					|| replace(month_formula, '#SEQ#', '< 1') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 1') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 2') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 3') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 4') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 5') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 6') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 7') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 8') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 9') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 10') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 11') || ',' 
  					|| replace(month_formula, '#SEQ#', '= 12') || '' 
  					|| ']'
  				ELSE 'ARRAY[NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric,NULL::numeric]' END)
  			FROM
  				(
  				SELECT generate_series, subview.oid, total_function, column_name, month_formula
  				FROM generate_series(1, _maxind) 
  				LEFT JOIN 
  				(
  				SELECT *, 
  					CASE WHEN column_name ILIKE '%DISTINCT%' 
  						THEN total_function || '(DISTINCT CASE WHEN columnby_sequence #SEQ# THEN ' || replace(pmsi.cti_prepare_rapport_get_column(column_name),'DISTINCT','') || ' ELSE NULL END)'
  						ELSE total_function || '(CASE WHEN columnby_sequence #SEQ# THEN ' || pmsi.cti_prepare_rapport_get_column(column_name) || ' ELSE NULL END)' END AS month_formula
  				FROM pmsi.t_indicateurs
  				WHERE oid IN (
  				SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id])
  				FROM pmsi.t_rapports 
  					JOIN pmsi.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  					JOIN pmsi.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid)
  				) subview ON generate_series = subview.oid  AND subview.table_name  = i_table_name
  				ORDER BY 1
  				) subview
  					
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	
  	END IF;
  IF (i_option = 'COMP' AND _table_count > 0) THEN
  		FOR row IN 
  			SELECT 
  				_indicateur_name::text,  
  				base.cti_group_concat(
  				CASE WHEN subview.oid IS NOT NULL THEN 
  					'ARRAY[' 
  					|| replace(month_formula, '#SEQ#', '= 1') || '::numeric,' 
  					|| replace(month_formula, '#SEQ#', '= 2') || '::numeric,' 
  					|| replace(month_formula, '#SEQ#', '= 3') || '::numeric' 
  					|| ']'
  				ELSE 'ARRAY[NULL::numeric,NULL::numeric,NULL::numeric]' END)
  			FROM
  				(
  				SELECT generate_series, subview.oid, month_formula
  				FROM generate_series(1, _maxind) 
  				LEFT JOIN 
  				(
  				SELECT *, 
  					CASE WHEN column_name ILIKE '%DISTINCT%' 
  						THEN total_function || '(DISTINCT CASE WHEN columnby_sequence #SEQ# THEN ' || replace(pmsi.cti_prepare_rapport_get_column(column_name),'DISTINCT','') || ' ELSE NULL END)'
  						ELSE total_function || '(CASE WHEN columnby_sequence #SEQ# THEN ' || pmsi.cti_prepare_rapport_get_column(column_name) || ' ELSE NULL END)' END AS month_formula
  				FROM pmsi.t_indicateurs
  				WHERE oid IN (
  				SELECT base.cti_unnest(Array[indicateur_id, indicateur_associe_1_id, indicateur_associe_2_id, indicateur_associe_3_id, indicateur_associe_4_id, indicateur_associe_5_id])
  				FROM pmsi.t_rapports 
  					JOIN pmsi.t_rapports_rubriques ON t_rapports.oid = t_rapports_rubriques.rapport_id AND rapport_id = i_rapport_id
  					JOIN pmsi.t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid)
  				) subview ON generate_series = subview.oid  AND subview.table_name  = i_table_name
  				ORDER BY 1
  				) subview
  				
  					
  			
  		LOOP
  			RETURN NEXT row;
  		END LOOP;
  	
  	END IF;
  	RETURN ;
  END;