-- Schema: C6MartPeriodico
-- Stored Procedure: PL_MP_S141AnalisiRisparmioTable


 
--EXEC [C6martPeriodico].PL_MP_S141AnalisiRisparmioTable  'F','FTSVSL35E25Z115H'
CREATE procedure [C6MartPeriodico].[PL_MP_S141AnalisiRisparmioTable]
	@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 
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 
