return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  	RAISE NOTICE '%' , 'Creation table de travail';
  	DROP TABLE IF EXISTS w_rss_ipa
  	;
  	CREATE TEMP TABLE w_rss_ipa 
  	(
  	  rss_id bigint, 
  	  ipa_id bigint, 
  	  est_maitre character(1),
  	  nb_ipa bigint DEFAULT 1
  	)
  	;
  	CREATE INDEX w_rss_ipa_i1 ON w_rss_ipa USING btree (rss_id);
  	CREATE INDEX w_rss_ipa_i2 ON w_rss_ipa USING btree (ipa_id);
  	-----------------------	**
  	RAISE NOTICE '%' , '** : Sejours Hors PMSI = Sejours sans GHM';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id, 
  		'1'::character(1) AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '**'
  	WHERE date_sortie >= '20110101' AND ghm_id = 0
  	;
  	DROP TABLE IF EXISTS w_rss_presta
  	;
  	CREATE TEMP TABLE w_rss_presta AS
  	SELECT
  		rss_id,
  		MAX(CASE WHEN t_prestations.code IN ('D09', 'D11', 'D12', 'D13', 'D15', 'D16') OR t_ghs.code = 9605 THEN 1 ELSE 0 END) AS prestation_d091112131516,
  		MAX(CASE WHEN t_prestations.code IN ('D09', 'D11', 'D12', 'D13', 'D14', 'D15', 'D16') OR (t_prestations.code = 'D11' AND t_ghs.code = 9605) THEN 1 ELSE 0 END) AS prestation_d09111213141516,
  		MAX(CASE WHEN t_prestations.code IN ('D12', 'D13', 'D14','D15', 'D16') THEN 1 ELSE 0 END) AS prestation_d1213141516,
  		MAX(CASE WHEN t_prestations.code IN ('D10') OR t_ghs.code = 9617 THEN 1 ELSE 0 END) AS prestation_d10,
  		MAX(CASE WHEN t_prestations.code IN ('D17','D18','D19','D20','D21','D24') OR t_ghs.code IN (9602,9603,9604) THEN 1 ELSE 0 END) AS prestation_c08oqn,
  		MAX(CASE WHEN t_prestations.code IN ('NN1', 'NN2', 'NN3') THEN 1 ELSE 0 END) AS prestation_nnx
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.t_prestations ON p_rsf_detail.prestation_id = t_prestations.oid
  	JOIN pmsi.t_ghs on p_rsf_detail.ghs_id = t_ghs.oid
  	WHERE (
  		t_prestations.code = ANY(Array['D09', 'D10', 'D11', 'D12', 'D13', 'D14', 'D15', 'D16', 'D17', 'D18', 'D19', 'D20', 'D21', 'D24', 'NN1', 'NN2', 'NN3']) OR
  		t_ghs.code = ANY(Array[9602,9603,9604,9605,9617])
  		) AND 
  		p_rsf_detail.est_ligne_rss = '1'
  	GROUP BY 1
  		
  	;
  	CREATE INDEX w_rss_presta_i1 ON w_rss_presta USING btree (rss_id)
  	;
  	
  	-----------------------	*01
  	RAISE NOTICE '%' , '*01 : Séjours en erreurs sauf dialyses exOQN';
  	-- Séjours avec CM 90, sauf pour les séjours de dialyse en
  	-- centre des établissements privés (RSA repérés à partir des RSFA par les codes actes D09, D11,
  	-- D12, D13, D15 et D16
  	DROP TABLE IF EXISTS w_tmp
  	;
  	CREATE TEMP TABLE w_tmp AS
  	SELECT
  		p_rss.oid AS rss_id
  	FROM pmsi.p_rss
  	JOIN w_rss_presta ON p_rss.oid = w_rss_presta.rss_id AND prestation_d09111213141516  = 1
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement = '1'
  	GROUP BY 1
  	;
  	CREATE INDEX w_tmp_i1 ON w_tmp USING btree (rss_id)
  	;
  	
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '*01'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code LIKE '90%'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	LEFT JOIN w_tmp ON p_rss.oid = w_tmp.rss_id
  	WHERE date_sortie >= '20110101' AND 
  		w_tmp.rss_id IS NULL AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-----------------------	*02
  	RAISE NOTICE '%' , '*02 : Sejours d''IVG dans le secteur exDG';
  	--  GHM 14Z08Z
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '*02'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code = '14Z08Z'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-----------------------	*03
  	-- Exclusions supprimée depuis la version d'avril 2012 des IPA
  	-- Les séances d'entraînement à la dialyse sont désormais intégrées dans l'indicateur C08
  	
  	--  GHM 28Z01, 28Z02 et 28Z03
  	-----------------------	*04
  	RAISE NOTICE '%' , '*04 : Prestations inter etablissements';
  	-- Mode d'entrée et de sortie = 0
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '*04'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		mode_entree = '0' AND
  		mode_sortie = '0' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-----------------------	*05
  	RAISE NOTICE '%' , '*05 : Morts-nes';
  	-- GHM 15Z10E
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '*05'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code = '15Z10E'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-----------------------	*06
  	
  	RAISE NOTICE '%' , '*06 : Sejours de chirurgie esthetique et de confort';
  	-- GHM 09Z02A, 09Z02B, 09Z02Z (Chirurgie esthétique) et 23Z03Z (Interventions de confort et autres
  	-- interventions non prises en charge par l'assurance maladie obligatoire)
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '*06'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code IN ('09Z02A', '09Z02B', '09Z02Z', '23Z03Z')
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-----------------------	*07
  		
  	RAISE NOTICE '%' , '*07 : Sejours realises integralement en UHCD';
  	-- séjours d'un RUM dont l'UM à l'autorisation 07 (Unité d'hospitalisation de courte durée)
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = '*07'
  	JOIN pmsi.t_unites_medicales ON p_rss.unite_medicale_principale_id = t_unites_medicales.oid AND t_unites_medicales.type_autorisation LIKE '07%'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.nb_rum = 1 AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	
  	-----------------------	A01
  	RAISE NOTICE '%' , 'A01 : Diagnostic prenatal';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'A01'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.diagnostic_principal_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAA01D'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	A02
  	RAISE NOTICE '%' , 'A02 : Obstetrique hors IVG';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'A02'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAA02G'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1,2,3
  	;
  	-----------------------	A03
  	-- Désactivé depuis 2014, semble ne plus être pris en compte
  	
  	-----------------------	A04
  	RAISE NOTICE '%' , 'A04 : Neonatalogie et reanimation neonatale';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'A04'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code LIKE '15%'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'A04'
  	JOIN w_rss_presta ON p_rss.oid = w_rss_presta.rss_id AND prestation_nnx = 1
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	A05
  	RAISE NOTICE '%' , 'A05 : Activite clinique d''assistance medicale a la procreation';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'A05'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.diagnostic_principal_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAA05D'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1,2,3
  	;
  	-----------------------	B01
  	RAISE NOTICE '%' , 'B01 : Greffes d''organes';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'B01'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code LIKE '27%'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	B02
  	RAISE NOTICE '%' , 'B02 : Neurochirurgie';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN pmsi.p_rss_actes ON p_rss_actes.rss_id = p_rss.oid
  	JOIN base.t_ipa ON t_ipa.code = 'B02'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss_actes.acte_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAB02A'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	B03
  	RAISE NOTICE '%' , 'B03 : Chirurge cardiaque - GHM';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'B03'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAB03G'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	RAISE NOTICE '%' , 'B03 : Chirurge cardiaque - Actes';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN pmsi.p_rss_actes ON p_rss_actes.rss_id = p_rss.oid
  	JOIN base.t_ipa ON t_ipa.code = 'B03'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.code ILIKE '05C06%'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss_actes.acte_id != ALL (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAB03A'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	B04
  	RAISE NOTICE '%' , 'B04 : Grands brules';
  	-- racine GHM 22Z02 ou racine GHM 22Z03 et diag Princ. dans la liste D-2202
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'B04'
  	JOIN pmsi.t_diagnostics ON p_rss.diagnostic_principal_id = t_diagnostics.oid 
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101'
  		AND (	t_ghm.code LIKE '22Z02%'
  			OR (t_ghm.code LIKE '22Z03%' 
  				AND p_rss.diagnostic_principal_id = ANY (ARRAY
  				(
  					SELECT t_listes_contenu.to_id
  					FROM pmsi.t_listes
  					JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  					WHERE t_listes.code = 'CTIIPAB04D'
  				)::bigint[])
  			)
  		)
  		AND w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1,2,3
  	;
  	-----------------------	B05
  	RAISE NOTICE '%' , 'B05 : Neuro-radiologie';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN pmsi.p_rss_actes ON p_rss_actes.rss_id = p_rss.oid
  	JOIN base.t_ipa ON t_ipa.code = 'B05'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss_actes.acte_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAB05A'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C01
  	
  	RAISE NOTICE '%' , 'C01 : Cardiologie interventionnelle';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN pmsi.p_rss_actes ON p_rss_actes.rss_id = p_rss.oid
  	JOIN base.t_ipa ON t_ipa.code = 'C01'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss_actes.acte_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC01A'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C02
  	RAISE NOTICE '%' , 'C02 : Cancer - Chirurgie sejours';
  	-- GHM EN C ayant pour diagnostic principal C00 à C97, D00 à D09 ou D37 à D48
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C02'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC02G'
  		)::bigint[]) AND
  		p_rss.diagnostic_principal_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC05D'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C03
  	RAISE NOTICE '%' , 'C03 : Cancer - Chimiotherapie sejours';
  	-- Racines GHM 17M05 et 17M06
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C03'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC03G'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C04
  	RAISE NOTICE '%' , 'C04 : Cancer - Chimiotherapie seances';
  	-- Racine GHM 28Z07
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C04'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC04G'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C05
  	RAISE NOTICE '%' , 'C05 : Cancer - Radiotherapie sejours';
  	-- Racine GHM 17K04, 17K05 et 17K06
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C05'
  	JOIN pmsi.t_diagnostics dp ON p_rss.diagnostic_principal_id = dp.oid AND dp.code IN ('Z5100', 'Z5101')
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC05G'
  		)::bigint[]) AND
  		p_rss.diagnostic_relie_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC05D'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C06
  	RAISE NOTICE '%' , 'C06 : Cancer - Cancer - Radiotherapie seances';
  	-- Racine GHM 28Z10, 28Z11, 28Z18, 28Z19, 28Z20, 28Z21, 28Z22, 28Z23, 28Z24, 28Z25
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C06'
  	JOIN pmsi.t_diagnostics dp ON p_rss.diagnostic_principal_id = dp.oid AND dp.code IN ('Z5100', 'Z5101')
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC06G'
  		)::bigint[]) AND
  		p_rss.diagnostic_relie_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC05D'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C07
  	RAISE NOTICE '%' , 'C07 : IRC - Activites dans le champ IPA';
  	-- exOQN prestations D09 D11 D12 D13 D14 D15 et D16
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C07'
  	JOIN w_rss_presta ON p_rss.oid = w_rss_presta.rss_id AND prestation_d09111213141516 = 1
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement = '1'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-- exOQN prestations GHS 9605 et CMD 28
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM 
  		pmsi.p_rss
  		JOIN base.t_ipa ON t_ipa.code = 'C07'
  		JOIN pmsi.t_ghs on p_rss.ghs_id = t_ghs.oid AND t_ghs.code = '9605'
  		JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id 
  		JOIN pmsi.t_cmd ON t_cmd.oid = t_ghm.cmd_id AND t_cmd.code = '28'
  		JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement = '1'
  		LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE 
  		date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	
  	-- Pour les GHS 9605, il faut en fait compmter leur nombre et non pas le nombre de séjours avec ce GHS
  	UPDATE w_rss_ipa SET
  		nb_ipa = subview.nb_ipa
  	FROM (
  			SELECT
  				p_rss.oid as rss_id,
  				count(p_rsf_detail.ghs_id) as nb_ipa
  			FROM
  				pmsi.p_rss
  				JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
  				JOIN pmsi.t_ghs ON t_ghs.oid = p_rsf_detail.ghs_id
  			WHERE 1=1
  				AND p_rss.ghm_id <> 0
  				AND date_sortie >= '20110101'
  				AND t_ghs.code = 9605
  			GROUP BY 1
  		) subview
  	WHERE subview.rss_id = w_rss_ipa.rss_id
  	AND w_rss_ipa.ipa_id = (SELECT oid FROM base.t_ipa WHERE t_ipa.code = 'C07')
  	;
  	
  	--  exDG prestations D12 D13 D14 D15 et D16
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C07'
  	JOIN w_rss_presta ON p_rss.oid = w_rss_presta.rss_id AND prestation_d1213141516 = 1
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement <> '1'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  		AND p_rss.traitement_epmsi IN ('30', '31', '32', '33')
  	GROUP BY 1, 2, 3
  	;
  	--  exDG GHM 28Z04Z et GH 9605 ou 9618
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C07'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.code = '28Z04Z'
  	JOIN pmsi.t_ghs ON t_ghs.oid = p_rss.ghs_id AND t_ghs.code IN ('9605', '9618')
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement <> '1'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-----------------------	C08
  	
  	RAISE NOTICE '%' , 'C08 : Autres activites de dialyse (centre pour enfants et seances d''entrainement)';
  	-- exOQN prestation D10
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C08'
  	JOIN w_rss_presta ON p_rss.oid = w_rss_presta.rss_id AND prestation_d10 = 1
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement = '1'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-- exDG GHM 28Z04Z
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C08'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.code = '28Z04Z'
  	JOIN pmsi.t_ghs ON t_ghs.oid = p_rss.ghs_id AND t_ghs.code = '9617'
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement <> '1'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-- séances d'entrainement exDG
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'C08'
  	JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid AND t_ghm.code IN ('28Z01Z', '28Z02Z', '28Z03Z')
  	JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement <> '1'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-- séances d'entrainement exOQN
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM 
  		pmsi.p_rss
  		JOIN base.t_ipa ON t_ipa.code = 'C08'
  		JOIN w_rss_presta ON p_rss.oid = w_rss_presta.rss_id AND prestation_c08oqn = 1
  		JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement = '1'
  		LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE 
  		date_sortie >= '20110101' AND 
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	-- deuxième passe pour les séances d'entrainement exOQN pour choper les séjours sans facture par exemple ou ceux pour lesquels le GHS n'est pas dans la facture
  	
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre,
  		0
  	FROM 
  		pmsi.p_rss
  		JOIN base.t_ipa ON t_ipa.code = 'C08'
  		JOIN base.t_finess ON p_rss.finess = t_finess.code AND t_finess.type_etablissement = '1'
  		JOIN pmsi.t_ghs ON t_ghs.oid = p_rss.ghs_id AND t_ghs.code = '9604'
  		LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE 
  		date_sortie >= '20110101' AND 
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	;
  	
  	
  	-- Pour les C08, il faut en fait compter le nombre de GHS, d'EMI et de PH8, les autres prestations doivent etre exclues (empirique)
  	UPDATE w_rss_ipa SET
  		nb_ipa = subview.nb_ipa
  	FROM (
  			SELECT
  				p_rss.oid as rss_id,
  				count(*) as nb_ipa
  			FROM
  				pmsi.p_rss
  				JOIN pmsi.p_rss_ipa ON p_rss_ipa.rss_id = p_rss.oid
  				JOIN base.t_ipa ON t_ipa.oid = p_rss_ipa.ipa_id
  				JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
  				JOIN pmsi.t_prestations ON t_prestations.oid = p_rsf_detail.prestation_id
  			WHERE 1=1
  				AND date_sortie BETWEEN '2014-01-01' AND '2014-04-30'
  				AND t_ipa.code = 'C08'
  				AND t_prestations.code In ('GHS', 'EMI', 'PH8')
  			GROUP BY 1
  		) subview
  	WHERE subview.rss_id = w_rss_ipa.rss_id
  	AND w_rss_ipa.ipa_id = (SELECT oid FROM base.t_ipa WHERE t_ipa.code = 'C08')
  	;
  	
  	----------------------- D01
  	RAISE NOTICE '%' , 'D01 : Activite de medecine interventionnelle';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D01'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.ghm3 = 'K'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	----------------------- D02
  	RAISE NOTICE '%' , 'D02 : Medecine en hospitalisation partielle - Sejours';
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D02'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.code NOT LIKE '28%' AND t_ghm.ghm3 IN ('M', 'Z')
  	WHERE date_sortie >= '20110101' AND
  		p_rss.duree_sejour = 0 AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	----------------------- D03
  	RAISE NOTICE '%' , 'D03 : Medecine en hospitalisation partielle - Seances';
  	-- transfusions, oxygénothérpaies, aphérèses et chimio hors cancer
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D03'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.code LIKE '28%' AND t_ghm.ghm3 IN ('M', 'Z')
  	WHERE date_sortie >= '20110101' AND
  		p_rss.duree_sejour = 0 AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid AND 
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAD03G'
  		)::bigint[])
  	GROUP BY 1, 2, 3
  	;
  	-- radiothérapie hors cancer (inverse C06)
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D03'
  	JOIN pmsi.t_diagnostics dp ON p_rss.diagnostic_principal_id = dp.oid AND dp.code IN ('Z5100', 'Z5101')
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	WHERE date_sortie >= '20110101' AND 
  		p_rss.ghm_id = ANY (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC06G'
  		)::bigint[]) AND
  		p_rss.diagnostic_relie_id != ALL (ARRAY
  		(
  			SELECT t_listes_contenu.to_id
  			FROM pmsi.t_listes
  			JOIN pmsi.t_listes_contenu ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE t_listes.code = 'CTIIPAC05D'
  		)::bigint[]) AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	----------------------- D04
  	RAISE NOTICE '%' , 'D04 : Medecine en hospitalisation complete';
  	-- tous les séjours de médecine avec une nuit au moins
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D04'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.ghm3 IN ('M', 'Z')
  	WHERE date_sortie >= '20110101' AND
  		p_rss.duree_sejour > 0 AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	----------------------- D05
  	
  	RAISE NOTICE '%' , 'D05 : Chirurgie ambulatoire hors transferts, mutations ou deces';
  	DROP TABLE IF EXISTS w_tmp
  	;
  	CREATE TEMP TABLE w_tmp AS
  	SELECT
  		p_rss.oid AS rss_id
  	FROM
  		pmsi.p_rss
  	WHERE 1=1
  		AND (mode_sortie = 9
  				OR (mode_sortie = 7 AND destination = 6)
  				OR (mode_sortie = 7 AND destination = 2)
  				OR (mode_sortie = 7 AND destination = 4)
  				OR (mode_sortie = 6 AND destination = 6)
  				OR (mode_sortie = 6 AND destination = 2)
  				OR (mode_sortie = 6 AND destination = 4)
  			)
  		AND date_sortie >= '20110101'
  	;
  	CREATE INDEX w_tmp_i1 ON w_tmp USING btree (rss_id)
  	;
  	
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D05'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.ghm3 = 'C'
  	LEFT JOIN w_tmp ON w_tmp.rss_id = p_rss.oid
  	WHERE date_sortie >= '20110101' AND
  		p_rss.duree_sejour = 0 AND
  		w_tmp.rss_id IS NULL AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	----------------------- D06
  	
  	RAISE NOTICE '%' , 'D06 : Chirurgie en hospitalisation complete ou sans nuitee avec transfert, mutation ou deces';
  	-- Chirurgie hospit complète
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D06'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.ghm3 = 'C'
  	WHERE date_sortie >= '20110101' AND
  		duree_sejour > 0 AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-- Chirurgie sans nuitee avec transfert, mutation ou deces
  	INSERT INTO w_rss_ipa 
  	SELECT
  		p_rss.oid AS rss_id,
  		t_ipa.oid AS ipa_id,
  		CASE WHEN w_rss_ipa.rss_id IS NULL THEN '1' ELSE '0' END AS maitre
  	FROM pmsi.p_rss
  	JOIN base.t_ipa ON t_ipa.code = 'D06'
  	LEFT JOIN w_rss_ipa ON p_rss.oid = w_rss_ipa.rss_id AND est_maitre = '1'
  	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id AND t_ghm.ghm3 = 'C'
  	LEFT JOIN w_tmp ON w_tmp.rss_id = p_rss.oid
  	WHERE date_sortie >= '20110101' AND
  		p_rss.duree_sejour = 0 AND
  		w_tmp.rss_id IS DISTINCT FROM NULL AND
  		w_rss_ipa.ipa_id IS DISTINCT FROM t_ipa.oid
  	GROUP BY 1, 2, 3
  	;
  	-- ############################################################################
  	
  	TRUNCATE pmsi.p_rss_ipa;
  	INSERT INTO pmsi.p_rss_ipa 
  	(
  		rss_id,
  		ipa_id,
  		est_maitre,
  		nb_ipa
  	)
  	SELECT
  		rss_id,
  		ipa_id,
  		est_maitre,
  		nb_ipa
  	FROM w_rss_ipa;
  	DROP TABLE IF EXISTS w_rss_ipa
  	;
  	DROP TABLE IF EXISTS w_tmp
  	;
  		
  	RETURN 'OK';
  END;