return: SETOF record
lang: plpgsql
parameters:
  p0:
    type: text
    name: p_v_rss_x
  p1:
    type: text
    name: p_groupby
  p2:
    type: text
    name: p_onfield
  p3:
    type: integer
    name: p_onmaxpercent
  p4:
    type: integer
    name: p_onmaxnumber
  p5:
    type: text
    name: p_selections_date
  p6:
    type: text
    name: p_selections_rss
  p7:
    type: text
    name: p_selections_context
src: |
  DECLARE	
  	oidField TEXT;
  	oidFieldComplete TEXT;
  	oidFieldText TEXT;
  	ch TEXT;
  	table_name TEXT;
  	sumField TEXT;
  	selectCmd TEXT;
  	selectCmdBase TEXT;
  	temp_cursor refcursor;
  	oids_array TEXT;
  	oids_array_bis TEXT;
  	oids_array_toapply TEXT;
  	oids_array_bis_toapply TEXT;
  	onmaxpercent integer;
  	onmaxnumber integer;
  	winteger integer;
  	select_percent integer;
  	other_percent integer;
  	onfield_text TEXT;
  	rec RECORD;
  BEGIN
  	oidField = '';
  	oidFieldComplete = '0';
  	oidFieldText = '0';
  	FOR i IN 1..length(p_groupby) LOOP
  		ch = substring(p_groupby,i,1);
  		IF (ch = ',') THEN 
  			oidFieldComplete = '1';
  			IF(oidField = 'v_rss_1.no_oid') THEN
  				oidField = '';
  				oidFieldComplete = '0';
  				oidFieldText = '1';
  			END IF;
  		ELSE
  			IF (oidFieldComplete = '0') THEN 
  				oidField = oidField || ch;
  			END IF;
  		END IF;
  	END LOOP;
  	IF(oidField = '') THEN
  		oidField = 'v_rss_1.ghm5_id';
  	END IF;
  	
  	sumField = p_onfield;
  	onfield_text = '';
  	if (sumField = 'v_rss_1.nb_rsa') THEN
  		onfield_text = 'des séjours';
  	END IF;
  	if (sumField = 'v_rss_1.base_remboursement') THEN
  		onfield_text = 'du C.A. séjour';
  	END IF;
  	IF (sumField = 'v_rss_lpp_1.nombre') THEN
  		onfield_text = 'du nombre de LPP';
  	END IF;
  	IF (sumField = 'v_rss_lpp_1.montant_facture') THEN
  		onfield_text = 'du C.A. LPP';
  	END IF;
  	IF (sumField = 'v_rss_ucd_1.nombre') THEN
  		onfield_text = 'du nombre d''''''''UCD';
  	END IF;
  	IF (sumField = 'v_rss_ucd_1.montant_facture') THEN
  		onfield_text = 'du C.A. UCD';
  	END IF;
  	IF (sumField = 'v_rsf_detail_1.nombre') THEN
  		onfield_text = 'du nombre de prestations';
  	END IF;
  	IF (sumField = 'v_rsf_detail_1.base_remboursement') THEN
  		onfield_text = 'du C.A. prestations';
  	END IF;
  	IF (sumField = 'v_rss_rum_1.nb_rum_rum') THEN
  		onfield_text = 'du nombre de RUM';
  	END IF;
  	IF (sumField = 'v_rss_rum_1.igs2_rum') THEN
  		onfield_text = 'de l''''''''IGS2 RUM';
  	END IF;
  	IF (sumField = 'v_rss_rum_1.duree_sejour_rum') THEN
  		onfield_text = 'de la durée de séjour RUM';
  	END IF;
  	IF (sumField = 'v_rss_rum_2.nb_rum_rum') THEN
  		onfield_text = 'du nombre de RUM';
  	END IF;
  	IF (sumField = 'v_rss_rum_2.igs2_rum') THEN
  		onfield_text = 'de l''''''''IGS2 RUM';
  	END IF;
  	IF (sumField = 'v_rss_rum_2.duree_sejour_rum') THEN
  		onfield_text = 'de la durée de séjour RUM';
  	END IF;
  	IF (sumField = 'v_rss_diagnostics_1.nb_diagnostics') THEN
  		onfield_text = 'du nombre de diagnostics';
  	END IF;
  	IF (sumField = 'v_rss_diagnostics_1.nb_diagnostics_p') THEN
  		onfield_text = 'du nombre de diagnostics principaux';
  	END IF;
  	IF (sumField = 'v_rss_actes_1.nombre') THEN
  		onfield_text = 'du nombre d''''''''actes';
  	END IF;
  	IF (sumField = 'v_rss_actes_1.icr') THEN
  		onfield_text = 'de l''''''''ICR';
  	END IF;
  	IF (sumField = 'v_rss_actes_4.nb_interventions') THEN
  		onfield_text = 'du nombre d''''''''interventions';
  	END IF;
  	IF (sumField = 'v_rss_actes_4.icr') THEN
  		onfield_text = 'de l''''''''ICR';
  	END IF;
  	table_name = 'v_rss_1';
  	
  	IF (sumField LIKE 'v_rss_lpp_1.%') THEN
  		table_name = 'v_rss_lpp_1';
  	END IF;
  	IF (sumField LIKE 'v_rss_ucd_1.%') THEN
  		table_name = 'v_rss_ucd_1';
  	END IF;
  	IF (sumField LIKE 'v_rsf_detail_1.%') THEN
  		table_name = 'v_rsf_detail_1';
  	END IF;
  	IF (sumField LIKE 'v_rss_rum_1.%') THEN
  		table_name = 'v_rss_rum_1';
  	END IF;
  	IF (sumField LIKE 'v_rss_rum_2.%') THEN
  		table_name = 'v_rss_rum_2';
  	END IF;
  	IF (sumField LIKE 'v_rss_diagnostics_1.%') THEN
  		table_name = 'v_rss_diagnostics_1';
  	END IF;
  	IF (sumField LIKE 'v_rss_actes_1.%') THEN
  		table_name = 'v_rss_actes_1';
  	END IF;
  	IF (sumField LIKE 'v_rss_actes_4.%') THEN
  		table_name = 'v_rss_actes_4';
  	END IF;
  	onmaxpercent = 0;
  	onmaxnumber = 0;
  	IF(p_onmaxpercent > 0) THEN
  		onmaxpercent = p_onmaxpercent;
  	ELSE
  		IF(p_onmaxnumber > 0) THEN
  			onmaxnumber = p_onmaxnumber;
  		END IF;
  	END IF;
  	
  	IF(onmaxpercent = 0 AND onmaxnumber = 0) THEN
  		onmaxpercent = 100;
  	END IF;
  	
  	IF(onmaxpercent < 100 AND oidfield != '*all') THEN
  		DROP SEQUENCE IF EXISTS temp_sequence;
  		CREATE TEMP SEQUENCE temp_sequence;        
  		DROP TABLE IF EXISTS temp_sum_table; 
  		
  		IF (table_name = 'v_rss_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || '        
  				WHERE ' || p_selections_date || ' ' || p_selections_rss || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_lpp_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_lpp_1        
  				WHERE v_rss_1.rss_id = v_rss_lpp_1.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_ucd_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_ucd_1        
  				WHERE v_rss_1.rss_id = v_rss_ucd_1.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rsf_detail_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rsf_detail_1        
  				WHERE v_rss_1.rss_id = v_rsf_detail_1.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_diagnostics_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_diagnostics_1        
  				WHERE v_rss_1.rss_id = v_rss_diagnostics_1.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_actes_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_actes_1        
  				WHERE v_rss_1.rss_id = v_rss_actes_1.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_actes_4') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_actes_4        
  				WHERE v_rss_1.rss_id = v_rss_actes_4.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_rum_1') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_rum_1        
  				WHERE v_rss_1.rss_id = v_rss_rum_1.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		IF (table_name = 'v_rss_rum_2') THEN
  			selectCmdBase = 'SELECT ' || oidField || ' as oid, sum(' || sumField || ') as sum        
  				FROM ' || p_v_rss_x || ' , pmsi.v_rss_rum_2        
  				WHERE v_rss_1.rss_id = v_rss_rum_2.rss_id  AND ' || p_selections_date || ' ' || p_selections_rss || ' ' || p_selections_context || '                                          
  				GROUP BY ' || oidField || '         
  				ORDER BY sum(' || sumField || ') desc';
  		END IF;
  		selectCmd = 'CREATE TEMP TABLE temp_sum_table AS       
  		SELECT *, 0.00 as temp_pourcentage from       (       
  		SELECT nextval(''temp_sequence''::regclass) as temp_sequence, * from (' || selectCmdBase || ') subview ) subview2';    
  		EXECUTE selectCmd;
  		oids_array_toapply='';	
  		
  		-- Liste des oids
  		IF(onmaxpercent > 0) THEN
  			EXECUTE 'UPDATE temp_sum_table SET temp_pourcentage = (select sum(sum) FROM temp_sum_table temp_sum_table_t WHERE temp_sum_table.temp_sequence > temp_sum_table_t.temp_sequence) / (SELECT sum(sum) FROM temp_sum_table temp_sum_table_t) * 100 WHERE temp_sum_table.temp_sequence > 1';
  			IF (oidFieldText = '0') THEN 
  				selectCmd = 'SELECT array(SELECT oid FROM temp_sum_table where temp_pourcentage <= ' || onmaxpercent || '),  array(SELECT oid FROM temp_sum_table where temp_pourcentage <= ' || onmaxpercent || ')';  
  			ELSE
  				selectCmd = 'SELECT array(SELECT '''''''''''' || oid || '''''''''''' FROM temp_sum_table where temp_pourcentage <= ' || onmaxpercent || '), array(SELECT '''''''' || oid || '''''''' FROM temp_sum_table where temp_pourcentage <= ' || onmaxpercent || ')';  
  			END IF;
  			
  		ELSE
  			
  			IF (oidFieldText = '0') THEN 
  				selectCmd = 'SELECT array(SELECT oid FROM temp_sum_table where temp_sequence <= ' || onmaxnumber || '), array(SELECT oid FROM temp_sum_table where temp_sequence <= ' || onmaxnumber || ')'; 
  			ELSE
  				selectCmd = 'SELECT array(SELECT '''''''''''' || oid || '''''''''''' FROM temp_sum_table where temp_sequence <= ' || onmaxnumber || '), array(SELECT '''''''' || oid || '''''''' FROM temp_sum_table where temp_sequence <= ' || onmaxnumber || ')'; 
  			END IF;
  		END IF;
  		OPEN temp_cursor FOR 
  		EXECUTE selectCmd;  
  		FETCH temp_cursor INTO oids_array, oids_array_bis;
  		WHILE FOUND LOOP
  			IF(oids_array is null) THEN
  				IF (oidFieldText = '0') THEN 
  					oids_array = '(-1)';
  					oids_array_bis = '(-1)';
  				ELSE
  					oids_array = '(''''-1'''')';
  					oids_array_bis = '(''-1'')';
  				END IF;
  			END IF;
  			oids_array_toapply = translate(oids_array,'{}','()');
  			oids_array_bis_toapply = translate(oids_array_bis,'{}','()');
  			FETCH temp_cursor INTO oids_array;
  		END LOOP;
  		CLOSE temp_cursor;
  		-- pourcentages retournés
  		select_percent = 0;
  		other_percent = 0;
  		
  		selectCmd = 'SELECT round(sum(CASE WHEN oid in ' || oids_array_bis_toapply || ' THEN sum ELSE 0 END) / sum(sum) * 100, 0) , sum(sum) FROM temp_sum_table ';
  		OPEN temp_cursor FOR 
  		EXECUTE selectCmd;  
  		FETCH temp_cursor INTO winteger;
  		WHILE FOUND LOOP
  			IF(winteger is null) THEN
  				winteger = 0;
  			END IF;
  			select_percent = winteger;
  			FETCH temp_cursor INTO oids_array;
  		END LOOP;
  		CLOSE temp_cursor;
  		other_percent = 100 - select_percent;
  		-- Retour des valeurs
  		FOR rec IN 	EXECUTE 'SELECT ''RSS_SOUSSELECT_OID_IN''::text, ''AND ' || oidField|| ' IN ' || oids_array_toapply || '''::text 
  					UNION 
  					SELECT ''RSS_SOUSSELECT_OID_WHENIN''::text, ''' || oidField || ' IN ' || oids_array_toapply || '''::text 
  					UNION 
  					SELECT ''RSS_SOUSSELECT_SELECT_PERCENT''::text, ''' || select_percent || '% ' || onfield_text || '''::text 
  					UNION 
  					SELECT ''RSS_SOUSSELECT_OTHER_PERCENT''::text, ''' || other_percent || '% ' || onfield_text || '''::text 
  					UNION 
  					SELECT ''SOUSSELECT_TODO''::text, ''AND ''''1'''' = ''''1''''''::text
  					' 
  					
  		LOOP
  			RETURN NEXT rec;
  		END LOOP;
  	
  	ELSE
  		
  		FOR rec IN 	EXECUTE 'SELECT ''RSS_SOUSSELECT_OID_IN''::text, ''''::text 
  					UNION 
  					SELECT ''RSS_SOUSSELECT_OID_WHENIN''::text, ''1 = 1''::text
  					UNION 
  					SELECT ''RSS_SOUSSELECT_SELECT_PERCENT''::text, ''100% ' || onfield_text || '''::text 
  					UNION 
  					SELECT ''RSS_SOUSSELECT_OTHER_PERCENT''::text, ''''::text 
  					UNION 
  					SELECT ''SOUSSELECT_TODO''::text, ''AND ''''1'''' <> ''''1''''''::text
  					'
  					
  		LOOP
  			RETURN NEXT rec;
  		END LOOP;
  	END IF;
  	RETURN;
  END;