return: text
lang: plpgsql
src: |
  DECLARE	
  BEGIN
  	-- Table des codes cancéro
  	
  	DROP TABLE IF EXISTS w_etude_6;
  	CREATE TEMP TABLE w_etude_6 AS
  	SELECT 
      ascii(substr(t_etude_6.code, 4, 1)) + ascii(substr(t_etude_6.code, 5, 1)) + ascii(substr(t_etude_6.code, 6, 1)) + ascii(substr(t_etude_6.code, 5, 2)) AS oid,
  		t_etude_6.code,
  		t_etude_6.texte,
  		t_etude_6.texte_court,
  		t_etude_6.activite_id,
  		t_etude_6.activite_code,
  		t_etude_6.activite_texte
  	FROM pmsi.t_etude_6
  	GROUP BY 1,2,3,4,5,6,7
  	ORDER BY 1;
  	DROP TABLE IF EXISTS w_rss_CTI000007D;
  	CREATE TEMP TABLE w_rss_CTI000007D AS
  	SELECT p_rss_diagnostics.rss_id
  	FROM pmsi.p_rss_diagnostics
  	WHERE (p_rss_diagnostics.diagnostic_id IN (
  		SELECT v_listes_3.to_id
  		FROM pmsi.v_listes_3
  		WHERE v_listes_3.liste_code = 'CTI000007D'::text))
  	GROUP BY 1
  	;
  	CREATE INDEX w_rss_CTI000007Di1 ON w_rss_CTI000007D USING btree	(rss_id);
  	
  	TRUNCATE pmsi.p_rss_cancero;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  	w_etude_6.code = 'CHIR1'::text AND 
  	p_rss.age >= 18::numeric AND 
  	(p_rss.ghm_id IN (
  		SELECT v_listes_3.to_id
  		FROM pmsi.v_listes_3
  		WHERE v_listes_3.liste_code = 'CTI2017051G'::text)) AND 
  	(p_rss.diagnostic_principal_id IN (
  		SELECT v_listes_3.to_id
  		FROM pmsi.v_listes_3
  		WHERE v_listes_3.liste_code = 'CTI002002D'::text))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIR2'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.ghm_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI2017051G'::text)) AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002004D'::text))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIR3'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.ghm_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI2017052G'::text)) AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002003D'::text))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIR4'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.ghm_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI2017051G'::text)) AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002005D'::text))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIR5'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.ghm_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI2017053G'::text)) AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002006D'::text))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIR6'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.ghm_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI2017051G'::text)) AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002001D'::text))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'RAD1'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002008D'::text)) AND 
  		(p_rss.oid IN (
  			SELECT p_rss_diagnostics.rss_id
  			FROM pmsi.p_rss_diagnostics
  			WHERE (p_rss_diagnostics.diagnostic_id IN (
  				SELECT v_listes_3.to_id
  				FROM pmsi.v_listes_3
  				WHERE v_listes_3.liste_code = 'CTI002007D'::text))))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIM1'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002009D'::text)) AND 
  		(p_rss.oid IN (
  			SELECT p_rss_diagnostics.rss_id
  			FROM pmsi.p_rss_diagnostics
  			WHERE (p_rss_diagnostics.diagnostic_id IN (
  				SELECT v_listes_3.to_id
  				FROM pmsi.v_listes_3
  				WHERE v_listes_3.liste_code = 'CTI002007D'::text))))
  	;
  	INSERT INTO pmsi.p_rss_cancero
  	(
  		cdc,
  		cancero_id,
  		cancero_code,
  		cancero_texte,
  		cancero_texte_court,
  		activite_cancero_id,
  		activite_cancero_code,
  		activite_cancero_texte,
  		rss_id
  	)
  	SELECT '2017'::text AS cdc,
  		w_etude_6.oid AS cancero_id,
  		w_etude_6.code AS cancero_code,
  		w_etude_6.texte AS cancero_texte,
  		w_etude_6.texte_court AS cancero_texte_court,
  		w_etude_6.activite_id AS activite_cancero_id,
  		w_etude_6.activite_code AS activite_cancero_code,
  		w_etude_6.activite_texte AS activite_cancero_texte,
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss,
  		w_etude_6
  	WHERE p_rss.etat <> 'S'::bpchar AND 
  		w_etude_6.code = 'CHIM2'::text AND 
  		p_rss.age >= 18::numeric AND 
  		(p_rss.ghm_id IN (
  			SELECT t_ghm.oid
  			FROM pmsi.t_ghm, pmsi.t_cmd
  			WHERE t_ghm.cmd_id = t_cmd.oid AND (t_cmd.code::text = ANY (ARRAY['24'::character varying::text, '28'::character varying::text])))) AND 
  		(p_rss.diagnostic_principal_id IN (
  			SELECT v_listes_3.to_id
  			FROM pmsi.v_listes_3
  			WHERE v_listes_3.liste_code = 'CTI002009D'::text)) AND 
  		(p_rss.oid IN ( SELECT p_rss_diagnostics.rss_id
  			FROM pmsi.p_rss_diagnostics
  			WHERE (p_rss_diagnostics.diagnostic_id IN (
  				SELECT v_listes_3.to_id
  				FROM pmsi.v_listes_3
  				WHERE v_listes_3.liste_code = 'CTI002007D'::text))));
  	
  	ANALYSE  pmsi.p_rss_cancero;
  	
  	RETURN 'OK';
  END;