-- =============================================
-- Author:		<Author,,Name>
-- Create date: <20141008>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [dbo].[CREA_CAMPIONE_20161216]
	-- 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 rete,cod_fiscale
from consevo_periodico.ConsulenzaEvoluta.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
select top 175
--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
-- RACCOMANDATE
	('FTGLMRA30A04A409N' --ok
	,'FBZZGPP46C18A409M' --ok
	,'FCCRGPP54B27H307C' --ok
	,'FCRNCST86C56G062N' --ok
	,'FGHNDNL46L21G007R' --ok
	,'FGNSMNL58D47H037E' --ok
	,'FNAIGNE40H10B463Q' --ok
	,'FNCLLCU74M70L682S' --ok
	,'FPRNNTN32B06E003M' --ok
	,'FRLDLCN35B18A409N' --ok
	,'FRPILNI47M11L746H' --ok
	,'FSNEPMR60S24I976R' --ok
	,'FTBRFRZ68L20H037P' --ok
	,'FTBRGBR37R22G062P' --ok
	,'FTBRMRZ65H27H037W' --ok
	,'FBRLGCR25P13B885H' --ok
	,'FMRNGMR53P60F205P' --ok
-- inizio cf cambio rete -----------------------------------------------------------
	,'SSPSVCN66S01F839D', 
	'SSPGTRS50P66F839Q',
	'SDRSRSR49D70F839H',
	'SBNCFNC38C06E466K',
	'SCRDBRN61A57I829K',
	'SCRDLMR58M47G388T', 
	'SMRCLSU29R56F237Z',
	'SCRDGRG29R14L833R',
	'SGCCSLL89R62G388M',
	'SVNGSDR60A21A412J',
	'SPRERNI38H26G752A', 
-- fine cf cambio rete --------------------------------------------------------------
-- inizio cf delle banche in default ------------------------------------------------
	'SBNOSDR61T03F205W', 
	'SBNOSDR61T03F205W',
	'SCLCLNE41M47H434D',
	'SCPPBRN41H45C773S',
	'SMGNCRN32R54G547I',
	'SMZZCRL31P55G916D',
	'SSVLSFN64B08A390G',
	'FBNCMHL27S63I760X',
	'FCNTRME55S24B853O',
	'FCRMVGN49S53F791G',
	'FCRSGPT53M02D548K',
	'FGNAGLL33E53A393A',
	'FMGNLRI75A60H901Z',
	'FMGNLRI75A60H901Z',
	'FMNZMNL46C62A393V',
	'FMRNMNG53P46H294F',
	'FNTRMRZ67T17H223L',
	'FPLZLRA40T54L390R',
	'FSRVDDE31L44A191J',
	'FPRTCHR52L69H017M',
	'FGLNNLN39P46L498G',
	'FBRTGPP47M06A541R',
-- fine cf delle banche in default ------------------------------------------------
-- cf richiesto da Quaranta il 6/10/2016 per verifica del var: se var > 4,50 e indicatori inadeguati allora va fatto l’intervento per riabbassarlo altrimenti ok e quindi sbloccato
	'F94146680262' 
)
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