-- Schema: dbo
-- Stored Procedure: CREA_CAMPIONE_20160119














-- =============================================
-- Author:		<Author,,Name>
-- Create date: <20141008>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [dbo].[CREA_CAMPIONE_20160119]
	-- 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

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 (se