-- =============================================
-- Author:		<Author,,Name>
-- Create date: <20141008>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CREA_CAMPIONE_20170114]
	-- 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
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 multiramo codprod = 'RE' - simulazione 16/12/2016
	,'FBDNPLA59P08L219U'
	,'FBNCSVN65B46I138Y'
	,'FBNNBRC71R64G702J'
	,'FBRCGPP32P10G195K'
	,'FBRGLLL68P60C573Q'
	,'FBRSGLL39E62D959G'
	,'FBRTMRA27P42Z126N'
	,'FCDONRC40P02L219T'
	,'FCLBCLD66E04G999R'
	,'FCNCDNL55P45F704W'
	,'FCNCMCL39E57A468C'
	,'FCPNRNT37H25I046O'
	,'FCPNSFN67M26D815V'
	,'FCRLGCM42P08H211R'
	,'FCRNLRT39T20F205G'
	,'FCVSCRL38C52E346P'
	,'FCVZLCU61A24H620I'
	,'FCZZRNN42L46D912L'
	,'FDLLPLA50A10B455H'
	,'FDMCNNA38D44L826J'
	,'SMSCCMN66E59F839W' --ok
	--,'FNLSMLN67B44G917W'
	--,'FMSCLVI54M09E922C'
	--,'FFRNLSN45E27C556H'
	--,'FCSTGNN56M67C287M'
	--,'FDLGGNN74P10D612S'
	--,'FBSUDNL67D03G224W'
	--,'FDFNGLG39T31F952N'
	--,'FMLZGLD42E21L736D'
	--,'FPLISFN65R20G337L'
	--,'SGLLSLL56T48D969O'
	,'FBNNMCL59M66A470J' --ok
	--,'FBRDPLA57C05H501X'
	,'FRSLPTR59C25E151X' --ok
	--,'FRSSLVN48T27F592Y'
	--,'FCMNFNN62E21A485D'
	--,'FFNLMRA53D16G942V'
	,'FCRRMRS45S54I968G' --ok
	,'FDSSDNL87E10G337L' --ok
	--,'FPMPGRG32P19H018W'
	,'FPZZMNG57T43E922K'
	,'FRFNMRA40E70F205Y'
	,'FRGNRTL48C50F205X'
	,'FRSANMR53H54F205P'
	,'FRTNGZN42R46D934H'
	,'FRVTTZN54T30I690K'
	,'FSCIFNC31E01D969Y'
	,'FSGLRSN42D50C704M'
	,'FSGNLSN70D01L736O'
	,'FSRSSFN68A69E897D'
	,'FSRTGNN57C31E648W'
	,'FSTCRMR37P43A944U'
	,'FSTRVKY71B52Z211P'
	,'FTRLGPP47D06E232K'
	------------------------------------- inizio cf clienti professionali
	,'FFF@5844'
	,'FMGNGPP55B25G224T'
	,'FLNCFBA59D06H437D'
    ------------------------------------- inizio cf Eccessiva Adeguatezza
    --,'FPRTFST39D18I921O'
    ,'FBAILDI62S56A794Z'
	,'FSBRFNC43M21G999H'
	,'FCSLFPP40H14H224F'
	--,'FSTCGFR43L10L792C'
	,'FGSCCLG71M53A182H'
	,'FDLLLBA40C70C553E'
	,'SNREMBR54E68A944E'
	,'SRFFSFN68M66D284B'
	,'SZNTDNC58S16L219G'
	--,'SFRRFNC56H28L840B'
	,'SGMBRNE30R47D969Z'
	,'SPRDPLN53P45D969Q'
---- 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