-- =============================================
-- 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_pre_selective]
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
		(Nome, Inizio, Nota)
	VALUES (
		'DM_MIFID_pre_selective',
		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,
		AN.CODPROF 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,
        COD_PROFEC,
--		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 
				ISNULL(AN.CODPROF,0)>0 THEN 0
			ELSE 1
		END AS SCADUTO
	FROM
	C6StagingPeriodico.WSEIAN2 AS AN
		--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,
		AN.CODPROF 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,
        COD_PROFEC,
--		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 
				ISNULL(AN.CODPROF,0)>0 THEN 0
			ELSE 1
		END AS SCADUTO
	FROM
		C6StagingPeriodico.WSEIAN2 AS AN
		--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
	SET
		Fine = GETDATE(),
		Tipo = 'INSERT',
		Righe = @@ROWCOUNT
	WHERE
		Nome = 'DM_MIFID_pre_selective' AND
		Inizio = (
			SELECT
				MAX(Inizio)
			FROM
				LOG_ESECUZIONE
			WHERE
				Nome = 'DM_MIFID_pre_selective'
		)
END
--select count(*) from c6martperiodico.mifid