-- Schema: C6MartPeriodico
-- Stored Procedure: DM_MIFID



  
-- =============================================  
-- Author:  Paolo Giovanetti  
-- Create date: 24/11/2008  
-- Description: Aggiunta chiave primaria autoincrement su tabella di staging   
--    per eliminazione duplicati.  
-- =============================================  
CREATE procedure [C6MartPeriodico].[DM_MIFID]  
AS  
BEGIN  
  
 SET NOCOUNT ON;  
  
 DECLARE @ID_ELAB INT  
 DECLARE @TIPO_ELAB VARCHAR(1)  
  
 SET @ID_ELAB = C6MartPeriodico.GETIDELAB()  
 SET @TIPO_ELAB = C6MartPeriodico.GETTIPOELAB()  
  
 -- Log esecuzione  
 INSERT INTO LOG_ESECUZIONE_PERIODICO  
  (Nome, Inizio, Nota)  
 VALUES (  
  'DM_MIFID',  
  GETDATE(),  
  'Elaborazione trimestrale'  
 )  
  
 TRUNCATE TABLE C6MartPeriodico.MIFID  
  
 INSERT INTO C6MartPeriodico.[MIFID]  
           ([RETE]  
           ,[COD_FISCALE]  
           ,[PROFILO_ASS]  
           ,[DATA_INIZIO_VAL]  
           ,[ID_ELAB]  
           ,[RISKCLASS]  
           ,[EXPERIENCE]   
   ,SCADUTO )  
 SELECT  
  AN.RETE AS RETE,  
  AN.CODFIS AS COD_FISCALE,  
  case when RPF.FLGPROF='P' then 0 else  AN.CODPROF end AS PROFILO_ASS,  
  CASE  
   WHEN AN.DTINVAL = 0 THEN CASE  
    WHEN AN.CODPROF = 0 THEN NULL  
    ELSE GETDATE()  
   END  
   ELSE CAST(CAST(AN.DTINVAL AS VARCHAR(8)) AS DATETIME)  
  END AS DATA_INIZIO_VAL,  
  @ID_ELAB AS ID_ELAB,  
        CLASSeRC,  
   CASE   
   WHEN   
   RPF.FLGPROF='P' then -1 else COD_PROFEC
	end,  
--  CASE   
--   WHEN datediff(dd,   
--    case when isnull(AN.DTINVAL,0) <> 0    
--    then CAST(CAST(AN.DTINVAL AS VARCHAR(8))  AS DATETIME)  
--    else CAST('19900101' as DATETIME)  
--    END  
--    , c6martperiodico.gettrimestre2(getdate(),0))/365.25<3 THEN 0  
--   ELSE 1  
--  END AS SCADUTO  
  CASE   
   WHEN RPF.FLGPROF='P' then 2 
   when ISNULL(AN.CODPROF,0)>0 THEN 0 
   ELSE 1  
  END AS SCADUTO  
  
 FROM  
 C6StagingPeriodico.WSEIAN2 AS AN  
 LEFT JOIN  C6MartPeriodico.wseirpf RPF
 ON AN.RETE=RPF.RETE AND AN.CODFIS=RPF.CODFIS 
  --C6StagingPeriodico.ALL_CF_PB AS AL  
  --LEFT OUTER JOIN C6StagingPeriodico.WSEIAN AS AN  
   --ON AL.RETE_PB = AN.RETE  
   --AND AL.CODICEFISCALE = AN.CODFIS  
 WHERE AN.DTTRIM = (  
  SELECT MAX(DTTRIM)  
  FROM C6StagingPeriodico.WSEIAN2 )  
        AND AN.CODMAN=''  
UNION   
SELECT  
  AN.RETE AS RETE,  
  'FF@' + AN.CODMAN AS COD_FISCALE, --AN.CODFIS AS COD_FISCALE,  
  case when RPF.FLGPROF='P' then 0 else  AN.CODPROF end AS PROFILO_ASS,  
  CASE  
   WHEN AN.DTINVAL = 0 THEN CASE  
    WHEN AN.CODPROF = 0 THEN NULL  
    ELSE GETDATE()  
   END  
   ELSE CAST(CAST(AN.DTINVAL AS VARCHAR(8)) AS DATETIME)  
  END AS DATA_INIZIO_VAL,  
  @ID_ELAB AS ID_ELAB,  
        classErc,  
   case when RPF.FLGPROF='P' then -1 else COD_PROFEC
   end, 
--  CASE   
--   WHEN datediff(dd,   
--    case when isnull(AN.DTINVAL,0) <> 0    
--    then CAST(CAST(AN.DTINVAL AS VARCHAR(8))  AS DATETIME)  
--    else CAST('19900101' as DATETIME)  
--    END  
--    , c6martperiodico.gettrimestre2(getdate(),0))/365.25<3 THEN 0  
--   ELSE 1  
--  END AS SCADUTO  
  CASE
  WHEN RPF.FLGPROF='P' then 2 
   WHEN   
    ISNULL(AN.CODPROF,0)>0 THEN 0   
   ELSE 1  
  END AS SCADUTO  
 FROM  
  C6StagingPeriodico.WSEIAN2 AS AN  
   LEFT JOIN  C6MartPeriodico.wseirpf RPF
 ON AN.RETE=RPF.RETE AND AN.CODFIS=RPF.CODFIS AND AN.CODMAN=RPF.CODMAN
  --C6StagingPeriodico.ALL_CF_PB AS AL  
  --LEFT OUTER JOIN C6StagingPeriodico.WSEIAN AS AN  
  -- ON AL.RETE_PB = AN.RETE  
  -- AND SUBSTRING(AL.CODICEFISCALE,4,4) = AN.CODMAN  
 WHERE AN.DTTRIM = (  
  SELECT MAX(DTTRIM)  
  FROM C6StagingPeriodico.WSEIAN2 )  
        AND AN.CODMAN <> ''   
        --AND AL.CODICEFISCALE LIKE '%@%'  
  
 -- Log esecuzione  
 UPDATE  
  LOG_ESECUZIONE_PERIODICO  
 SET  
  Fine = GETDATE(),  
  Tipo = 'INSERT',  
  Righe = @@ROWCOUNT  
 WHERE  
  Nome = 'DM_MIFID' AND  
  Inizio = (  
   SELECT 
