-- Schema: dbo
-- Stored Procedure: Controlli_Mattina_PdC





-- =============================================
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [dbo].[Controlli_Mattina_PdC]
AS
BEGIN
drop table if exists #batch
create table #batch(
id int identity,
nomeBatch varchar(128)
)

insert into #batch values('CatalogoProdotti')
insert into #batch values('replicaMarketing')
insert into #batch values('AMPRO')
insert into #batch values('ClassiDiPrezzo')
insert into #batch values('ReplicaWasset')
insert into #batch values('TMD')
insert into #batch values('CalcolaVarNP')
insert into #batch values('FlussoRol')
insert into #batch values('ReplicaBasket')
insert into #batch values('ReplicaNAV')
insert into #batch values('W6CVNA')
insert into #batch values('MAC')
insert into #batch values('CalcolaVarNP_PtfCons')
insert into #batch values('Carica_PTFCONS_DatiFlussoMK')
insert into #batch values('ESEGUITI')
insert into #batch values('Flusso F')
insert into #batch values('FlussoPortafogli')
insert into #batch values('FlussoRol')
insert into #batch values('Perfezionamento')
insert into #batch values('ReplicaWNPTAB_APP')
insert into #batch values('RiallocazioneGuidataNA')
insert into #batch values('RDR')
insert into #batch values('replicaMonitoraggio')
insert into #batch values('AllineamentoMassivo_RiallocazioneGuidataNA')
insert into #batch values('ProdottiEsenti')
insert into #batch values('AllineamentoMassivo')
insert into #batch values('replicaW6LPST')
insert into #batch values('AllineamentoMassivo_RCM')
insert into #batch values('ReplicaClientiMovimentati')
insert into #batch values('CambioReteAmministrato')
insert into #batch values('ContrattoNuclei')
insert into #batch values('Movimentazione_Trimestrale')
insert into #batch values('CaricaListaIntermediari')
insert into #batch values('ContrattoSei')
insert into #batch values('ESTRAZIONE_SIMWEB')
insert into #batch values('ReplicaWassal')
insert into #batch values('ReplicaWKCCAT')
insert into #batch values('ReplicaWNPTAB')
insert into #batch values('ReplicaDichiarazioniPB')

--select * from #batch
drop table if exists #log_batch
create table #log_batch (
NomeBatch varchar(128),
Dtesecuzione datetime,
Stato varchar(200),
DtFineElaborazione datetime)

insert into #log_batch (NomeBatch,DtEsecuzione,Stato,DtFineElaborazione)
(SELECT NomeBatch,DtEsecuzione,Stato,DtFineElaborazione
FROM 	consuni.[ConsulenzaUnicaFL].[dbo].[logBatchGenerale] 
WHERE 	
CONVERT(DATETIME, CONVERT(DATE, DtEsecuzione))= CONVERT(DATETIME, CONVERT(DATE, GETDATE()))
--ORDER BY DTEsecuzione desc, nomebatch 

Union all

SELECT NomeBatch,DtEsecuzione,Stato,DtFineElaborazione
FROM 	consuni.[ConsulenzaUnicaFL].[dbo].[logBatchGenerale] 
WHERE 	
CONVERT(DATETIME, CONVERT(DATE, DtEsecuzione))= CONVERT(DATETIME, CONVERT(DATE, GETDATE()-1))
and CONVERT(DATETIME, CONVERT(DATETIME, DtEsecuzione))>CONVERT(DATETIME, CONVERT(DATETIME,dateadd(hh,0,GETDATE()-1)))
--ORDER BY DTEsecuzione desc, nomebatch 
)


--select * from #log_batch

select id,b.NomeBatch,Dtesecuzione,Stato,DtFineElaborazione 
into #def
from #batch b left join #log_batch l on b.nomeBatch=l.NomeBatch order by id 

SELECT DISTINCT
	b.id,b.NomeBatch,b.Dtesecuzione,b.Stato,b.DtFineElaborazione 
FROM #def b
WHERE b.Dtesecuzione = (SELECT MAX(Dtesecuzione) FROM #def c WHERE c.nomeBatch = b.nomeBatch ) or b.Dtesecuzione is null
ORDER BY b.id


END





