-- Schema: C6StagingPeriodico
-- Stored Procedure: RP_ST_SPB_CONTR_SINTESI2

 
--select count(*) from c6stagingperiodico.spb_contr_sintesi2
CREATE procedure [C6StagingPeriodico].[RP_ST_SPB_CONTR_SINTESI2]
AS
BEGIN
	 

	--TRUNCATE TABLE [C6StagingPeriodico].[SPB_CONTR_SINTESI2]

	--INSERT INTO [C6StagingPeriodico].[SPB_CONTR_SINTESI2]
	--		   ([POSITIONID]
	--		  ,[DTTRIM]
	--		  ,[DTRIFE]
	--		  ,[RETE]
	--		  ,[CODFIS]
	--		  ,[CODCONF]
	--		  ,[NUMPOL]
	--		  ,[CONTO]
	--		  ,[RUBR]
	--		  ,[CUSTGAR]
	--		  ,[TIPPROD]
	--		  ,[CODINT]
	--		  ,[CODPROD]
	--		  ,[SUBPROD]
	--		  ,[MAF]
	--		  ,[CODABI]
	--		  ,[PCTKEY]
	--		  ,[DESTIT]
	--		  ,[CTV]
	--		  ,[PARTVIA]
	--		  ,[PARTVIAAL]
	--		  ,[DTSOTTOS]
	--		  ,[RENDINIZ]
	--		  ,[RENDANNO]
	--		  ,[VALORE]
	--		  ,[SUBSET]
	--		  ,[CVN]
	--		  ,[SALRISC]
	--		  ,[ISIN])

	SELECT 
		   NULL AS [POSITIONID]
	      ,[DTTRIM]
		  ,[DTRIFE]
          ,[CS].[RETE]
		  ,C6.cod_fiscale AS CODFIS
		  ,[CODCONF]
		  ,[NUMPOL]
		  ,[CONTO]
		  ,case when (tipprod = 'CC' and rubr =' ') then '000' else RUBR end as RUBR
		  ,case when (tipprod = 'CC' and rubr =' ') then '0' else CUSTGAR end as custgar
		  ,[TIPPROD]
		  ,[CODINT]
		  ,[CODPROD]
		  ,[SUBPROD]
		  ,[MAF]
		  ,[CODABI]
		  ,case when (tipprod = 'CC' and rubr =' ') then '0' else PCTKEY end as pctkey
		  ,[DESTIT]
		  ,[CTV]
		  ,[PARTVIA]
		  ,[PARTVIAAL]
		  ,[DTSOTTOS]
		  ,[RENDINIZ]
		  ,[RENDANNO]
		  ,[VALORE]
		  ,[SUBSET]
		  ,[CVN]
		  ,[SALRISC]
		  ,'NA' as ISIN 
    INTO #CS_MENO_K_L191
	FROM C6StagingPeriodico.WSEICS CS
	JOIN C6StagingPeriodico.appo_contratti C6
	ON (case when cs.codman <> '' then 'FF@'+ cs.codman else cs.codfis end) = C6.COd_fiscale
	AND CS.RETE = C6.RETE  
	AND CS.SUBPROD NOT IN ('KE','KS','KW','K1','K2','K3','K4','K5','K6','K7', 'L191')
	AND CS.DTTRIM = c6martperiodico.gettrimestre2(getdate(),0)


	SELECT
		  NULL AS [POSITIONID]
	      ,CS.[DTTRIM]
		  ,CS.[DTRIFE]
          ,[CS].[RETE]
		  ,C6.cod_fiscale AS CODFIS
		  ,CS.[CODCONF]
		  ,[NUMPOL]
		  ,[CONTO]
		  ,case when (tipprod = 'CC' and rubr =' ') then '000' else RUBR end as RUBR
		  ,case when (tipprod = 'CC' and rubr =' ') then '0' else CUSTGAR end as custgar
		  ,CASE WHEN UL.ISIN IS NULL OR UL.SUBPROD = 'CL05' THEN 'ASU1' ELSE 'ASU2' end as TIPPROD
		  ,UL.[CODINT]
		  ,[CODPROD]
		  ,UL.[SUBPROD]
		  ,UL.[MAF]
		  ,[CODABI]
		  ,case when (tipprod = 'CC' and rubr =' ') then '0' else PCTKEY end as pctkey
		  ,[DESTIT]
		  ,UL.[CTV]
		  ,[PARTVIA]
		  ,[PARTVIAAL]
		  ,[DTSOTTOS]
		  ,[RENDINIZ]
		  ,[RENDANNO]
		  ,UL.[CTV] AS VALORE 
		  ,UL.[SUBSET]
		  ,[CVN]
		  ,[SALRISC]
		  ,case when(UL.ISIN is null) then 'NA' else UL.ISIN end as ISIN
	  INTO #UL_1
	  FROM C6StagingPeriodico.WSEICS CS 
	  join C6StagingPeriodico.appo_contratti C6
  		ON CS.RETE = C6.RETE 
		AND (case when cs.codman <> '' then 'FF@'+ ltrim(rtrim(cs.codman)) else cs.codfis end) = C6.COd_fiscale
		AND CS.[DTTRIM] = c6martperiodico.gettrimestre2(getdate(),0)
	  JOIN C6STAGINGPERIODICO.WSEIUL AS UL
		ON	CS.CODCONF = UL.CODCONF
		AND	CS.DTTRIM = UL.DTTRIM
	  WHERE   
	      CS.SUBPROD  IN ('KE','KS','KW','K1') and ul.subprod not in('VAR5','VAR10','VAR15','CL05','LIQUI')

	 
	  SELECT 
		DISTINCT
		   NULL AS [POSITIONID]
	      ,CS.[DTTRIM]
		  ,CS.[DTRIFE]
          ,[CS].[RETE]
		  ,C6.COd_fiscale AS CODFIS
		  ,CS.[CODCONF]
		  ,[NUMPOL]
		  ,[CONTO]
		  ,case when (tipprod = 'CC' and rubr =' ') then '000' else RUBR end as RUBR
		  ,case when (tipprod = 'CC' and rubr =' ') then '0' else CUSTGAR end as custgar
		  ,'ASU1' as TIPPROD
		  ,UL.[CODINT]
		  ,[CODPROD]
		  ,UL.[SUBPROD]
		  ,'NA' as MAF
		  ,[CODABI]
		  ,case when (tipprod = 'CC' and rubr =' ') then '0' else PCTKEY end as pctkey
		  ,[DESTIT]
		  ,SUM(UL.[CTV]) OVER (partition by UL.codConf) as CTV
		  ,[PARTVIA]
		  ,[PARTVIAAL]
		  ,[DTSOTTOS]
		  ,[RENDINIZ]
		  ,[RENDANNO]
		  ,[VALORE]
		  ,UL.[SUBSET]
		  ,[CVN]
		 
