<?xml version="1.0" encoding="ISO-8859-1"?>
<INDICATOR title="Intégrité tables"  
	name="iCTI_pmsi_integrite_tables"
	softCode="iCTI_pmsi"
	eisOnly="false"
	type="CTIsql" 
	indicatorKey="" 
	linkView="PMSI000084"
>
	<reportTitle><![CDATA[Intégrité tables [CONTEXT] [STATUT]]]></reportTitle>
	<reportDescription></reportDescription>
	<SQL database="iCTI">
		<select><![CDATA[		
						DROP TABLE IF EXISTS w_count;
						CREATE TEMP TABLE w_count (tablename text, nb numeric)
						;
						INSERT INTO w_count VALUES('p_rss',(SELECT count(*) FROM pmsi.p_rss WHERE etat <> 'S'))
						;
						INSERT INTO w_count VALUES('v_rss_1',(SELECT count(*) FROM pmsi.v_rss_1))
						;
						INSERT INTO w_count VALUES('p_rss_rum',(SELECT count(*) FROM pmsi.p_rss_rum))
						;
						INSERT INTO w_count VALUES('v_rss_rum_1',(SELECT count(*) FROM pmsi.v_rss_rum_1))
						;
						INSERT INTO w_count VALUES('p_rss_diagnostics',(SELECT count(*) FROM pmsi.p_rss_diagnostics))
						;
						INSERT INTO w_count VALUES('v_rss_diagnostics_1',(SELECT count(*) FROM pmsi.v_rss_diagnostics_1))
						;
						INSERT INTO w_count VALUES('p_rss_actes',(SELECT count(*) FROM pmsi.p_rss_actes))
						;
						INSERT INTO w_count VALUES('v_rss_actes_3',(SELECT count(*) FROM pmsi.v_rss_actes_3))
						;
						INSERT INTO w_count VALUES('p_rss_lpp',(SELECT count(*) FROM pmsi.p_rss_lpp))
						;
						INSERT INTO w_count VALUES('v_rss_lpp_1',(SELECT count(*) FROM pmsi.v_rss_lpp_1))
						;
						INSERT INTO w_count VALUES('p_rss_ucd',(SELECT count(*) FROM pmsi.p_rss_ucd JOIN pmsi.t_type_ucd ON t_type_ucd.oid = p_rss_ucd.type_ucd_id))
						;
						INSERT INTO w_count VALUES('v_rss_ucd_1',(SELECT count(*) FROM pmsi.v_rss_ucd_1))
						;
						INSERT INTO w_count VALUES('p_rsf_detail',(SELECT count(*) FROM pmsi.p_rsf_detail WHERE type = 'B' AND est_ligne_rss = '1'))
						;
						INSERT INTO w_count VALUES('v_rsf_detail_1',(SELECT count(*) FROM pmsi.v_rsf_detail_1))
						;
						INSERT INTO w_count VALUES('p_rsf_detail_h',(SELECT count(*) FROM pmsi.p_rsf_detail WHERE type = 'C' AND est_ligne_rss = '1'))
						;
						INSERT INTO w_count VALUES('v_rsf_detail_6',(SELECT count(*) FROM pmsi.v_rsf_detail_1))
						;



						SELECT
							now() AS date,
							count_p_rss - count_v_rss +
							count_p_rss_rum - count_v_rss_rum +
							count_p_rss_diagnostics - count_v_rss_diagnostics +
							count_p_rss_actes - count_v_rss_actes +
							count_p_rss_ucd - count_v_rss_ucd +
							count_p_rss_lpp - count_v_rss_lpp +
							count_p_rsf_detail - count_v_rsf_detail 
								AS ecart_total,
							count_p_rss,
							count_v_rss,
							count_p_rss - count_v_rss AS ecart_rss,
							count_p_rss_rum,
							count_v_rss_rum,
							count_p_rss_rum - count_v_rss_rum AS ecart_rss_rum,
							count_p_rss_diagnostics,
							count_v_rss_diagnostics,
							count_p_rss_diagnostics - count_v_rss_diagnostics AS ecart_rss_diagnostics,
							count_p_rss_actes,
							count_v_rss_actes,
							count_p_rss_actes - count_v_rss_actes AS ecart_rss_actes,
							count_p_rss_lpp,
							count_v_rss_lpp,
							count_p_rss_lpp - count_v_rss_lpp AS ecart_rss_lpp,
							count_p_rss_ucd,
							count_v_rss_ucd,
							count_p_rss_ucd - count_v_rss_ucd AS ecart_rss_ucd,
							count_p_rsf_detail,
							count_v_rsf_detail,
							count_p_rsf_detail - count_v_rsf_detail AS ecart_rsf_detail,
							count_p_rsf_detail_h,
							count_v_rsf_detail_h,
							count_p_rsf_detail_h - count_v_rsf_detail_h AS ecart_rsf_detail_h
					FROM
						(
						SELECT 
							MAX(CASE WHEN tablename = 'p_rss' THEN nb ELSE 0 END) AS count_p_rss,
							MAX(CASE WHEN tablename = 'v_rss_1' THEN nb ELSE 0 END) AS count_v_rss,
							MAX(CASE WHEN tablename = 'p_rss_rum' THEN nb ELSE 0 END) AS count_p_rss_rum,
							MAX(CASE WHEN tablename = 'v_rss_rum_1' THEN nb ELSE 0 END) AS count_v_rss_rum,
							MAX(CASE WHEN tablename = 'p_rss_diagnostics' THEN nb ELSE 0 END) AS count_p_rss_diagnostics,
							MAX(CASE WHEN tablename = 'v_rss_diagnostics_1' THEN nb ELSE 0 END) AS count_v_rss_diagnostics,
							MAX(CASE WHEN tablename = 'p_rss_actes' THEN nb ELSE 0 END) AS count_p_rss_actes,
							MAX(CASE WHEN tablename = 'v_rss_actes_3' THEN nb ELSE 0 END) AS count_v_rss_actes,
							MAX(CASE WHEN tablename = 'p_rss_lpp' THEN nb ELSE 0 END) AS count_p_rss_lpp,
							MAX(CASE WHEN tablename = 'v_rss_lpp_1' THEN nb ELSE 0 END) AS count_v_rss_lpp,
							MAX(CASE WHEN tablename = 'p_rss_ucd' THEN nb ELSE 0 END) AS count_p_rss_ucd,
							MAX(CASE WHEN tablename = 'v_rss_ucd_1' THEN nb ELSE 0 END) AS count_v_rss_ucd,
							MAX(CASE WHEN tablename = 'p_rsf_detail' THEN nb ELSE 0 END) AS count_p_rsf_detail,
							MAX(CASE WHEN tablename = 'v_rsf_detail_1' THEN nb ELSE 0 END) AS count_v_rsf_detail,
							MAX(CASE WHEN tablename = 'p_rsf_detail_h' THEN nb ELSE 0 END) AS count_p_rsf_detail_h,
							MAX(CASE WHEN tablename = 'v_rsf_detail_6' THEN nb ELSE 0 END) AS count_v_rsf_detail_h
						FROM w_count
						) subview
		]]></select>
	</SQL>
	<ALERT rule_compareField="ecart_rss" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rss" />
	<ALERT rule_compareField="ecart_rss_rum" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rss_rum" />
	<ALERT rule_compareField="ecart_rss_diagnostics" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rss_diagnostics" />
	<ALERT rule_compareField="ecart_rss_actes" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rss_actes" />
	<ALERT rule_compareField="ecart_rss_lpp" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rss_lpp" />
	<ALERT rule_compareField="ecart_rss_ucd" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rss_ucd" />
	<ALERT rule_compareField="ecart_rsf_detail" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rsf_detail" />
	<ALERT rule_compareField="ecart_rsf_detail_h" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="léger écart (inférieur à 10) d'intégrité sur table p_rsf_detail_h" />

	<ALERT rule_compareField="ecart_rss" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rss" />
	<ALERT rule_compareField="ecart_rss_rum" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rss_rum" />
	<ALERT rule_compareField="ecart_rss_diagnostics" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rss_diagnostics" />
	<ALERT rule_compareField="ecart_rss_actes" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rss_actes" />
	<ALERT rule_compareField="ecart_rss_lpp" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rss_lpp" />
	<ALERT rule_compareField="ecart_rss_ucd" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rss_ucd" />
	<ALERT rule_compareField="ecart_rsf_detail" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rsf_detail" />
	<ALERT rule_compareField="ecart_rsf_detail_h" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_rsf_detail_h" />

	<ALERT rule_compareField="ecart_total" rule_compareOperator="=" rule_compareValue="0" result="OK" resultText="Intégrité CORRECTE" />

</INDICATOR>
