--EXEC [C6martPeriodico].PL_MP_S141AnalisiRisparmioTable_test  'S','BNOSDR61T03F205W'
CREATE procedure [C6MartPeriodico].[PL_MP_S141AnalisiRisparmioTable_test]
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	SET NOCOUNT ON;
declare @patr_compl decimal(18,3)
select @patr_compl=sum(ctv_prodotto) from 
c6martperiodico.patrimonio_emittenti 
where  cod_fiscale =@codicefiscale
and rete=@rete
and ctv_prodotto > 0
SELECT  --U.*
	COD_FISCALE,
	RETE,
	RATING_AAA,
	RATING_AAp,
	RATING_AA,
	RATING_AAm,
	RATING_Ap,
	RATING_A,
	RATING_Am,
	RATING_BBBp,
	RATING_BBB,
	RATING_BBBm,
	RATING_BBp,
	RATING_BB,
	RATING_BBm,
	RATING_Bp,
	RATING_B,
	RATING_Bm,
	RATING_CCCp,
	RATING_CCC,
	RATING_CCCm,
	RATING_CC,
	RATING_C,
	RATING_D,
	RATING_na,
	FLG_RISKCLASS1,
	FLG_RISKCLASS2,
	FLG_RISKCLASS3,
	FLG_RISKCLASS4,
	FLG_RISKCLASS5,
	FLG_RISKCLASS6,
	FLG_RISKCLASS7,
	FLG_RISKCLASS8,
	FLG_RISKCLASS9,
	FLG_RISKCLASS10,
	INTERMEDIARIO,
	ISSUER,
	STOCKSCounterValue,
	bondsCounterValue,
	CONCENTRATION_ISSUER,
	CONCENTRATION,
	NC 
	into #tabtemp
FROM  (
-- PER INTERMEDIARIO 
    SELECT
		COD_FISCALE,
		RETE,
		MAX(RATING_AAA) AS RATING_AAA,
		MAX(RATING_AAp) AS RATING_AAp,
		MAX(RATING_AA) AS RATING_AA,
		MAX(RATING_AAm) AS RATING_AAm,
		MAX(RATING_Ap) AS RATING_Ap,
		MAX(RATING_A) AS RATING_A,
		MAX(RATING_Am) AS RATING_Am,
		MAX(RATING_BBBp) AS RATING_BBBp,
		MAX(RATING_BBB) AS RATING_BBB,
		MAX(RATING_BBBm) AS RATING_BBBm,
		MAX(RATING_BBp) AS RATING_BBp,
		MAX(RATING_BB) AS RATING_BB,
		MAX(RATING_BBm) AS RATING_BBm,
		MAX(RATING_Bp) AS RATING_Bp,
		MAX(RATING_B) AS RATING_B,
		MAX(RATING_Bm) AS RATING_Bm,
		MAX(RATING_CCCp) AS RATING_CCCp,
		MAX(RATING_CCC) AS RATING_CCC,
		MAX(RATING_CCCm) AS RATING_CCCm,
		MAX(RATING_CC) AS RATING_CC,
		MAX(RATING_C) AS RATING_C,
		MAX(RATING_D) AS RATING_D,
		MAX(RATING_na) AS RATING_na,
		MAX(FLG_RISKCLASS1) AS FLG_RISKCLASS1,
		MAX(FLG_RISKCLASS2) AS FLG_RISKCLASS2,
		MAX(FLG_RISKCLASS3) AS FLG_RISKCLASS3,
		MAX(FLG_RISKCLASS4) AS FLG_RISKCLASS4,
		MAX(FLG_RISKCLASS5) AS FLG_RISKCLASS5,
		MAX(FLG_RISKCLASS6) AS FLG_RISKCLASS6,
		MAX(FLG_RISKCLASS7) AS FLG_RISKCLASS7,
		MAX(FLG_RISKCLASS8) AS FLG_RISKCLASS8,
		MAX(FLG_RISKCLASS9) AS FLG_RISKCLASS9,
		MAX(FLG_RISKCLASS10) AS FLG_RISKCLASS10,
		--V nel caso sia null è BF???
		isnull(INTERMEDIARIO,'BF') AS INTERMEDIARIO,
		EMITTENTE AS ISSUER,
		--RATING AS RATING, 
		SUM(STOCKSCounterValue) AS  STOCKSCounterValue,
		sum(bondsCounterValue) AS bondsCounterValue,
		0 as  CONCENTRATION_ISSUER ,
		--V normalizzo la percentuale per essere rappresentato sul report
		MAX(CONCENTRATION)* 100 AS CONCENTRATION,
--EN 26012016: solo se per un emittente nessun prodotto riporta nessuna copertura
--             viene impostato nc=1 (non classificato) e si assume che nessuna riskclass
--             sia valorizzato.
--		sum(case when copertura > 0 then 0 else 1 end) as nc
		min(case when copertura > 0 then 0 else 1 end) as nc
	FROM
				(SELECT --Y.*   
					V.COD_FISCALE,
					V.RETE,
					INTERMEDIARIO,
					CREDITRISKCLASS,
					EMITTENTE,
					--RATING AS RATING,                  
					CASE WHEN RATING = 'AAA' THEN 1 ELSE 0 END AS RATING_AAA,
					CASE WHEN RATING = 'AA+' THEN 1 ELSE 0 END AS RATING_AAp,
					CASE WHEN RATING = 'AA' THEN 1 ELSE 0 END AS RATING_AA,
					CASE WHEN RATING = 'AA-' THEN 1 ELSE 0 END AS RATING_AAm,
					CASE WHEN RATING = 'A+' THEN 1 ELSE 0 END AS RATING_Ap,
					CASE WHEN RATING = 'A' THEN 1 ELSE 0 END AS RATING_A,
					CASE WHEN RATING = 'A-' THEN 1 ELSE 0 END AS RATING_Am,
					CASE WHEN RATING = 'BBB+' THEN 1 ELSE 0 END AS RATING_BBBp,
					CASE WHEN RATING = 'BBB' THEN 1 ELSE 0 END AS RATING_BBB,
					CASE WHEN RATING = 'BBB-' THEN 1 ELSE 0 END AS RATING_BBBm,
					CASE WHEN RATING = 'BB+' THEN 1 ELSE 0 END AS RATING_BBp,
					CASE WHEN RATING = 'BB' THEN 1 ELSE 0 END AS RATING_BB,
					CASE WHEN RATING = 'BB-' THEN 1 ELSE 0 END AS RATING_BBm,
					CASE WHEN RATING = 'B+' THEN 1 ELSE 0 END AS RATING_Bp,
					CASE WHEN RATING = 'B' THEN 1 ELSE 0 END AS RATING_B,
					CASE WHEN RATING = 'B-' THEN 1 ELSE 0 END AS RATING_Bm,
					CASE WHEN RATING = 'CCC+' THEN 1 ELSE 0 END AS RATING_CCCp,
					CASE WHEN RATING = 'CCC' THEN 1 ELSE 0 END AS RATING_CCC,
					CASE WHEN RATING = 'CCC-' THEN 1 ELSE 0 END AS RATING_CCCm,
					CASE WHEN RATING = 'CC' THEN 1 ELSE 0 END AS RATING_CC,
					CASE WHEN RATING = 'C' THEN 1 ELSE 0 END AS RATING_C,
					CASE WHEN RATING = 'D' THEN 1 ELSE 0 END AS RATING_D,
					CASE WHEN RATING is null THEN 1 ELSE 0 END AS RATING_na,                
					CASE WHEN CREDITRISKCLASS ='Rischio 1/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS1,
					CASE WHEN CREDITRISKCLASS ='Rischio 2/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS2,
					CASE WHEN CREDITRISKCLASS ='Rischio 3/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS3,
					CASE WHEN CREDITRISKCLASS ='Rischio 4/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS4,
					CASE WHEN CREDITRISKCLASS ='Rischio 5/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS5,
					CASE WHEN CREDITRISKCLASS ='Rischio 6/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS6,
					CASE WHEN CREDITRISKCLASS ='Rischio 7/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS7,
					CASE WHEN CREDITRISKCLASS ='Rischio 8/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS8,
					CASE WHEN CREDITRISKCLASS ='Rischio 9/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS9,
					CASE WHEN CREDITRISKCLASS ='Rischio 10/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS10,
					SUM(CTV_AZ_OB_EMIT) as CTV_AZ_OB_EMIT,
					SUM(CTV_PRODOTTO) AS CTV_PRODOTTO,
					sum(CTV_AZIONARIO) AS STOCKSCounterValue,
					sum(CTV_OBBLIGAZIONARIO) AS bondsCounterValue,
					0 AS  CONCENTRATION_ISSUER ,
					SUM(CTV_AZ_OB_EMIT_INTER)/SUM(CTV_COMPL_NOCC_NEG)  AS CONCENTRATION,
					sum(copertura) as copertura
				FROM  C6MARTPERIODICO.PATRIMONIO_EMITTENTI V  
					LEFT JOIN C6MARTPERIODICO.ANAG_PRODOTTI ANAG
					ON V.COD_PRODOTTO = ANAG.COD_PRODOTTO
				WHERE 1=1
					AND V.COD_FISCALE = @CODICEFISCALE
					AND V.RETE = @RETE
					AND (CTV_AZIONARIO >0 OR CTV_OBBLIGAZIONARIO > 0)
				GROUP BY
					V.COD_FISCALE,
					V.RETE,
					INTERMEDIARIO,
					EMITTENTE,
					RATING,
					CREDITRISKCLASS 
				 ) Y
	GROUP BY 
		 COD_FISCALE,
		 RETE,
		 INTERMEDIARIO,
		 EMITTENTE
--V devo rappresentare solo le 3 principali me le ordine e nel codice scorro per le prime 3 dopo aver filtrato per intermediario
--ORDER BY CONCENTRATION DESC
UNION 
------COMPLESSIVO
   SELECT
     COD_FISCALE,
     RETE,
     MAX(RATING_AAA) AS RATING_AAA,
		MAX(RATING_AAp) AS RATING_AAp,
		MAX(RATING_AA) AS RATING_AA,
		MAX(RATING_AAm) AS RATING_AAm,
		MAX(RATING_Ap) AS RATING_Ap,
		MAX(RATING_A) AS RATING_A,
		MAX(RATING_Am) AS RATING_Am,
		MAX(RATING_BBBp) AS RATING_BBBp,
		MAX(RATING_BBB) AS RATING_BBB,
		MAX(RATING_BBBm) AS RATING_BBBm,
		MAX(RATING_BBp) AS RATING_BBp,
		MAX(RATING_BB) AS RATING_BB,
		MAX(RATING_BBm) AS RATING_BBm,
		MAX(RATING_Bp) AS RATING_Bp,
		MAX(RATING_B) AS RATING_B,
		MAX(RATING_Bm) AS RATING_Bm,
		MAX(RATING_CCCp) AS RATING_CCCp,
		MAX(RATING_CCC) AS RATING_CCC,
		MAX(RATING_CCCm) AS RATING_CCCm,
		MAX(RATING_CC) AS RATING_CC,
		MAX(RATING_C) AS RATING_C,
		MAX(RATING_D) AS RATING_D,
		MAX(RATING_na) AS RATING_na,
     MAX(FLG_RISKCLASS1) AS FLG_RISKCLASS1,
     MAX(FLG_RISKCLASS2) AS FLG_RISKCLASS2,
     MAX(FLG_RISKCLASS3) AS FLG_RISKCLASS3,
     MAX(FLG_RISKCLASS4) AS FLG_RISKCLASS4,
	 MAX(FLG_RISKCLASS5) AS FLG_RISKCLASS5,
	 MAX(FLG_RISKCLASS6) AS FLG_RISKCLASS6,
	 MAX(FLG_RISKCLASS7) AS FLG_RISKCLASS7,
	 MAX(FLG_RISKCLASS8) AS FLG_RISKCLASS8,
	 MAX(FLG_RISKCLASS9) AS FLG_RISKCLASS9,
	 MAX(FLG_RISKCLASS10) AS FLG_RISKCLASS10,
	 isnull(INTERMEDIARIO,'BF') AS INTERMEDIARIO,
     EMITTENTE AS ISSUER,
     SUM(STOCKSCounterValue) AS  STOCKSCounterValue,
     SUM(bondsCounterValue) AS bondsCounterValue,
     SUM(isnull(bondsCounterValue,0)+ isnull(STOCKSCounterValue,0))/ max(@patr_compl)  *100  CONCENTRATION_ISSUER ,
     0 AS CONCENTRATION,
--EN 26012016
--	sum(case when copertura > 0 then 0 else 1 end) as nc
	min(case when copertura > 0 then 0 else 1 end) as nc
FROM
(
SELECT  --L.*
	COD_FISCALE,
	RETE,
	'COMPLESSIVO' as INTERMEDIARIO,
	CREDITRISKCLASS,
	EMITTENTE,
	RATING_AAA,
	RATING_AAp,
	RATING_AA,
	RATING_AAm,
	RATING_Ap,
	RATING_A,
	RATING_Am,
	RATING_BBBp,
	RATING_BBB,
	RATING_BBBm,
	RATING_BBp,
	RATING_BB,
	RATING_BBm,
	RATING_Bp,
	RATING_B,
	RATING_Bm,
	RATING_CCCp,
	RATING_CCC,
	RATING_CCCm,
	RATING_CC,
	RATING_C,
	RATING_D,
	RATING_na,
	FLG_RISKCLASS1,
	FLG_RISKCLASS2,
	FLG_RISKCLASS3,
	FLG_RISKCLASS4,
	FLG_RISKCLASS5,
	FLG_RISKCLASS6,
	FLG_RISKCLASS7,
	FLG_RISKCLASS8,
	FLG_RISKCLASS9,
	FLG_RISKCLASS10,
	SUM(CTV_AZ_OB_EMIT) CTV_AZ_OB_EMIT,
	SUM(STOCKSCounterValue) AS STOCKSCounterValue,
	SUM(bondsCounterValue) AS bondsCounterValue,
	0 AS CONCENTRATION_ISSUER,
	SUM(CTV_AZ_OB_EMIT_INTER)/SUM(CTV_COMPLessivo) TOT ,
	SUM(COPERTURA) AS COPERTURA
FROM
(
		SELECT --V.*     
			V.COD_FISCALE,
			V.RETE,
			'COMPLESSIVO' as INTERMEDIARIO,
			CREDITRISKCLASS,
			EMITTENTE,            
			CASE WHEN RATING = 'AAA' THEN 1 ELSE 0 END AS RATING_AAA,
			CASE WHEN RATING = 'AA+' THEN 1 ELSE 0 END AS RATING_AAp,
			CASE WHEN RATING = 'AA' THEN 1 ELSE 0 END AS RATING_AA,
			CASE WHEN RATING = 'AA-' THEN 1 ELSE 0 END AS RATING_AAm,
			CASE WHEN RATING = 'A+' THEN 1 ELSE 0 END AS RATING_Ap,
			CASE WHEN RATING = 'A' THEN 1 ELSE 0 END AS RATING_A,
			CASE WHEN RATING = 'A-' THEN 1 ELSE 0 END AS RATING_Am,
			CASE WHEN RATING = 'BBB+' THEN 1 ELSE 0 END AS RATING_BBBp,
			CASE WHEN RATING = 'BBB' THEN 1 ELSE 0 END AS RATING_BBB,
			CASE WHEN RATING = 'BBB-' THEN 1 ELSE 0 END AS RATING_BBBm,
			CASE WHEN RATING = 'BB+' THEN 1 ELSE 0 END AS RATING_BBp,
			CASE WHEN RATING = 'BB' THEN 1 ELSE 0 END AS RATING_BB,
			CASE WHEN RATING = 'BB-' THEN 1 ELSE 0 END AS RATING_BBm,
			CASE WHEN RATING = 'B+' THEN 1 ELSE 0 END AS RATING_Bp,
			CASE WHEN RATING = 'B' THEN 1 ELSE 0 END AS RATING_B,
			CASE WHEN RATING = 'B-' THEN 1 ELSE 0 END AS RATING_Bm,
			CASE WHEN RATING = 'CCC+' THEN 1 ELSE 0 END AS RATING_CCCp,
			CASE WHEN RATING = 'CCC' THEN 1 ELSE 0 END AS RATING_CCC,
			CASE WHEN RATING = 'CCC-' THEN 1 ELSE 0 END AS RATING_CCCm,
			CASE WHEN RATING = 'CC' THEN 1 ELSE 0 END AS RATING_CC,
			CASE WHEN RATING = 'C' THEN 1 ELSE 0 END AS RATING_C,
			CASE WHEN RATING = 'D' THEN 1 ELSE 0 END AS RATING_D,
			CASE WHEN RATING is null THEN 1 ELSE 0 END AS RATING_na, 
			CASE WHEN CREDITRISKCLASS ='Rischio 1/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS1,
			CASE WHEN CREDITRISKCLASS ='Rischio 2/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS2,
			CASE WHEN CREDITRISKCLASS ='Rischio 3/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS3,
			CASE WHEN CREDITRISKCLASS ='Rischio 4/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS4,
			CASE WHEN CREDITRISKCLASS ='Rischio 5/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS5,
			CASE WHEN CREDITRISKCLASS ='Rischio 6/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS6,
			CASE WHEN CREDITRISKCLASS ='Rischio 7/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS7,
			CASE WHEN CREDITRISKCLASS ='Rischio 8/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS8,
			CASE WHEN CREDITRISKCLASS ='Rischio 9/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS9,
			CASE WHEN CREDITRISKCLASS ='Rischio 10/10' THEN 1 ELSE 0 END AS FLG_RISKCLASS10,
			SUM(CTV_AZ_OB_EMIT) as CTV_AZ_OB_EMIT,
			SUM(CTV_AZIONARIO) AS STOCKSCounterValue,
			sum(CTV_OBBLIGAZIONARIO) AS bondsCounterValue,
			0 AS  CONCENTRATION_ISSUER ,
			SUM(CTV_AZ_OB_EMIT_INTER) CTV_AZ_OB_EMIT_INTER,
			SUM(CTV_COMPLessivo)  AS CTV_COMPLessivo,
			MAX(copertura) as copertura
		FROM  C6MARTPERIODICO.PATRIMONIO_EMITTENTI V  
				LEFT JOIN C6MARTPERIODICO.ANAG_PRODOTTI ANAG
				ON V.COD_PRODOTTO = ANAG.COD_PRODOTTO
		WHERE 
			1=1
			AND V.COD_FISCALE = @CODICEFISCALE
			AND V.RETE = @RETE
			AND (CTV_AZIONARIO >0 OR CTV_OBBLIGAZIONARIO > 0)
		GROUP BY
			V.COD_FISCALE,
			V.RETE,
			EMITTENTE,
			RATING,
			INTERMEDIARIO,
			CREDITRISKCLASS
		) Y
GROUP BY 
	COD_FISCALE,
	RETE,
	INTERMEDIARIO,
	CREDITRISKCLASS,
	EMITTENTE,
	RATING_AAA,
RATING_AAp,
RATING_AA,
RATING_AAm,
RATING_Ap,
RATING_A,
RATING_Am,
RATING_BBBp,
RATING_BBB,
RATING_BBBm,
RATING_BBp,
RATING_BB,
RATING_BBm,
RATING_Bp,
RATING_B,
RATING_Bm,
RATING_CCCp,
RATING_CCC,
RATING_CCCm,
RATING_CC,
RATING_C,
RATING_D,
RATING_na,
	FLG_RISKCLASS1,
	FLG_RISKCLASS2,
	FLG_RISKCLASS3,
	FLG_RISKCLASS4,
	FLG_RISKCLASS5,
	FLG_RISKCLASS6,
	FLG_RISKCLASS7,
	FLG_RISKCLASS8,
	FLG_RISKCLASS9,
	FLG_RISKCLASS10
) L
GROUP BY 
     COD_FISCALE,
     RETE,
     INTERMEDIARIO,
     EMITTENTE
) U
 ORDER BY U.CONCENTRATION DESC
SELECT  distinct
	temp.COD_FISCALE,
	temp.RETE,
	temp.RATING_AAA,
	temp.RATING_AAp,
	temp.RATING_AA,
	temp.RATING_AAm,
	temp.RATING_Ap,
	temp.RATING_A,
	temp.RATING_Am,
	temp.RATING_BBBp,
	temp.RATING_BBB,
	temp.RATING_BBBm,
	temp.RATING_BBp,
	temp.RATING_BB,
	temp.RATING_BBm,
	temp.RATING_Bp,
	temp.RATING_B,
	temp.RATING_Bm,
	temp.RATING_CCCp,
	temp.RATING_CCC,
	temp.RATING_CCCm,
	temp.RATING_CC,
	temp.RATING_C,
	temp.RATING_D,
	temp.RATING_na,
	temp.FLG_RISKCLASS1,
	temp.FLG_RISKCLASS2,
	temp.FLG_RISKCLASS3,
	temp.FLG_RISKCLASS4,
	temp.FLG_RISKCLASS5,
	temp.FLG_RISKCLASS6,
	temp.FLG_RISKCLASS7,
	temp.FLG_RISKCLASS8,
	temp.FLG_RISKCLASS9,
	temp.FLG_RISKCLASS10,
	temp.INTERMEDIARIO,
	temp.ISSUER,
	temp.STOCKSCounterValue,
	--temp.bondsCounterValue,
	Case 
    when 
    pemitt.cod_prodotto like '%IT0003972996%' or
	pemitt.cod_prodotto like '%XS0257293828%'or
	pemitt.cod_prodotto like '%IT0004867781%'or
	pemitt.cod_prodotto like '%IT0004880933%'or
	pemitt.cod_prodotto like '%XS0302580880%'or
	pemitt.cod_prodotto like '%IT0004119407%'or
	pemitt.cod_prodotto like '%IT0004281504%'or
	pemitt.cod_prodotto like '%IT0004931405%'or
	pemitt.cod_prodotto like '%IT0004966856%'or
	pemitt.cod_prodotto like '%IT0004539786%'or
	pemitt.cod_prodotto like '%IT0004657786%'or
	pemitt.cod_prodotto like '%IT0004556327%'or
	pemitt.cod_prodotto like '%IT0004064538%'or
	pemitt.cod_prodotto like '%IT0004119605%'or
	pemitt.cod_prodotto like '%IT0004291891%' 
     THEN   999999999  else temp.bondsCounterValue end
     as bondsCounterValue,
	temp.CONCENTRATION_ISSUER,
	temp.CONCENTRATION,
	temp.NC 
from #tabtemp temp
inner join C6MARTPERIODICO.PATRIMONIO_EMITTENTI pemitt
ON temp.COD_FISCALe= pemitt.COD_FISCALe and
	temp.RETE = pemitt.rete
END