return: text
lang: plpgsql
parameters:
  p0:
    type: integer
    name: input_rapport_id
strict: STRICT
src: |
  DECLARE 
  	rapport_joins TEXT;
  	rapport_joins_v_rsf_detail_1 TEXT;
  	rapport_joins_v_rss_lpp_1 TEXT;
  	rapport_joins_v_rss_actes_1 TEXT;
  	rapport_joins_v_rss_diagnostics_1 TEXT;
  	rapports_cursor refcursor;
  	numero_rubrique INTEGER;
  	column_name TEXT;
  	rubrique_texte TEXT;
  	table_name TEXT;
  	total_function TEXT;
  	last_rubrique INTEGER;
  BEGIN
  	rapport_joins = '';
  	rapport_joins_v_rsf_detail_1 = '';
  	rapport_joins_v_rss_lpp_1 = '';
  	rapport_joins_v_rss_actes_1 = '';
  	rapport_joins_v_rss_diagnostics_1 = '';
  	OPEN rapports_cursor FOR 
  	SELECT  t_rapports_rubriques.numero_rubrique, t_indicateurs.column_name, t_indicateurs.table_name, t_indicateurs.total_function 
          FROM    activite.t_rapports_rubriques, activite.t_indicateurs
          WHERE   rapport_id = $1 AND indicateur_id = t_indicateurs.oid 
  	ORDER BY numero_rubrique;
  	FETCH rapports_cursor INTO numero_rubrique, column_name, table_name, total_function;
  	WHILE FOUND LOOP
  		IF (table_name = 'v_rsf_detail_1') THEN
  			rapport_joins_v_rsf_detail_1 = rapport_joins_v_rsf_detail_1 || ', ' || total_function || '(' || column_name || ') AS indicateur' || numero_rubrique;
  		END IF;
  		IF (table_name = 'v_rss_lpp_1') THEN
  			rapport_joins_v_rss_lpp_1 = rapport_joins_v_rss_lpp_1 || ', ' || total_function || '(' || column_name || ') AS indicateur' || numero_rubrique;
  		END IF;
  		IF (table_name = 'v_rss_actes_1') THEN
  			rapport_joins_v_rss_actes_1 = rapport_joins_v_rss_actes_1 || ', ' || total_function || '(' || column_name || ') AS indicateur' || numero_rubrique;
  		END IF;
  		IF (table_name = 'v_rss_diagnostics_1') THEN
  			rapport_joins_v_rss_diagnostics_1 = rapport_joins_rss_diagnostics_1 || ', ' || total_function || '(' || column_name || ') AS indicateur' || numero_rubrique;
  		END IF;
  		FETCH rapports_cursor INTO numero_rubrique, column_name, table_name, total_function;
  	END LOOP;
  	CLOSE rapports_cursor;
  	IF (rapport_joins_v_rsf_detail_1 != '') THEN
  		rapport_joins_v_rsf_detail_1 = 'LEFT JOIN (SELECT v_rsf_detail_1.no_rss, v_rsf_detail_1.finess' || rapport_joins_v_rsf_detail_1 || ' FROM activite.v_rsf_detail_1, activite.v_rss_1 WHERE v_rsf_detail_1.no_rss = activite.v_rss_1.no_rss AND v_rsf_detail_1.finess = activite.v_rss_1.finess  GROUP BY v_rsf_detail_1.no_rss, v_rsf_detail_1.finess)  subview_v_rsf_detail_1  ON (v_rss_1.no_rss = subview_v_rsf_detail_1.no_rss AND v_rss_1.finess = subview_v_rsf_detail_1.finess) ';
  		rapport_joins = rapport_joins || rapport_joins_v_rsf_detail_1;
  	END IF;
  	IF (rapport_joins_v_rss_lpp_1 != '') THEN
  		rapport_joins_v_rss_lpp_1 = 'LEFT JOIN (SELECT v_rss_lpp_1.no_rss, v_rss_lpp_1.finess' || rapport_joins_v_rss_lpp_1 || ' FROM activite.v_rss_lpp_1, activite.v_rss_1 WHERE v_rss_lpp_1.no_rss = activite.v_rss_1.no_rss AND v_rss_lpp_1.finess = activite.v_rss_1.finess  GROUP BY v_rss_lpp_1.no_rss, v_rss_lpp_1.finess)  subview_v_rss_lpp_1  ON (v_rss_1.no_rss = subview_v_rss_lpp_1.no_rss AND v_rss_1.finess = subview_v_rss_lpp_1.finess) ';
  		rapport_joins = rapport_joins || rapport_joins_v_rss_lpp_1;
  	END IF;
  	IF (rapport_joins_v_rss_actes_1 != '') THEN
  		rapport_joins_v_rss_actes_1 = 'LEFT JOIN (SELECT v_rss_actes_1.no_rss, v_rss_actes_1.finess' || rapport_joins_v_rss_actes_1 || ' FROM activite.v_rss_actes_1, activite.v_rss_1 WHERE v_rss_actes_1.no_rss = activite.v_rss_1.no_rss AND v_rss_actes_1.finess = activite.v_rss_1.finess  GROUP BY v_rss_actes_1.no_rss, v_rss_actes_1.finess)  subview_v_rss_actes_1  ON (v_rss_1.no_rss = subview_v_rss_1.no_rss AND v_rss_actes_1.finess = subview_v_rss_actes_1.finess) ';
  		rapport_joins = rapport_joins || rapport_joins_v_rss_actes_1;
  	END IF;
  	IF (rapport_joins_v_rss_diagnostics_1 != '') THEN
  		rapport_joins_v_rss_diagnostics_1 = 'LEFT JOIN (SELECT v_rss_diagnostics_1.no_rss, v_rss_diagnostics_1.finess' || rapport_joins_v_rss_diagnostics_1 || ' FROM activite.v_rss_diagnostics_1, activite.v_rss_1 WHERE v_rss_diagnostics_1.no_rss = activite.v_rss_1.no_rss AND v_rss_diagnostics_1.finess = activite.v_rss_1.finess  GROUP BY v_rss_diagnostics_1.no_rss, v_rss_diagnostics_1.finess)  subview_v_rss_diagnostics_1  ON (v_rss_1.no_rss = subview_v_rss_diagnostics_1.no_rss AND v_rss_1.finess = subview_v_rss_diagnostics_1.finess) ';
  		rapport_joins = rapport_joins || rapport_joins_v_rss_diagnostics_1;
  	END IF;
          RETURN rapport_joins;
  END;