-- ============================================= -- Author: -- Create date: <20141008> -- Description: -- ============================================= CREATE procedure [dbo].[CREA_CAMPIONE_20180921] -- Add the parameters for the stored procedure here AS BEGIN --------------------------------------------------------------- --SELEZIONE DEI PRIMI 200 CLIENTI ORDINATI PER NUMERO DI CASI-- --------------------------------------------------------------- select distinct campione.* into #appoCamp1 from ( select b.* FROM [C6MartPeriodico].[selezionecampione2] A inner join ( SELECT d.*, ([c1]+[c2]+[c3]+[c4]+[c5]+[c6]+[c7]+[c8]+[c9]+[c10]+[c11]+[c12]+[c13]+[c14]+[c15]+[c16]+[c17]+[c18]+[c19]+[c20] +[c21]+[c22]+[c23]+[c24]+[c25]+[c26]+[c27]+[c28]+[c29]+[c30]+[c31]+[c32]+[c33]+[c34]+[c35]+[c36]+[c37]+[c38]+[c39]+[c40] +[c41]+[c42]+[c43]+[c44]+[c45]+[c46]+[c47]+[c48]+[c49]+[c50]+[c51]+[c52]+[c53]+[c54]+[c55]+[c56]+[c57]+[c58] ) tot FROM [C6MartPeriodico].[selezionecampione2] d ) B on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale -- where c2 > 0 ) campione select distinct campione.* into #appoCamp2 from #appoCamp1 campione inner join ( select a.rete,a.cod_fiscale from consuni_periodico.ConsulenzaUnica.dbo.MonitoraggioStorico s inner join c6stagingperiodico.appo_contratti a on a.chiaveclientepb=s.chiaveclientePB and datediff(dd,dtAvvio,dtsalvataggio) =0 ) diffdate on campione.rete=diffdate.rete and campione.cod_fiscale=diffdate.cod_fiscale select d.rete,d.cod_fiscale,id_area,nome_progetto,max(data_cono) dt into #appoggio from C6MartPeriodico.CONO_PIANIFICAZIONE d where d.cod_fiscale in ( select d.cod_fiscale from #appoCamp2) -- dbo.campionetrimestrale - appocampionereport group by d.rete,cod_fiscale,id_area,nome_progetto select a.* into #appoCamp3 from #appoCamp2 a left join (select rete,cod_fiscale,dt from #appoggio where dt < '20140930') e on a.rete=e.rete and a.cod_fiscale=e.cod_fiscale where e.rete is null order by tot desc -- luca 19/1/2016 truncate table appocampionereport -- fine luca 19/1/2016 insert into appocampionereport -- mod 11/7/2018 - vogliamo meno report --select top 220 select top 100 -- fine mod 11/7/2018 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) order by tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c6 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c16 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c42 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c39 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c45 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c56 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c57 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select top 1 --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* from #appoCamp3 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and c58 > 0 order by b.tip_contratto desc, tot desc insert into appocampionereport select --DISASTERISCARE PER "INSERT SELECT" ed ASTERISCARE PER "SELECT" a.rete,a.cod_fiscale,'',b.tip_contratto --DISASTERISCARE PER "SELECT" ed ASTERISCARE PER "INSERT SELECT" --case when b.tip_contratto=0 then 'OLD' -- when b.tip_contratto=1 then 'AFFLUENT' -- when b.tip_contratto=2 then 'PRIVATE' --end as TipoContratto, --a.* -- modifica di Luca del 14/4/2016 - Per essere sicuri di estrarre tutti i cf in più richiesti pesco dal bacino che contiene -- tutto cioè #appoCamp1 -- luca from #appoCamp3 a from #appoCamp1 a inner join C6MARTPERIODICO.CONTRATTOSEI b on a.rete=b.rete and a.cod_fiscale=b.cod_fiscale where a.rete+a.cod_fiscale not in (select rete+cod_fiscale from c6martperiodico.test_quality) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from dbo.disallineati2) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_PROFILO_SCADUTO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MartPeriodico.CONTROLLO_TRIMESTRALE_RFA_ZERO) and a.rete+a.cod_fiscale not in (select rete+cod_fiscale from C6MARTPERIODICO.CONTROLLO_TRIMESTRALE where stato_report = 23) and a.rete+a.cod_fiscale in (select rete + codicefiscale from [dbo].[Incl_SpecCdT_Campione] ) drop table #appoCamp1 drop table #appoCamp2 drop table #appoCamp3 truncate table C6MartPeriodico.TB_Campione insert into C6MartPeriodico.TB_Campione select distinct rete, cod_fiscale, 1 --1 è il campo flag stampa che indica che dev esser stampato in fase di generazione del campione, 0 niente stampa from appocampionereport END