-- Schema: wh
-- Stored Procedure: MERGED_PROCEDURE_S169



CREATE procedure [wh].[MERGED_PROCEDURE_S169]
AS
BEGIN
	SET NOCOUNT ON;

		-- QUERY PATRIMONIO CASA E PATRIMONIO TERZI
	select
		PATRBF.RETE,
		PATRBF.COD_FISCALE,
		PATRBF.INSTITUTENAME,
		CTV,
		M.PROFILO_ASS AS CODICEPROFILO,
		CR.MAX_VAR AS VARMAX,
		RISK_AGG.VAR_PERC_PTF AS VAR ,
		CASE 
			WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.c.' 
			ELSE NULL 
		END AS VARSTRING,
		RISK_AGG.CREDITRISK RISKCLASS,
		CASE 
			WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.c.' 
			WHEN RISK_AGG.creditrisk is null THEN 'n.a.' 
			WHEN RISK_AGG.creditrisk = 0.00 THEN 'n.a.' 
			ELSE NULL 
		END AS RISKCLASSSTRING,
		ISNULL(RISK_AGG.COPERTURA,0.00) AS COVERAGE,
		NULL AS COVERAGESTRING,
		M.RISKCLASS AS RISKCLASSMAX,
		round((SUM(CTV) OVER (PARTITION BY PATRBF.COD_FISCALE,PATRBF.RETE, PATRBF.INSTITUTENAME) / 
								-- 16.07.2011 COLAIANNI CONSOLI GESTIONE DIVISIONE PER ZERO
								CASE
									WHEN ( SUM(CTV) OVER (PARTITION BY PATRBF.RETE, PATRBF.COD_FISCALE) ) = 0 THEN 1
									ELSE ( SUM(CTV) OVER (PARTITION BY PATRBF.RETE, PATRBF.COD_FISCALE) )
								END) * 100, 2) AS PERCENTAGE,
		ORDINE,
		RISK_AGG.Ully_perc
	into #appo
  from
  (
		select 
			PATRBF.RETE,
			PATRBF.COD_FISCALE,
			case
				when PATRBF.RETE = 'F' then 'Patrimonio Fideuram'
				else 'Patrimonio Sanpaolo Invest'
			end AS INSTITUTENAME,
			sum(PATRBF.CTV) as CTV,
			1 as ORDINE,
			RISK_AGG.Ully_perc
		from
			c6martperiodico.patrimonio_bf PATRBF,
			C6MartPeriodico.RISCHIO_AGGREGATO RISK_AGG
		where 
			1=1
			and PATRBF.COD_FISCALE = RISK_AGG.COD_FISCALE 
			and PATRBF.rete= RISK_AGG.rete  
			and PATRBF.CTV > 0
			and RISK_AGG.COD_AGGREG = 'COMPLESSIVO|BF'
		group by
			PATRBF.RETE,
			PATRBF.COD_FISCALE,
			RISK_AGG.Ully_perc,
			case
				when PATRBF.RETE = 'F' then 'Patrimonio Fideuram'
				else 'Patrimonio Sanpaolo Invest'
			end

		union

		SELECT
			TERZI.RETE,
			TERZI.COD_FISCALE,
			'Patrimonio altri Istituti' AS INSTITUTENAME,
			SUM(ROUND(TERZI.CTV,2)) AS CTV,
			--V
			2 as ORDINE,
			RISK_AGG.Ully_perc
		from
			c6martperiodico.PATRIMONIO_TERZI TERZI,
			C6MartPeriodico.RISCHIO_AGGREGATO RISK_AGG
		where 
			1=1
			and TERZI.COD_FISCALE = RISK_AGG.COD_FISCALE 
			and TERZI.rete= RISK_AGG.rete 
			and TERZI.CTV > 0
			and RISK_AGG.COD_AGGREG= 'COMPLESSIVO|TERZI'
		group by
			TERZI.RETE,
			TERZI.COD_FISCALE,
			RISK_AGG.Ully_perc

  ) PATRBF
		LEFT OUTER JOIN c6martperiodico.MIFID AS M
		ON PATRBF.COD_FISCALE = M.COD_FISCALE
		AND PATRBF.RETE = M.RETE
			LEFT OUTER JOIN c6martperiodico.CODIFICA_RISCHIO AS CR
			ON M.PROFILO_ASS = CR.PROFILO
				LEFT OUTER JOIN c6martperiodico.CODIFICA_CREDITRISK CK
				ON M.riskclass = CK.PROFILO
					LEFT OUTER JOIN c6martperiodico.RISCHIO_AGGREGATO RISK_AGG
					ON PATRBF.COD_FISCALE = RISK_AGG.COD_FISCALE
					AND PATRBF.RETE = RISK_AGG.RETE
					AND (case
							when RISK_AGG.COD_AGGREG = 'COMPLESSIVO|BF' and PATRBF.RETE = 'F' then 'Patrimonio Fideuram' 
							when RISK_AGG.COD_AGGREG = 'COMPLESSIVO|BF' and PATRBF.RETE = 'S' then 'Patrimonio Sanpaolo Invest'
							else 'Patrimonio altri Istituti'
						end) = PATRBF.INSTITUTENAME
	  where
		(
		 RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' or
		 RISK_AGG.COD_AGGREG= 'COMPLESSIVO|TERZI'
		)
	-- FINE QUERY PER PATRIMONIO CASA E PATRIMONIO TERZI

	-- SEPARAZIONE DEL PATRIMONIO CASA E DEL PATRIMONIO TERZI
	select * into #ordine1 from #appo where ordine = 1
	select * into #ordine2 from #appo where ordine = 2	

	-- QUERY PATRIMONIO COMPLESSIVO
	  SELECT
		PATRBF.RETE,
		PATRBF.COD_FISCALE,
		'Patrimonio Complessivo' AS INSTITUTENAME,
		SUM(CTV) AS CTV,
		MAX(M.PROFILO_ASS) AS CODICEPROFILO,
		MAX(CR.MAX_VAR) AS VARMAX,
		MAX(RISK_AGG.VAR_PERC_PTF) AS VAR ,
		MAX(CASE 
				WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.c.' 
				ELSE NULL 
			END) AS VARSTRING,
		MAX(RISK_AGG.CREDITRISK) RISKCLASS,
		MAX(case
				WHE
