﻿-- =============================================  
-- Author:  Paolo Giovanetti  / TRINGALI PER FIDUCIARIE  
-- Create date: <Create Date,,>  
-- Description: <Description,,>  
-- =============================================  
CREATE procedure [C6Mart].[RP_DM_BrsClienteESG]  
AS  
BEGIN  
 SET NOCOUNT ON;  
 -- Log esecuzione  
 INSERT INTO LOG_ESECUZIONE  
  (Nome, Inizio, Nota)  
 VALUES (  
  'RP_DM_BrsClienteESG',  
  GETDATE(),  
  'Elaborazione giornaliera'  
 )  
-- declare @data varchar(8)
--set @data= [C6MartPeriodico].[getTrimestre2](getdate(),0)
truncate table C6Mart.BrsClienteESG
insert into [C6StampeCentralizzate].[C6Mart].[BrsClienteESG]
SELECT esg.[rete]
      ,esg.[Codice_Fiscale]
      ,case when isnull(Sost_E,'N')='N' and isnull(Sost_S,'N')='N' and isnull(Sost_G,'N')='N' then 0 else 1 end as [Preferenza_esg_cliente]
      ,case when Sost_E='S' then 'Y' 
			when Sost_E='N' then 'N' 
			else 'N' end as 
			[ESG_E]
      ,case when Sost_S='S' then 'Y' 
			when Sost_S='N' then 'N' 
			else 'N' end as 
			[ESG_S]
      ,case when Sost_G='S' then 'Y' 
			when Sost_G='N' then 'N' 
			else 'N' end as [ESG_G]
      ,esg.PesoSostESG as [obiettivo_ESG_minimo]
      ,NULL as [ESG_Rating]
      ,esg.SostPercE as [ESG_E_perc]
      ,esg.SostPercS as [ESG_S_perc]
      ,esg.SostPercG as [ESG_G_perc]
  FROM  C6Mart.SPB_W6CLIESG esg
  --SELECT [rete]
  --   -- ,[codfis]
  --    ,[Preferenza_esg_cliente]
  --    ,[ESG_E]
  --    ,[ESG_S]
  --    ,[ESG_G]
  --    ,[obiettivo_ESG_minimo]
  --    ,[ESG_Rating]
  --    ,[ESG_E_perc]
  --    ,[ESG_S_perc]
  --    ,[ESG_G_perc]
  --FROM [C6StampeCentralizzate].[C6StagingPeriodico].[ESG_Metriche_Cliente_IL]
  --select * from [C6StampeCentralizzate].[C6StagingPeriodico].[ESG_Metriche_Cliente_IL]
  --select * from C6StagingPeriodico.wseian2
  --select * from C6MartPeriodico.W6CLIESG
 UPDATE  
  LOG_ESECUZIONE  
 SET  
  Fine = GETDATE(),  
  Tipo = 'INSERT',  
  Righe = @@ROWCOUNT  
 WHERE  
  Nome = 'RP_DM_BrsClienteESG' AND  
  Inizio = (  
   SELECT  
    MAX(Inizio)  
   FROM  
    LOG_ESECUZIONE  
   WHERE  
    Nome = 'RP_DM_W6CATESG'  
  )  
END