﻿-- Stored procedure
-- =============================================
-- Author:		<Alessandro Tringali>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
 --[C6MartPeriodico].[PL_D2_S169RischiomercatoRischiocredito] 'S','BLDMCL41L09L378V'
CREATE procedure [C6MartPeriodico].[PL_D2_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
	-- 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.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 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.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 
    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(CK.DESCRIZIONE) AS RISKCLASSMAX,
	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 cod_fiscale =@codiceFiscale
    and rete=@rete
	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 cod_fiscale =@codiceFiscale
    and rete=@rete
	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
	--------------------------------------------- 16/7/2018
	declare @contaPatrimoni smallint
	select @contaPatrimoni = count(*) from #appo where ordine = 1
	if @contaPatrimoni = 0 --manca il patrimonio casa
	   begin
	   select * from (
	   select top 1
			  #appo.RETE,
		      #appo.COD_FISCALE,
		      case when  #appo.RETE= 'F'  then 'Patrimonio Fideuram' 
                   WHEN  #appo.RETE= 'S'  then 'Patrimonio Sanpaolo Invest'
				   when  #appo.RETE= 'W' then 'Patrimonio IW Private Inv.'
			end as INSTITUTENAME,
			0 as CTV,
			#appo.CODICEPROFILO,
			#appo.VARMAX,
			0 AS VAR,
			'n.c.' AS VARSTRING,
			0 as RISKCLASS,
			'n.c.'  as RISKCLASSSTRING,
			#appo.COVERAGE,
			NULL AS COVERAGESTRING,
			#appo.RISKCLASSMAX,
			0.0 as PERCENTAGE,
			1 as ordine,
			0 as Ully_perc
			from #appo
			union
		select * from #appo) risultato order by ordine;
     end
	else
	   select * from #appo order by ordine;
	------------------------------------------ fine 16/7/2018
--6,7,9,13 della appo
--case 
--	when var <= varmax then '1'
--	when var > varmax then '0'
--	else -1
--end as rischio_mercato,
--case
--	when riskclass is null then '1'
--	when riskclass <= convert(int,RISKCLASSMAX) then '1'
--	when riskclass > convert(int,RISKCLASSMAX) then '0'
--	else -1
--end as rischio_credito
END
--USE [C6StampeCentralizzate]
--GO
--/****** Object:  StoredProcedure [C6MartPeriodico].[PL_D2_S169RischiomercatoRischiocredito]    Script Date: 09/02/2022 11:34:55 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- Stored procedure
---- =============================================
---- Author:		<Alessandro Tringali>
---- Create date: <Create Date,,>
---- Description:	<Description,,>
---- =============================================
-- --[C6MartPeriodico].[PL_D2_S169RischiomercatoRischiocredito] 'S','BLDMCL41L09L378V'
--ALTER procedure [C6MartPeriodico].[PL_D2_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
--	-- 16/7/2018
--	into #appo
--	-- fine 16/7/2018
--    FROM
--    (
--    select 
--    PATRBF.RETE,
--    PATRBF.COD_FISCALE,
--    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Fideuram' ELSE 'Patrimonio Sanpaolo Invest' 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.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 Fideuram' ELSE 'Patrimonio Sanpaolo Invest' 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.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 
--    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'
--          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(CK.DESCRIZIONE) AS RISKCLASSMAX,
--	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 cod_fiscale =@codiceFiscale
--    and rete=@rete
--	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 cod_fiscale =@codiceFiscale
--    and rete=@rete
--	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
--	--------------------------------------------- 16/7/2018
--	declare @contaPatrimoni smallint
--	select @contaPatrimoni = count(*) from #appo where ordine = 1
--	if @contaPatrimoni = 0 --manca il patrimonio casa
--	   begin
--	   select * from (
--	   select top 1
--			  #appo.RETE,
--		      #appo.COD_FISCALE,
--		      case when  #appo.RETE= 'F'  then 'Patrimonio Fideuram' 
--                   WHEN  #appo.RETE= 'S'  then 'Patrimonio Sanpaolo Invest'
--			end as INSTITUTENAME,
--			0 as CTV,
--			#appo.CODICEPROFILO,
--			#appo.VARMAX,
--			0 AS VAR,
--			'n.c.' AS VARSTRING,
--			0 as RISKCLASS,
--			'n.c.'  as RISKCLASSSTRING,
--			#appo.COVERAGE,
--			NULL AS COVERAGESTRING,
--			#appo.RISKCLASSMAX,
--			0.0 as PERCENTAGE,
--			1 as ordine,
--			0 as Ully_perc
--			from #appo
--			union
--		select * from #appo) risultato order by ordine;
--     end
--	else
--	   select * from #appo order by ordine;
--	------------------------------------------ fine 16/7/2018
----6,7,9,13 della appo
----case 
----	when var <= varmax then '1'
----	when var > varmax then '0'
----	else -1
----end as rischio_mercato,
----case
----	when riskclass is null then '1'
----	when riskclass <= convert(int,RISKCLASSMAX) then '1'
----	when riskclass > convert(int,RISKCLASSMAX) then '0'
----	else -1
----end as rischio_credito
--END