CREATE procedure [C6MartPeriodico].[RP_DM_IndicatoreRischio]
as 
begin
DECLARE @DataFineTrim VARCHAR(8)  
SET @DataFineTrim = c6martperiodico.getTrimestre2(getDate(),0)
print(@DataFineTrim)
DECLARE @DataTab VARCHAR(8)
SELECT TOP 1 @DataTab = dttrim FROM C6MartPeriodico.Indicatore_Rischio
print(@DataTab)
drop table if exists #appo
drop table if exists #NoCFPatrCasa
drop table if exists #NoPatrCasa
drop table if exists #NoPatrCasaFin
drop table if exists #tot
drop table if exists #finale
    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) / 
    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
	-- 16/7/2018
	into #appo
	-- fine 16/7/2018
    FROM
    (
    select 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio 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
    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' --or
    )
    GROUP BY 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
	RISK_AGG.Ully_perc,   
    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio 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
    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|BF' or
      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'
		  WHEN  RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' and PATRBF.RETE= 'W'  then 'Patrimonio IW Private Inv.'
          else 'Patrimonio altri Istituti' end)= PATRBF.INSTITUTENAME
   where
    (
      RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF' or
      RISK_AGG.COD_AGGREG= 'COMPLESSIVO|TERZI'
    )
    UNION ALL
    SELECT 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
    'Patrimonio Complessivo' AS INSTITUTENAME,
    SUM(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	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,
    MAX(ISNULL(RISK_AGG.COPERTURA,0.00)) AS COVERAGE,
    MAX(   	CASE 
		WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.c.' 
		ELSE NULL 
	END) AS COVERAGESTRING,
	MAX(M.RISKCLASS) AS RISKCLASSMAX,
    100 AS PERCENTAGE,
	--V
	3 AS ORDINE,
	RISK_AGG.Ully_perc
    FROM
    (
    select 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
    SUM(PATRBF.CTV) AS CTV
    from
    c6martperiodico.patrimonio_bf  PATRBF
    where 
    1=1 
	and PATRBF.CTV > 0    
    GROUP BY 
    PATRBF.RETE,
    PATRBF.COD_FISCALE
    UNION
    SELECT 
    TERZI.RETE,
    TERZI.COD_FISCALE,
    SUM(ROUND(TERZI.CTV,2)) AS CTV
    FROM
    c6martperiodico.PATRIMONIO_TERZI  TERZI
        where 
    1=1 
	and TERZI.CTV > 0       
     GROUP BY 
    TERZI.RETE,
    TERZI.COD_FISCALE
    ) 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
        where  
    (
      RISK_AGG.COD_AGGREG= 'COMPLESSIVO'
    )
    GROUP BY 
    PATRBF.RETE,
    PATRBF.COD_FISCALE,
    RISK_AGG.Ully_perc
	--V
	order by ordine
	   --SENZA PATRIMONIO CASA
	   select rete,cod_fiscale 
	   into #NOCFPatrCasa
	   from #appo where ordine<>1
	   except
	      select rete,cod_fiscale from #appo where ordine=1
		select a.* 
		into #NOPatrCasa
		from #appo a inner join #NOCFPatrCasa p on a.RETE=p.RETE and a.COD_FISCALE=p.COD_FISCALE
		 select distinct
			  c.RETE,
		      c.COD_FISCALE,
		      case when  c.RETE= 'F'  then 'Patrimonio Fideuram' 
                   WHEN  c.RETE= 'S'  then 'Patrimonio Sanpaolo Invest'
				   when  c.RETE= 'W' then 'Patrimonio IW Private Inv.'
			end as INSTITUTENAME,
			0 as CTV,
			c.CODICEPROFILO,
			c.VARMAX,
			0 AS VAR,
			'n.c.' AS VARSTRING,
			0 as RISKCLASS,
			'n.c.'  as RISKCLASSSTRING,
			0.000 as COVERAGE,--c.COVERAGE,
			NULL AS COVERAGESTRING,
			c.RISKCLASSMAX,
			0.0 as PERCENTAGE,
			1 as ordine,
			0 as Ully_perc
			into #NoPatrCasaFin
			from #NOPatrCasa c
			select * 
			into #tot
			from #NoPatrCasaFin
			union all 
			select * from #appo where ORDINE=1
			select [RETE]
      ,[COD_FISCALE]
	  ,@DataFineTrim as DTTRIM
      ,[INSTITUTENAME]
      ,[CTV]
      ,[CODICEPROFILO]
      ,[VARMAX]
      ,[VAR]
      ,[VARSTRING]
      ,[RISKCLASS]
      ,[RISKCLASSSTRING]
      ,[COVERAGE]
      ,[COVERAGESTRING]
      ,[RISKCLASSMAX]
      ,[PERCENTAGE]
      ,[ordine]
      ,[Ully_perc],	case when VAR <= VARMAX then 'S'
	when var > VARMAX then 'N'
	else 'X' end as Ind_Rischio
	into #finale
	from #tot
if(isnull(@DataFineTrim,0) <> isnull(@DataTab,0))
BEGIN
truncate table C6MartPeriodico.Indicatore_Rischio
print('OK')
	  insert into  C6MartPeriodico.Indicatore_Rischio
	  select [RETE]
      ,[COD_FISCALE]
	  ,[DTTRIM]
      ,[INSTITUTENAME]
      ,[CTV]
      ,[CODICEPROFILO]
      ,[VARMAX]
      ,[VAR]
      ,[VARSTRING]
      ,[RISKCLASS]
      ,[RISKCLASSSTRING]
      ,[COVERAGE]
      ,[COVERAGESTRING]
      ,[RISKCLASSMAX]
      ,[PERCENTAGE]
      ,[ordine]
      ,[Ully_perc]
	  ,Ind_Rischio
	  from #finale
	select * from C6MartPeriodico.Indicatore_Rischio
	END
	ELSE 
	BEGIN
	delete from C6MartPeriodico.Indicatore_Rischio where RETE+COD_FISCALE in (select RETE+COD_FISCALE from #finale)
	insert into C6MartPeriodico.Indicatore_Rischio
	select [RETE]
      ,[COD_FISCALE]
	  ,[DTTRIM]
      ,[INSTITUTENAME]
      ,[CTV]
      ,[CODICEPROFILO]
      ,[VARMAX]
      ,[VAR]
      ,[VARSTRING]
      ,[RISKCLASS]
      ,[RISKCLASSSTRING]
      ,[COVERAGE]
      ,[COVERAGESTRING]
      ,[RISKCLASSMAX]
      ,[PERCENTAGE]
      ,[ordine]
      ,[Ully_perc]
	  ,Ind_Rischio
	  from #finale
	select * from c6martperiodico.wseian2
	END
END