-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6MartPeriodico].[SRV_SELEZIONA_CAMPIONE_20160412]
AS
BEGIN
--procedura di caricamento campione
DECLARE @appoDataInizioTrimestre varchar(8)
SET @appoDataInizioTrimestre = '20160101'
--SET @appoDataInizioTrimestre = '20151001'
--SET @appoDataInizioTrimestre = '20150701'
DECLARE @appoDataFineTrimestre varchar(8)
SET @appoDataFineTrimestre = '20160331'
--SET @appoDataFineTrimestre = '20151231'
--SET @appoDataFineTrimestre = '20150930'
DECLARE @appoDataInizioTrimPrec varchar(8)
SET @appoDataInizioTrimPrec = '20151001'
--SET @appoDataInizioTrimPrec = '20150701'
--SET @appoDataInizioTrimPrec = '20150401'
DECLARE @appoDataFineTrimPrec varchar(8)
SET @appoDataFineTrimPrec = '20151231'
--SET @appoDataFineTrimPrec = '20150930'
--SET @appoDataFineTrimPrec = '20150630'
-- QUI VA SPECIFICATO IL PERIODO DI EFFETTIVA GENERAZIONE DEI REPORT CHE, IN GENERE, E' IL MESE SUCCESSIVO
-- A QUELLO DI FINE TRIMESTRE
DECLARE @appoDataGenerazioneTrimPrecDa varchar(8)
SET @appoDataGenerazioneTrimPrecDa = '20160101'
--SET @appoDataGenerazioneTrimPrecDa = '20151001'
--SET @appoDataGenerazioneTrimPrecDa = '20150701'
DECLARE @appoDataGenerazioneTrimPrecA varchar(8)
SET @appoDataGenerazioneTrimPrecA = '20160228' -- solo per questa trim. mettiamo 2 mesi di generazione perché in effetti abbiamo finito a fine febbraio 2016
--SET @appoDataGenerazioneTrimPrecA = '20151031'
--SET @appoDataGenerazioneTrimPrecA = '20150731'
DECLARE @appoDataGenerazioneTrimPrecX2Da varchar(8)
SET @appoDataGenerazioneTrimPrecX2Da = '20151001'
--SET @appoDataGenerazioneTrimPrecX2Da = '20150701'
--SET @appoDataGenerazioneTrimPrecX2Da = '20150401'
DECLARE @appoDataGenerazioneTrimPrecX2A varchar(8)
SET @appoDataGenerazioneTrimPrecX2A = '20151031'
--SET @appoDataGenerazioneTrimPrecX2A = '20150731'
--SET @appoDataGenerazioneTrimPrecX2A = '20150430'
TRUNCATE TABLE C6MARTPERIODICO.SELEZIONECAMPIONE
--select * into c6martperiodico.appo_selezione_campione from  C6MARTPERIODICO.SELEZIONECAMPIONE
--0 tutta la popolazione
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
--into c6martperiodico.selezionecampione
from 
(
select distinct bf.cod_fiscale, bf.rete
from
C6MartPeriodico.rp_vcontrattipergenerazionereport bf
) bf
--1  UNA MACRO ASSET
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'UNA MACRO ASSET                                  ' AS DESCRIZIONE,
1 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
--into c6martperiodico.selezionecampione
from 
(
--Effettuato intervento correttivo in quanto venivano estratti i clienti che presentavano 1 "ASSET CLASS" anzichè 1 "MACRO ASSET CLASS"
--select distinct bf.cod_fiscale, bf.rete
--from
--C6MartPeriodico.patrimonio_bf bf, C6MartPeriodico.ASSET_PERC p, C6MartPeriodico.ASSETCLASs a
--where p.COD_PRODOTTO = bf.COD_PRODOTTO
--and p.ID_ASSETCLASS=a.ID_ASSETCLASS
--group by bf.rete, bf.cod_fiscale
--having count(distinct a.ordinamento)=1
select distinct cod_fiscale, rete
from
C6MartPeriodico.patrimonio_bf bf, C6MartPeriodico.ASSET_PERC p, C6MartPeriodico.ASSETCLASS a
where p.COD_PRODOTTO = bf.COD_PRODOTTO
and p.ID_ASSETCLASS=a.ID_ASSETCLASS
and a.ordinamento like '%000'
group by bf.rete, bf.cod_fiscale
having count(distinct a.ordinamento)=1
) bf
--2 TUTTE LE MACRO ASSET
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select  
bf.rete, 
bf.cod_fiscale,
'TUTTE LE MACRO ASSET                             ' AS DESCRIZIONE,
0 AS c1,
1 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
from 
(
--Effettuato intervento correttivo in quanto venivano estratti i clienti che presentavano 7 "ASSET CLASS" anzichè 7 "MACRO ASSET CLASS"
--select distinct cod_fiscale, rete
--from
--C6MartPeriodico.patrimonio_bf bf, C6MartPeriodico.ASSET_PERC p, C6MartPeriodico.ASSETCLASs a
--where p.COD_PRODOTTO = bf.COD_PRODOTTO
--and p.ID_ASSETCLASS=a.ID_ASSETCLASS
--group by bf.rete, bf.cod_fiscale
--having count(distinct a.ordinamento)=7
select distinct cod_fiscale, rete
from
C6MartPeriodico.patrimonio_bf bf, C6MartPeriodico.ASSET_PERC p, C6MartPeriodico.ASSETCLASS a
where p.COD_PRODOTTO = bf.COD_PRODOTTO
and p.ID_ASSETCLASS=a.ID_ASSETCLASS
and a.ordinamento like '%000'
group by bf.rete, bf.cod_fiscale
having count(distinct a.ordinamento)=7
) bf
--3 TUTTE LE AREE SONO MONITORATE
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis as cod_fiscale,
'TUTTE LE AREE SONO MONITORATE                    ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
1 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end as codfis, count(distinct codarea) c
from C6StagingPeriodico.TANAB
where dtfine='99991231'
group by rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end
having count(distinct codarea)=4
)bf
--4 SENZA PATRIMONIO TERZI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'SENZA PATRIMONIO TERZI                           ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
1 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.PATRIMONIO_bf a
where a.cod_fiscale + a.rete not in (select distinct cod_fiscale + rete from C6MartPeriodico.PATRIMONIO_TERZI)
) bf
--5 RISCHIO CREDITO NA
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'RISCHIO CREDITO N.A.                             ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
1 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.RISCHIO_AGGREGATO a, C6MartPeriodico.CODIFICA_RISCHIO cod, C6MartPeriodico.MIFID mifid
where mifid.rete=a.rete and mifid.cod_fiscale=a.cod_fiscale
and cod.profilo=mifid.profilo_ass
and a.creditrisk is null
and a.cod_aggreg='COMPLESSIVO'
) bf
-- 6 CLASSE DI RISCHIO CREDITO > 3 
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'RISCHIO CREDITO > 3                              ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
1 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.RISCHIO_AGGREGATO a, C6MartPeriodico.CODIFICA_RISCHIO cod, C6MartPeriodico.MIFID mifid
where mifid.rete=a.rete and mifid.cod_fiscale=a.cod_fiscale
and cod.profilo=mifid.profilo_ass
and a.creditrisk>3
and a.cod_aggreg='COMPLESSIVO'
) bf
-- 7 PRODOTTO SENZA RENDIMENTO
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PRODOTTO SENZA RENDIMENTO                        ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
1 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end as cod_fiscale
from C6StagingPeriodico.TSCABP
) bf
-- 8 piu trimestrali
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PIU TRIMESTRALI                                  ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
1 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select rete, codicefiscale AS COD_FISCALE, count(*)N
from C6StoricoPeriodico.GESTIONE_PDF_FTP
where tiporeport='M'
and dataGenerazione > @appoDataGenerazioneTrimPrecX2Da
group by rete, codicefiscale
having count(*)>1
) bf
-- 9 PATRIMONIO TERZI SU PIU' INTERMEDIARI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PATRIMONIO TERZI SU PIU'' INTERMEDIARI           ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
1 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select a.rete, a.cod_fiscale, count(distinct a.intermediario) M
from C6MartPeriodico.PATRIMONIO_TERZI a
group by a.rete, a.cod_fiscale
having count(distinct a.intermediario)>1
) bf
-- 10 PERSONE GIURIDICHE
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PERSONE GIURIDICHE                               ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
1 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.PATRIMONIO_BF 
where len(cod_fiscale)=11
) bf
-- 11 PB TERMINATI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PB TERMINATI                                     ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
1 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
--select distinct bf.rete, bf.cod_fiscale
--from C6MartPeriodico.PATRIMONIO_BF bf, C6Mart.CONTRATTOSEI c6,C6Stagingperiodico.ADS4WS_PROMOTORI pr
--where bf.rete=c6.rete and bf.cod_fiscale=c6.cod_fiscale
--and c6.cod_agente=pr.codice and pr.rete=c6.rete
--and stato= 'T'
select distinct bf.rete, bf.cod_fiscale
from C6MartPeriodico.PATRIMONIO_BF bf, C6MartPeriodico.CONTRATTOSEI c6,C6Stagingperiodico.RP_anagrafica_promotori pr
where bf.rete=c6.rete and bf.cod_fiscale=c6.cod_fiscale
and c6.cod_agente=pr.codage and pr.rete=c6.rete
and stato= 'T'
) bf
-- 12 PATRIMONIO TERZI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PATRIMONIO TERZI                                 ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
1 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
--select distinct a.rete, a.cod_fiscale
--from C6MartPeriodico.PATRIMONIO_TERZI a
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.PATRIMONIO_bf a
where a.cod_fiscale + a.rete in (select distinct cod_fiscale + rete from C6MartPeriodico.PATRIMONIO_TERZI)
) bf
-- 13 PATR NON RAPPR
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PATR NON RAPPR                                   ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
1 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select rete, cod_fiscale
from C6MartPeriodico.vPatrimonioNonRappresentabileAsset
) bf
-- 14 PATR IMMOBILIARE
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PATR IMMOB                                       ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
1 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select a.rete, a.cod_fiscale, sum(a.VALORE_STIMATO) M
from C6MartPeriodico.PATRIMONIO_IMMOBILIARE a 
group by a.rete, a.cod_fiscale
) bf
-- 15 PARTITE VIAGGIANTI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PARTITE VIAGGIANTI                               ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
1 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.PARTITE_VIAGGIANTI) bf
--16 NON MONITORATI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'NON MONITORATI                                   ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
1 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct bf.rete, bf.cod_fiscale
from C6MartPeriodico.patrimonio_bf bf
where bf.rete + bf.cod_fiscale not in ( select distinct rete + case when len(codfis) =4 then 'FF@'+codfis else codfis end from C6StagingPeriodico.TANAB where dtfine='99991231')
) bf
-- 17 NO PATR IMMOB
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'NON PATR IMMOB                                   ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
1 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from  C6MartPeriodico.PATRIMONIO_bf 
where cod_fiscale + rete not in (select distinct cod_fiscale + rete from C6MartPeriodico.patrimonio_immobiliare)
) bf
-- 18 MON SU UNA SOLA AREA
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select
bf.rete, 
bf.cod_fiscale,
'MON SU UNA SOLA AREA                             ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
1 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(select rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end AS COD_fISCALE, count(distinct codarea) H
from C6StagingPeriodico.TANAB
group by rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end
having count(distinct codarea)=1
) bf
-- 19 MON DA PIU DI 12 MESI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'MON DA PIU DI 12 MESI                            ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
1 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct  rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end AS COD_FISCALE
from C6StagingPeriodico.TANAB
--where ('20101201'-dtrif)/30>12
where dtrif < convert(varchar(8), DATEADD(year, -1, c6martperiodico.getTrimestre2(getdate(), 1)), 112)
) bf
-- 20 MON APPENA AVVIATO
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select
bf.rete, 
bf.cod_fiscale,
'MON APPENA AVVIATO                               ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
1 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, case when len(codfis) =4 then 'FF@'+codfis else codfis end AS COD_fISCALE
from  C6StagingPeriodico.TANAB
--where '20101201'-GETDATE()<1
where (dtrif > convert(varchar(8), DATEADD(day, -1, c6martperiodico.getTrimestre2(getdate(), 1)), 112))
) bf
-- 21 MANDATI FIDUCIARI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'MANDATI FIDUCIARI                                ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
1 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.PATRIMONIO_BF 
where cod_fiscale like 'FF@%'
) bf
-- 22 INADEGUATI VAR
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'INADEGUATI VAR                                   ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
1 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.RISCHIO_AGGREGATO a, C6MartPeriodico.CODIFICA_RISCHIO cod, C6MartPeriodico.MIFID mifid
where mifid.rete=a.rete and mifid.cod_fiscale=a.cod_fiscale
and cod.profilo=mifid.profilo_ass
and a.var_perc_ptf>cod.max_var
and a.cod_aggreg='COMPLESSIVO'
) bf
-- 23 INADEGUATI CRISK
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'INADEGUATI CRISK                                 ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
1 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.RISCHIO_AGGREGATO a, C6MartPeriodico.CODIFICA_CREDITRISK cod, C6MartPeriodico.MIFID mifid
where mifid.rete=a.rete and mifid.cod_fiscale=a.cod_fiscale
and cod.profilo=mifid.profilo_ass
and a.creditrisk>mifid.profilo_ass
and a.cod_aggreg='COMPLESSIVO'
) bf
-- 24 INADEGUATI COMPLESSITA
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'INADEGUATI COMPLESSITA                           ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
1 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale
from C6MartPeriodico.PATRIMONIO_BF a, C6MartPeriodico.MIFID mifid
where mifid.rete=a.rete and mifid.cod_fiscale=a.cod_fiscale
and mifid.experience<(select max(complex) from C6MartPeriodico.PATRIMONIO_BF where rete=mifid.rete and cod_fiscale=mifid.cod_fiscale)
) bf
-- 25 DIVERSIFICAZIONE < 30
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'DIVERSIFICAZIONE < 30                            ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
1 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select rete, cod_fiscale, beneficio
from C6MartPeriodico.RISCHIO_AGGREGATO
where cod_aggreg='COMPLESSIVO'
and beneficio<30
) bf
-- 26 CC NEG E POS 
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'CC NEG E POS                                     ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
1 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.ANAG_CC
where saldocontabile<0
intersect
select distinct rete, cod_fiscale
from C6MartPeriodico.ANAG_CC
where saldocontabile>0 
) bf
-- 27 CC NEG
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'CC NEGATIVO                                      ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
1 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select rete, cod_fiscale, sum(saldocontabile) M
from C6MartPeriodico.ANAG_CC
group by rete, cod_fiscale
having sum(saldocontabile)<0
) bf
where rete+cod_fiscale not in 
(
select distinct  rete+cod_fiscale
from C6MartPeriodico.ANAG_CC
where saldocontabile<0
intersect
select distinct  rete+cod_fiscale
from C6MartPeriodico.ANAG_CC
where saldocontabile>0
)
-- 28 REND NON RAPPR COME UNICA POSIZIONE
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'REND NON RAPPR COME UNICA POSIZIONE IN AREA MONIT' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
1 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
SELECT
DISTINCT T.RETE, T.COD_FISCALE FROM
(
SELECT
RETE, COD_FISCALE , ID_AREA, SUM(CASE WHEN REND_NON_RAPPR =1 THEN 0 ELSE 1 END) TOT 
FROM
C6MARTPERIODICO
.PATRIMONIO_BF B 
GROUP
BY 
RETE
, COD_FISCALE, ID_AREA 
)
T, 
(
SELECT
RETE, case when len(codfis) =4 then 'FF@'+codfis else codfis end as codfis, CASE WHEN CODAREA ='EXR' THEN 'EXT'ELSE CODAREA END AS CODAREA FROM
C6STAGINGPERIODICO
.TANAB 
WHERE
DTFINE = 99991231 
)
B 
WHERE
T
.RETE=B.RETE AND B.CODFIS=T.COD_FISCALE 
AND
ID_AREA=CODAREA 
AND
TOT=0
) bf
-- 29 PREVIDENZA 
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PREVIDENZA                                       ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
1 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.PATRIMONIO_BF
where id_area like '%Pre%'
) bf
--30 RESTART CONTRATTOSEI
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'RESTART CONTRATTOSEI                             ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
1 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, codicefiscale as cod_fiscale
from C6StoricoPeriodico.GESTIONE_PDF_FTP
where datagenerazione between @appoDataGenerazioneTrimPrecX2Da and @appoDataGenerazioneTrimPrecX2A
except 
select distinct p.rete,p.codicefiscale
     from C6StoricoPeriodico.GESTIONE_PDF_FTP p
     inner join c6martperiodico.patrimonio_bf b
     on p.rete=b.rete and p.codicefiscale =b.cod_fiscale 
        where datagenerazione between @appoDataGenerazioneTrimPrecDa and @appoDataGenerazioneTrimPrecA
--select distinct rete, codicefiscale as cod_fiscale
--from appoGESTIONE_PDF_FTP
--where datagenerazione between '20111001' and '20111031'
--and rete + codicefiscale not in (select distinct rete + codicefiscale
--        from appoGESTIONE_PDF_FTP
--        where datagenerazione between '20120101' and '20120131')
--and rete + codicefiscale in (select distinct rete + cod_fiscale
--        from c6martperiodico.patrimonio_bf)        
) bf
--31 INDIRIZZO CONTRATTO ESTERO
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'INDIRIZZO CONTRATTO ESTERO                       ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
1 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct bf.rete, bf.cod_fiscale
from C6MartPeriodico.PATRIMONIO_BF bf, C6MartPeriodico.CONTRATTOSEI c6
where bf.rete=c6.rete and bf.cod_fiscale=c6.cod_fiscale
and nazione<> 'italia'
) bf
--32 CC>10000
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'CC>10000                                         ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
1 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.ANAG_CC
group by rete, cod_fiscale
having sum(saldocontabile)>10000
) bf
--33 COPERTURA INFERIORE A 100
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'COPERTURA INFERIORE A 100                        ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
1 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
 from C6MartPeriodico.RISCHIO_AGGREGATO
where cod_aggreg='complessivo'
and copertura<100
) bf
--34 evoluzione controvalore > 30%
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'EVOLUZIONE CONTROVALORE > 30%                    ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
1 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct corrente.rete, corrente.cod_fiscale 
from
(select b.rete, b.cod_fiscale, sum(controvalore) ctv
from C6StagingPeriodico.RP_REP_S80_EVOL_STAMPA_C6 a
inner join C6StagingPeriodico.appo_contratti b
on a.chiaveClientePB=b.chiaveClientePB 
group by b.rete, b.cod_fiscale) scorsa,
(select rete, cod_fiscale, sum(ctv) ctv
from c6martperiodico.patrimonio_bf
group by rete, cod_fiscale) corrente
where scorsa.rete=corrente.rete and scorsa.cod_fiscale=corrente.cod_fiscale
and  (case when scorsa.ctv>corrente.ctv
      then (scorsa.ctv-abs(corrente.ctv))
      else (corrente.ctv-abs(scorsa.ctv))
      end)
      >
	 (case when scorsa.ctv>corrente.ctv
      then (0.30*corrente.ctv)
      else (0.30*scorsa.ctv)
      end)
--select distinct corrente.rete, corrente.cod_fiscale
--from
--(select rete, cod_fiscale, sum(controvalore) ctv
--from C6StagingPeriodico.S80_EVOL_STAMPA_C6
--group by rete, cod_fiscale) scorsa,
--(select rete, cod_fiscale, sum(ctv) ctv
--from c6martperiodico.patrimonio_bf
--group by rete, cod_fiscale) corrente
--where scorsa.rete=corrente.rete and scorsa.cod_fiscale=corrente.cod_fiscale
--and (0.33*scorsa.ctv>corrente.ctv or 0.33*scorsa.ctv<corrente.ctv)
) bf
--35  Clienti con investimenti in corso nel precedente report 
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'CLIENTI CON INV IN CORSO NEL PRECEDENTE REPORT   ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
1 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct b.rete, b.cod_fiscale
from C6StagingPeriodico.RP_REP_S80_EVOL_STAMPA_C6 a
inner join C6StagingPeriodico.appo_contratti b
on a.chiaveClientePB=b.chiaveClientePB 
where a.investimentiincorso>0
--select distinct rete, cod_fiscale
--from C6StagingPeriodico.S80_EVOL_STAMPA_C6
--where investimentiincorso>0
) bf
--36  Monitoraggio con cedole/dividendi 
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'MONITORAGGIO CON CEDOLE/DIVIDENDI                ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
1 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale
from C6MartPeriodico.MONITORAGGIO_ATTUALE
where CEDOLE_DIVIDENDI>0 
) bf
-- 37 PARTITE VIAGGIANTI DI DISINVESTIMENTO - 19/1/2016
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'PARTITE VIAGGIANTI DI DISINVESTIMENTO            ' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
1 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale from C6MartPeriodico.patrimonio_bf a
inner join [C6StampeCentralizzate].[C6MartPeriodico].[TB_Campione] b
on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale
where (partvia_disinv <> 0.000 and partvia_disinv is not null)
) bf
--CRITERIO ELIMINATO IN QUANTO DUPLICATO DEL CRITERIO 31
--38   Indirizzo Estero
--INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
--select 
--bf.rete, 
--bf.cod_fiscale,
--'Indirizzo Estero' AS DESCRIZIONE,
--0 AS c1,
--0 AS c2,
--0 AS c3,
--0 AS c4,
--0 AS c5,
--0 AS c6,
--0 AS c7,
--0 AS c8,
--0 AS c9,
--0 AS c10,
--0 AS c11,
--0 AS c12,
--0 AS c13,
--0 AS c14,
--0 AS c15,
--0 AS c16,
--0 AS c17,
--0 AS c18,
--0 AS c19,
--0 AS c20,
--0 AS c21,
--0 AS c22,
--0 AS c23,
--0 AS c24,
--0 AS c25,
--0 AS c26,
--0 AS c27,
--0 AS c28,
--0 AS c29,
--0 AS c30,
--0 AS c31,
--0 AS c32,
--0 AS c33,
--0 AS c34,
--0 AS c35,
--0 AS c36,
--0 AS c37,
--1 AS c38,
--0 AS c39,
--0 AS c40,
--0 AS c41,
--0 AS c42,
--0 AS c43,
--0 AS c44,
--0 AS c45,
--0 AS c46,
--0 AS c47,
--0 AS c48,
--0 AS c49,
--0 AS c50,
--0 AS c51,
--0 AS c52,
--0 AS c53,
--0 AS c54,
--0 AS c55,
--0 AS c56,
--0 AS c57,
--0 AS c58
--FROM
--(
--select distinct rete, cod_fiscale 
--from c6martperiodico.contrattosei
--where nazione <> 'italia'
--) bf
--INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE 
-- VALUES (
--'S', 
--'RSSRNZ49E14A479S',
--'Indirizzo Estero',
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--1,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0)
----38   Indirizzo Estero
--INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE 
-- VALUES (
--'F', 
--'CSCGPP34A03L050L',
--'Indirizzo Estero',
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--1,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0)
----38   Indirizzo Estero
--INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE 
-- VALUES (
--'F', 
--'LNERSR61L08C351V',
--'Indirizzo Estero',
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--1,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0)
--INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE 
-- VALUES (
--'F', 
--'BGLPRD65E10Z130S',
--'Indirizzo Estero',
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--1,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0)
--INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE 
-- VALUES (
--'F', 
--'LNZNLS71M48C814J',
--'Indirizzo Estero',
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--1,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0,
--0)
---------------------
--c39
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'Campionatura C39 apporti e prelievi' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
1 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale from c6martperiodico.monitoraggio_iniziale a
--where data_foto > '20120101'
where data_foto >= @appoDataInizioTrimestre
) bf
--c40
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'Campionatura C40 apporti e prelievi' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
1 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete,a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
left join 
(select distinct y.rete,y.cod_fiscale from C6StagingPeriodico.RP_REP_S80_EVOL_STAMPA_C6 x
inner join C6StagingPeriodico.appo_contratti y
on x.chiaveClientePB=y.chiaveClientePB) c 
on a.rete = c.rete
and a.cod_fiscale = c.cod_fiscale
where data_foto >= '20111001' and data_foto < @appoDataInizioTrimestre
and c.rete is not null
--select distinct a.* from c6martperiodico.monitoraggio_iniziale a
--left join C6StagingPeriodico.S80_EVOL_STAMPA_C6 c
--on a.rete = c.rete
--and a.cod_fiscale = c.cod_fiscale
--where data_foto >= '20111001' and data_foto < @appoDataInizioTrimestre
--and c.rete is not null
) bf
--41
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'Campionatura C41 apporti e prelievi' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
1 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete,a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
left join 
(select distinct y.rete,y.cod_fiscale from C6StagingPeriodico.RP_REP_S80_EVOL_STAMPA_C6 x
inner join C6StagingPeriodico.appo_contratti y
on x.chiaveClientePB=y.chiaveClientePB) c 
on a.rete = c.rete
and a.cod_fiscale = c.cod_fiscale
where data_foto >= '20111001' and data_foto < @appoDataInizioTrimestre
and c.rete is null
--select distinct a.* from c6martperiodico.monitoraggio_iniziale a
--left join C6StagingPeriodico.S80_EVOL_STAMPA_C6 c
--on a.rete = c.rete
--and a.cod_fiscale = c.cod_fiscale
--where data_foto >= '20111001' and data_foto < @appoDataInizioTrimestre
--and c.rete is null
) bf
--c42
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'Campionatura C42 apporti e prelievi' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
1 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete,a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
left join 
(select distinct y.rete,y.cod_fiscale from C6StagingPeriodico.RP_REP_S80_EVOL_STAMPA_C6 x
inner join C6StagingPeriodico.appo_contratti y
on x.chiaveClientePB=y.chiaveClientePB) c 
on a.rete = c.rete
and a.cod_fiscale = c.cod_fiscale
where data_foto < '20111001'
and c.rete is null
--select distinct a.rete, a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
--left join C6StagingPeriodico.S80_EVOL_STAMPA_C6 c
--on a.rete = c.rete
--and a.cod_fiscale = c.cod_fiscale
--where data_foto < '20111001'
--and c.rete is null
) bf
--c43
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'Campionatura C43 apporti e prelievi' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
1 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete,a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
left join 
(select distinct y.rete,y.cod_fiscale from C6StagingPeriodico.RP_REP_S80_EVOL_STAMPA_C6 x
inner join C6StagingPeriodico.appo_contratti y
on x.chiaveClientePB=y.chiaveClientePB) c 
on a.rete = c.rete
and a.cod_fiscale = c.cod_fiscale
where data_foto < '20111001'
and c.rete is not null
--select distinct a.rete, a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
--left join C6StagingPeriodico.S80_EVOL_STAMPA_C6 c
--on a.rete = c.rete
--and a.cod_fiscale = c.cod_fiscale
--where data_foto < '20111001'
--and c.rete is not null
) bf
--C44
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.cod_fiscale,
'F' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
1 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.cod_fiscale from c6martperiodico.monitoraggio_iniziale a
inner join c6stagingperiodico.st_mon 
on a.cod_fiscale = codfis
and a.rete = st_mon.rete
where data_foto > '20111001'
) bf
--C45
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'G' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
1 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on a.codfis = b.cod_fiscale
and a.rete = b.rete
where codprod in ('37', 'GP')
--and dttrim = '20111231' -- dttrim = dt fine trimestre precedente
and dttrim = @appoDataFineTrimPrec
and data_foto < '20111001'
) bf
--C46
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'H' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
1 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on a.codfis = b.cod_fiscale
and a.rete = b.rete
where codprod in ('37', 'GP')
--and dttrim = '20111231' -- dttrim = dt fine trimestre precedente
and dttrim = @appoDataFineTrimPrec
and data_foto > '20111001'
) bf
--C47
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'I' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
1 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on a.codfis = b.cod_fiscale
and a.rete = b.rete
where codprod in ('37', 'GP')
--Se fase di Simulazione:
--		Impostare la condizione DTTRIM = a dt fine mese precedente a dt fine trimestre
--and dttrim = '20131130'
--Se fase di Trimestrale:
--		Impostare la condizione DTTRIM = a dt fine trimestre in elaborazione
and dttrim = @appoDataFineTrimestre
and data_foto > '20111001'
) bf
--C48
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'J' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
1 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
/*
(select * from c6stagingperiodico.wseics where dttrim = '20111231') a
inner join
(select * from c6stagingperiodico.wseics where dttrim = '20120331') b
on a.rete = b.rete
and a.codfis = b.codfis
inner join c6martperiodico.monitoraggio_iniziale c
on b.codfis = c.cod_fiscale
and b.rete = c.rete
where
a.codprod not in ('37', 'GP')
and
b.codprod in ('37', 'GP')
and c.data_foto > '20111001'
*/
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on a.codfis = b.cod_fiscale
and a.rete = b.rete
where a.codprod in ('37', 'GP')
--Se fase di Simulazione:
--		Impostare la condizione DTTRIM = a dt fine mese precedente a dt fine trimestre
--and a.dttrim = '20131130'
--Se fase di Trimestrale:
--		Impostare la condizione DTTRIM = a dt fine trimestre in elaborazione
and   a.dttrim = @appoDataFineTrimestre
and   b.data_foto > '20111001'and   b.data_foto > '20111001'
except
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on    a.codfis = b.cod_fiscale
and   a.rete = b.rete
where a.codprod in ('37', 'GP')
and   a.dttrim = @appoDataFineTrimPrec
and   b.data_foto > '20111001'
) bf
--C49
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'K' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
1 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
--select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
--inner join c6stagingperiodico.wseics b
--on b.codfis = b.codfis
--and a.rete = b.rete
--where a.codprod not in ('37', 'GP')
----Se fase di Simulazione:
----		Impostare la condizione DTTRIM = a dt fine mese precedente a dt fine trimestre
----and a.dttrim = '20131130'
----Se fase di Trimestrale:
----		Impostare la condizione DTTRIM = a dt fine trimestre in elaborazione
--and a.dttrim = @appoDataFineTrimestre
--and b.codprod in ('37', 'GP')
----and b.dttrim = '20111231' -- b.dttrim = dt fine trimestre precedente
--and b.dttrim = @appoDataFineTrimPrec
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on a.codfis = b.cod_fiscale
and a.rete = b.rete
where a.codprod in ('37', 'GP')
and   a.dttrim = @appoDataFineTrimPrec
and   b.data_foto > '20111001'and   b.data_foto > '20111001'
except
select distinct a.rete, a.codfis from c6stagingperiodico.wseics a
inner join c6martperiodico.monitoraggio_iniziale b
on    a.codfis = b.cod_fiscale
and   a.rete = b.rete
where a.codprod in ('37', 'GP')
--Se fase di Simulazione:
--		Impostare la condizione DTTRIM = a dt fine mese precedente a dt fine trimestre
--and a.dttrim = '20131130'
--Se fase di Trimestrale:
and   a.dttrim = @appoDataFineTrimestre
and   b.data_foto > '20111001'
) bf
--nuovi-----------------------------------------------------------
--C50
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Popolazione Contratti OLD' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
1 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale as codfis from 
c6martperiodico.rp_vcontrattipergenerazionereport
where tip_contratto = 0
) bf
--C51
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Popolazione Contratti Affluent' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
1 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale as codfis from 
c6martperiodico.rp_vcontrattipergenerazionereport
where tip_contratto = 1
) bf
--C52
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Popolazione Contratti Private' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
1 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale as codfis from 
c6martperiodico.rp_vcontrattipergenerazionereport
where tip_contratto = 2
) bf
--C53
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Bonus' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
1 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct vc.rete, vc.cod_fiscale as codfis from 
c6martperiodico.rp_vcontrattipergenerazionereport vc
inner join
c6martperiodico.bonus bon
on vc.rete = bon.rete
and vc.cod_fiscale = bon.cod_fiscale
) bf
--C54
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Scadenze' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
1 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct vc.rete, vc.cod_fiscale as codfis from 
c6martperiodico.rp_vcontrattipergenerazionereport vc
inner join
c6martperiodico.scadenze sca
on vc.rete = sca.rete
and vc.cod_fiscale = sca.cod_fiscale
) bf
--C55
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Proventi' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
1 AS c55,
0 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct vc.rete, vc.cod_fiscale as codfis from 
c6martperiodico.rp_vcontrattipergenerazionereport vc
inner join
c6martperiodico.proventi pro
on vc.rete = pro.rete
and vc.cod_fiscale = pro.cod_fiscale
) bf
--C56
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'Monitoraggio avviato Trimestre precedente' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
1 AS c56,
0 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale as codfis from c6martperiodico.monitoraggio_iniziale a
--where data_foto > '20120101'
--where data_foto > C6MartPeriodico.getPreviousTrimestre(getdate(), 1)
where isnull(data_foto,getDate()) between @appoDataInizioTrimPrec and @appoDataFineTrimPrec
) bf
--C57
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
1 AS c57,
0 AS c58
FROM
(
select distinct rete, cod_fiscale as codfis from c6martperiodico.monitoraggio_iniziale a
--where isnull(data_foto,getDate()) >= c6martperiodico.getPreviousTrimestre(getDate(),0)
where isnull(data_foto,getDate()) between @appoDataInizioTrimestre and @appoDataFineTrimestre
) bf
--C58
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE
select 
bf.rete, 
bf.codfis,
'' AS DESCRIZIONE,
0 AS c1,
0 AS c2,
0 AS c3,
0 AS c4,
0 AS c5,
0 AS c6,
0 AS c7,
0 AS c8,
0 AS c9,
0 AS c10,
0 AS c11,
0 AS c12,
0 AS c13,
0 AS c14,
0 AS c15,
0 AS c16,
0 AS c17,
0 AS c18,
0 AS c19,
0 AS c20,
0 AS c21,
0 AS c22,
0 AS c23,
0 AS c24,
0 AS c25,
0 AS c26,
0 AS c27,
0 AS c28,
0 AS c29,
0 AS c30,
0 AS c31,
0 AS c32,
0 AS c33,
0 AS c34,
0 AS c35,
0 AS c36,
0 AS c37,
0 AS c38,
0 AS c39,
0 AS c40,
0 AS c41,
0 AS c42,
0 AS c43,
0 AS c44,
0 AS c45,
0 AS c46,
0 AS c47,
0 AS c48,
0 AS c49,
0 AS c50,
0 AS c51,
0 AS c52,
0 AS c53,
0 AS c54,
0 AS c55,
0 AS c56,
0 AS c57,
1 AS c58
FROM
(
--select distinct b.rete, b.cod_fiscale as codfis
--from c6martperiodico.monitoraggio_report a
--right join C6MartPeriodico.rp_vcontrattipergenerazionereport b
--on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale
--where a.rete is null
select distinct rete, cod_fiscale as codfis 
from c6martperiodico.monitoraggio_iniziale 
where isnull(data_foto,getDate()) between @appoDataInizioTrimPrec and @appoDataFineTrimPrec
except
select distinct rete, cod_fiscale as codfis
from c6martperiodico.monitoraggio_report
) bf
---------------------
--drop table C6MARTPERIODICO.SELEZIONECAMPIONE2
truncate table  C6MARTPERIODICO.SELEZIONECAMPIONE2
INSERT INTO C6MARTPERIODICO.SELEZIONECAMPIONE2
select
v.rete, 
v.cod_fiscale,
sum( C1 ) as C1,
sum( C2 ) as C2,
sum( C3 ) as C3,
sum( C4 ) as C4,
sum( C5 ) as C5,
sum( C6 ) as C6,
sum( C7 ) as C7,
sum( C8 ) as C8,
sum( C9 ) as C9,
sum( C10 ) as C10,
sum( C11) as C11,
sum( C12 ) as C12,
sum( C13 ) as C13,
sum( C14 ) as C14,
sum( C15 ) as C15,
sum( C16 ) as C16,
sum( C17 ) as C17,
sum( C18 ) as C18,
sum( C19 ) as C19,
sum( C20 ) as C20,
sum( C21 ) as C21,
sum( C22 ) as C22,
sum( C23) as C23,
sum( C24 ) as C24,
sum( C25 ) as C25,
sum( C26) as C26,
sum( C27 ) as C27,
sum( C28) as C28,
sum( C29 ) as C29,
sum( C30 ) as C30,
sum(C31) AS C31,
SUM(C32) AS C32,
SUM(C33) AS C33,
SUM(C34) AS C34,
SUM(C35) AS C35,
SUM(C36) AS C36,
SUM(C37) AS C37,
SUM(C38) AS C38,
SUM(C39) AS C39,
SUM(C40) AS C40,
SUM(C41) AS C41,
SUM(C42) AS C42,
SUM(C43) AS C43,
SUM(C44) AS C44,
SUM(C45) AS C45,
SUM(C46) AS C46,
SUM(C47) AS C47,
SUM(C48) AS C48,
SUM(C49) AS C49,
SUM(C50) AS C50,
SUM(C51) AS C51,
SUM(C52) AS C52,
SUM(C53) AS C53,
SUM(C54) AS C54,
SUM(C55) AS C55,
SUM(C56) AS C56,
SUM(C57) AS C57,
SUM(C58) AS C58
--INTO C6MARTPERIODICO.SELEZIONECAMPIONE2
FROM
C6MARTPERIODICO.SELEZIONECAMPIONE sc join C6MartPeriodico.rp_vcontrattipergenerazionereport v  
on sc.rete=v.rete and sc.cod_fiscale=v.cod_fiscale
group by
v.rete,
v.cod_fiscale
END
--SELECT * FROM C6MARTPERIODICO.SELEZIONECAMPIONE2