-- Stored procedure
-- =============================================
-- Authors:		<Alessandro Tringali, Valerio Colaianni>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
-- [C6MartPeriodico].[PL_S102DistribuzioneRischioCredito] 'F','94009110068'
CREATE procedure [C6MartPeriodico].[PL_S102DistribuzioneRischioCredito_OLD]
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
--QUERY PER INTERMEDIARIO
SELECT 
RETE,
COD_FISCALE,
INTERMEDIARIO,
CASE WHEN INTERMEDIARIO  IN ( 'Banca Fideuram', 'Sanpaolo Invest') THEN 1
     ELSE 2 END AS ORDINE,
SUM(CTV)  AS CTV_TOT,
SUM(CASE WHEN RISKCLASS = 'NON COPERTO' THEN CTV ELSE 0 END)  AS CTV_notCovered,
SUM(CASE WHEN RISKCLASS = 'Classe 1' THEN CTV ELSE 0 END)  AS CTV_Class1,
SUM(CASE WHEN RISKCLASS = 'Classe 2' THEN CTV ELSE 0 END)  AS CTV_Class2,
SUM(CASE WHEN RISKCLASS = 'Classe 3' THEN CTV ELSE 0 END)  AS CTV_Class3,
SUM(CASE WHEN RISKCLASS = 'Oltre Classe 3' THEN CTV ELSE 0 END)  AS CTV_overClass3,
SUM(CASE WHEN RISKCLASS = 'NA' THEN CTV ELSE 0 END)  AS CTV_na,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'NON COPERTO' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_notCovered,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Classe 1' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_class1,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Classe 2' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_class2,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Classe 3' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_class3,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Oltre Classe 3' THEN CTV ELSE 0 END)/SUM(CTV))*100 END   AS perc_overClass3,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'NA' THEN CTV ELSE 0 END) /sum(ctv))*100  END as PERC_NA
FROM
(
 -- Start 1.1
SELECT 
PATRBF.RETE,
PATRBF.COD_FISCALE,
CASE	
	WHEN RISCHIO_PROD.COPERTURA = 0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END AS RISKCLASS,
CASE WHEN PATRBF.RETE = 'F' THEN 'Banca Fideuram' ELSE 'Sanpaolo Invest' END AS INTERMEDIARIO,
--SUM(CASE WHEN PATRBF.ID_AREA = 'Cc' AND PATRBF.CTV < 0 THEN 0 ELSE PATRBF.CTV END) AS CTV
SUM(CASE WHEN ctvcc.cc_ctv <0 and patrbf.id_area='cc' THEN 0 ELSE PATRBF.CTV END) AS ctv
FROM
[C6Martperiodico].PATRIMONIO_BF PATRBF
---Per calcolare la somma algebrica dei cc 
LEFT OUTER JOIN
(select cod_fiscale, rete, sum(ctv) as cc_ctv
from
[C6MARTPERIODICO].pATRIMONIO_BF 
where id_Area='Cc'
group by cod_fiscale, rete
) ctvcc
on 
patrbf.cod_fiscale=ctvcc.cod_fiscale
and
patrbf.rete=ctvcc.rete
--fine calcolo somma cc
LEFT OUTER JOIN
[C6Martperiodico].RISCHIO_AGGREGATO RISCHIO_PROD 
ON 
	PATRBF.RETE= RISCHIO_PROD.RETE
	AND PATRBF.COD_FISCALE= RISCHIO_PROD.COD_FISCALE
	AND RISCHIO_PROD.COD_AGGREG = 
			CASE 
				WHEN (PATRBF.NOME_PROGETTO IS NOT NULL)
					THEN 'RISFIN|PIRAMIDE|' + LEFT(PATRBF.ID_AREA,3) + '|' + PATRBF.NOME_PROGETTO+ '|'+ isnull(cast(PATRBF.ordinamento_progetto as varchar),'')+'|'
				--V
				WHEN (PATRBF.NOME_PROGETTO IS NULL) AND PATRBF.ID_AREA IN ('EXT','INV','PRE','RIS','LIQ')
					THEN 'RISFIN|PIRAMIDE|' + LEFT(PATRBF.ID_AREA,3) + '|' 
				WHEN (PATRBF.NOME_PROGETTO IS NULL) AND PATRBF.ID_AREA IN ('CC')
					THEN 'COMPLESSIVO|BF|CC|' + LEFT(PATRBF.ID_AREA,3) + '|'  
				WHEN (PATRBF.NOME_PROGETTO IS NULL) AND PATRBF.ID_AREA IN ('NA')
					THEN 'RISFIN|' + LEFT(PATRBF.ID_AREA,3) + '|'  
				--
			END	+ PATRBF.POSITION_ID 
--INNER JOIN C6MartPeriodico.CODIFICA_CREDITRISK CR
LEFT outer JOIN C6MartPeriodico.CODIFICA_CREDITRISK CR
ON RISCHIO_PROD.CREDITRISK = cr.profilo
--V
WHERE PATRBF.COD_FISCALE = @CodiceFiscale AND
		PATRBF.RETE = @Rete
--
GROUP BY 
PATRBF.RETE,
PATRBF.COD_FISCALE,
CASE	
	WHEN RISCHIO_PROD.COPERTURA = 0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END,
CASE WHEN PATRBF.RETE = 'F' THEN 'FIDEURAM' ELSE 'SANPAOLO' END
--- End 1.1
UNION ALL
-- Start 1.2
SELECT 
PATRTERZI.RETE,
PATRTERZI.COD_FISCALE,
CASE	
	WHEN RISCHIOPROD.COPERTURA = 0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END  AS RISKCLASS,
PATRTERZI.INTERMEDIARIO,
SUM(ROUND(CTV,2)) AS CTV
FROM 
[C6Martperiodico].PATRIMONIO_TERZI PATRTERZI
LEFT OUTER JOIN 
[C6Martperiodico].RISCHIO_AGGREGATO RISCHIOPROD
		ON 
			PATRTERZI.RETE = RISCHIOPROD.RETE
			AND PATRTERZI.COD_FISCALE = RISCHIOPROD.COD_FISCALE
			AND RISCHIOPROD.COD_AGGREG ='TIPOPRODTERZI|TERZI|' +ISNULL(PATRTERZI.DESCR_GRUPPO_PRODOTTO, PATRTERZI.TIPO_PRODOTTO)  + '|'+ PATRTERZI.COD_PRODOTTO_TERZI
left outer JOIN [C6Martperiodico].CODIFICA_CREDITRISK CR
ON RISCHIOPROD.CREDITRISK = cr.profilo
--V
WHERE PATRTERZI.COD_FISCALE = @CodiceFiscale AND
		PATRTERZI.RETE = @Rete
--
GROUP BY 
PATRTERZI.RETE,
PATRTERZI.COD_FISCALE,
PATRTERZI.INTERMEDIARIO,
CASE	
	WHEN RISCHIOPROD.COPERTURA = 0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END
-- End 1.2
)H
GROUP BY
RETE,
COD_FISCALE,
INTERMEDIARIO
--------
UNION ALL
-----------
--QUERY TOTALE
SELECT 
RETE,
COD_FISCALE,
'Complessivo' AS INTERMEDIARIO,
3 AS ORDINE,
SUM(CTV)  AS CTV_TOT,
SUM(CASE WHEN RISKCLASS = 'NON COPERTO' THEN CTV ELSE 0 END)  AS CTV_notCovered,
SUM(CASE WHEN RISKCLASS = 'Classe 1' THEN CTV ELSE 0 END)  AS CTV_Class1,
SUM(CASE WHEN RISKCLASS = 'Classe 2' THEN CTV ELSE 0 END)  AS CTV_Class2,
SUM(CASE WHEN RISKCLASS = 'Classe 3' THEN CTV ELSE 0 END)  AS CTV_Class3,
SUM(CASE WHEN RISKCLASS = 'Oltre Classe 3' THEN CTV ELSE 0 END)  AS CTV_overClass3,
SUM(CASE WHEN RISKCLASS ='NA' THEN CTV ELSE 0 END)  AS CTV_na,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'NON COPERTO' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_notCovered,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Classe 1' THEN CTV ELSE 0 END)/SUM(CTV))*100 END AS PERC_class1,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Classe 2' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_class2,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Classe 3' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS PERC_class3,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'Oltre Classe 3' THEN CTV ELSE 0 END)/SUM(CTV))*100  END AS perc_overClass3,
CASE WHEN SUM(CTV)=0 THEN 0 ELSE (SUM(CASE WHEN RISKCLASS = 'NA' THEN CTV ELSE 0 END) /sum(ctv))*100 END as PERC_NA
FROM
(
--- Start 2.1
SELECT 
PATRBF.RETE,
PATRBF.COD_FISCALE,
CASE	
	WHEN RISCHIO_PROD.COPERTURA =0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END  AS RISKCLASS,
--SUM(CASE WHEN PATRBF.ID_AREA = 'Cc' AND PATRBF.CTV < 0 THEN 0 ELSE PATRBF.CTV END) AS CTV
SUM(CASE WHEN ctvcc.cc_ctv <0 and patrbf.id_area='cc'  THEN 0 ELSE PATRBF.CTV END) AS CTV
FROM
[C6Martperiodico].PATRIMONIO_BF PATRBF
--per fare la somma algebrica dei cc
LEFT OUTER JOIN
(select cod_fiscale, rete, sum(ctv) as cc_ctv
from
[C6MARTPERIODICO].pATRIMONIO_BF 
where id_Area='Cc'
group by cod_fiscale, rete
) ctvcc
on 
patrbf.cod_fiscale=ctvcc.cod_fiscale
and
patrbf.rete=ctvcc.rete
--fine somma cc
LEFT OUTER JOIN
[C6Martperiodico].RISCHIO_AGGREGATO RISCHIO_PROD 
ON 
	PATRBF.RETE= RISCHIO_PROD.RETE
	AND PATRBF.COD_FISCALE= RISCHIO_PROD.COD_FISCALE
	AND RISCHIO_PROD.COD_AGGREG = 
			CASE 
				WHEN (PATRBF.NOME_PROGETTO IS NOT NULL)
					THEN 'RISFIN|PIRAMIDE|' + LEFT(PATRBF.ID_AREA,3) + '|' + PATRBF.NOME_PROGETTO+ '|'++ isnull(cast(PATRBF.ordinamento_progetto as varchar),'')+'|'
				--V
				WHEN (PATRBF.NOME_PROGETTO IS NULL) AND PATRBF.ID_AREA IN ('EXT','INV','PRE','RIS','LIQ')
					THEN 'RISFIN|PIRAMIDE|' + LEFT(PATRBF.ID_AREA,3) + '|' 
				WHEN (PATRBF.NOME_PROGETTO IS NULL) AND PATRBF.ID_AREA IN ('CC')
					THEN 'COMPLESSIVO|BF|CC|' + LEFT(PATRBF.ID_AREA,3) + '|'  
				WHEN (PATRBF.NOME_PROGETTO IS NULL) AND PATRBF.ID_AREA IN ('NA')
					THEN 'RISFIN|' + LEFT(PATRBF.ID_AREA,3) + '|'  
				--
			END	+ PATRBF.POSITION_ID 
left outer JOIN [C6Martperiodico].CODIFICA_CREDITRISK CR
ON RISCHIO_PROD.CREDITRISK = cr.profilo
--V
WHERE PATRBF.COD_FISCALE = @CodiceFiscale AND
		PATRBF.RETE = @Rete
--
GROUP BY 
PATRBF.RETE,
PATRBF.COD_FISCALE,
CASE	
	WHEN RISCHIO_PROD.COPERTURA = 0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END
-- End 2.1
UNION ALL
-- Start 2.2
SELECT 
PATRTERZI.RETE,
PATRTERZI.COD_FISCALE,
CASE	
	WHEN RISCHIOPROD.COPERTURA =0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END  AS RISKCLASS,
SUM(ROUND(CTV,2)) AS CTV
FROM 
[C6Martperiodico].PATRIMONIO_TERZI PATRTERZI
LEFT OUTER JOIN 
[C6Martperiodico].RISCHIO_AGGREGATO RISCHIOPROD
		ON 
			PATRTERZI.RETE = RISCHIOPROD.RETE
			AND PATRTERZI.COD_FISCALE = RISCHIOPROD.COD_FISCALE
			AND RISCHIOPROD.COD_AGGREG ='TIPOPRODTERZI|TERZI|' + ISNULL(PATRTERZI.DESCR_GRUPPO_PRODOTTO, PATRTERZI.TIPO_PRODOTTO)  + '|'+ PATRTERZI.COD_PRODOTTO_TERZI
left outer  JOIN [C6Martperiodico].CODIFICA_CREDITRISK CR
ON RISCHIOPROD.CREDITRISK = cr.profilo
--V
WHERE PATRTERZI.COD_FISCALE = @CodiceFiscale AND
		PATRTERZI.RETE = @Rete
--
GROUP BY 
PATRTERZI.RETE,
PATRTERZI.COD_FISCALE,
CASE	
	WHEN RISCHIOPROD.COPERTURA =0 
		THEN 'NON COPERTO'
	WHEN DESCRIZIONE IS NULL
		THEN 'NA'
	ELSE DESCRIZIONE	
END
--- End 2.2
)H
GROUP BY
RETE,
COD_FISCALE
END