-- Schema: C6Mart
-- Stored Procedure: PL_D_S169RischiomercatoRischiocredito



CREATE procedure [C6Mart].[PL_D_S169RischiomercatoRischiocredito]
	-- 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;
 
    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,
--V La copertura viene valorizzata con un valore da 0 da 100
  --  	CASE 
	--	WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.c.' 
--		ELSE 
NULL 
--	END 
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,
	--V
	ORDINE,
	RISK_AGG.Ully_perc
    FROM
    (
    select 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Banca Fideuram' WHEN PATRBF.RETE='S' THEN 'Patrimonio Sanpaolo Invest'  WHEN PATRBF.RETE='W' THEN  'Patrimonio IW Private Inv.' END AS INSTITUTENAME,
    SUM(PATRBF.CTV) AS CTV,
	--V
	1 as ORDINE,
	RISK_AGG.Ully_perc
    from
    C6MART.patrimonio_bf  PATRBF,C6MART.RISCHIO_AGGREGATO RISK_AGG
    where 
    1=1
    and PATRBF.COD_FISCALE = RISK_AGG.COD_FISCALE 
    and PATRBF.rete= RISK_AGG.rete    
    and PATRBF.cod_fiscale = @codiceFiscale
    and PATRBF.rete= @rete
    and PATRBF.CTV > 0
    and
    (
      RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' --or
      --RISK_AGG.COD_AGGREG= 'COMPLESSIVO|TERZI'
    )
    GROUP BY 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
	RISK_AGG.Ully_perc,   
    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Banca Fideuram' WHEN PATRBF.RETE='S' THEN 'Patrimonio Sanpaolo Invest'  WHEN PATRBF.RETE='W' THEN  'Patrimonio IW Private Inv.' 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
    C6MART.PATRIMONIO_TERZI  TERZI,C6MART.RISCHIO_AGGREGATO RISK_AGG
    where 
    1=1
    and TERZI.COD_FISCALE = RISK_AGG.COD_FISCALE 
    and TERZI.rete= RISK_AGG.rete 
    and TERZI.cod_fiscale =@codiceFiscale
    and TERZI.rete=@rete
    and TERZI.CTV > 0
    and
    (
      --RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' or
      RISK_AGG.COD_AGGREG= 'COMPLESSIVO|TERZI'
    )    
    GROUP BY 
    TERZI.RETE,
    TERZI.COD_FISCALE,
    RISK_AGG.Ully_perc

    ) PATRBF
    LEFT OUTER JOIN 
    C6MART.MIFID AS M
    ON 
    PATRBF.COD_FISCALE=M.COD_FISCALE
    AND PATRBF.RETE=M.RETE
	LEFT OUTER JOIN C6MART.CODIFICA_RISCHIO AS CR
	ON M.PROFILO_ASS = CR.PROFILO
    LEFT OUTER JOIN 
    C6MART.CODIFICA_CREDITRISK  CK
    ON 
    M.riskclass=CK.PROFILO
    LEFT OUTER JOIN 
    C6MART.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 Banca Fideuram' 
          WHEN  RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' and PATRBF.RETE= 'S'  then 'Patrimonio Sanpaolo Invest'
		   WHEN  RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' and 
