spool /mnpapp/dbcgo/log/script/SM_1737.log;

------ESEGUIRE CON UTENZA DWH su MNP

-- CREA VISTA MNP_DWH_REC_DATI_VIEW

CREATE OR REPLACE VIEW MNP_DWH_REC_DATI_VIEW
(ID_PROG, 
 COUNT, 
 CLIENTE,
 OT,
 DONATING, 
 "STANDARD/HOC",
 DATA_EVENTO,
 SETTIMANA_EVENTO,
 STATO_EVENTO,
 STATO_ATTUALE,
 SCARTATA_PIC,
 "CODICE_POP-PRP",
 OPERATORE,
 MESE_EVENTO)
AS
(SELECT ROWNUM, r.aa, r.bb, r.cc, r.dd, r.ee, r.ff, r.gg, r.hh, r.ii, r.ll, r.mm, r.nn, r.oo
FROM 
(
SELECT 
Sum(MNP_DWH_REC_PUSH.RICHIESTE)aa, 
MNP_DWH_REC_PUSH.TIPO_CLIENTE bb, 
MNP_DWH_REC_PUSH.NOME_OT cc, 
MNP_DWH_REC_PUSH.DONATING dd, 
MNP_DWH_REC_PUSH.TIPO_RICHIESTA ee, 
MNP_DWH_REC_PUSH.DATA_EVENTO ff, 
MNP_DWH_REC_PUSH.SETTIMANA_EVENTO gg, 
MNP_DWH_REC_PUSH.DESCR_STATO_EVENTO hh, 
MNP_DWH_REC_PUSH.DESCR_STATO_ATTUALE ii, 
MNP_DWH_REC_PUSH.PIC_SCARTO ll, 
MNP_DWH_REC_PUSH.CODICE_PRE_POST_PAGATO mm, 
MNP_DWH_REC_PUSH.OPERATORE_INTERNO nn,
TO_CHAR(MNP_DWH_REC_PUSH.DATA_EVENTO,'YY-MM') oo
FROM MNP_DWH_REC_PUSH
WHERE (MNP_DWH_REC_PUSH.DONATING<>'TIMT')
GROUP BY 
MNP_DWH_REC_PUSH.TIPO_CLIENTE, 
MNP_DWH_REC_PUSH.NOME_OT, 
MNP_DWH_REC_PUSH.TIPO_RICHIESTA, 
MNP_DWH_REC_PUSH.DONATING, 
MNP_DWH_REC_PUSH.DATA_EVENTO, 
MNP_DWH_REC_PUSH.SETTIMANA_EVENTO, 
MNP_DWH_REC_PUSH.DESCR_STATO_EVENTO, 
MNP_DWH_REC_PUSH.DESCR_STATO_ATTUALE, 
MNP_DWH_REC_PUSH.PIC_SCARTO, 
MNP_DWH_REC_PUSH.CODICE_PRE_POST_PAGATO, 
MNP_DWH_REC_PUSH.OPERATORE_INTERNO,
TO_CHAR(MNP_DWH_REC_PUSH.DATA_EVENTO,'YY-MM'))r);

--- CREA LA VISTA MNP_DWH_REC_DATI_RIFIUTI_VIEW

CREATE OR REPLACE VIEW MNP_DWH_REC_DATI_RIFIUTI_VIEW
(ID_PROG,
 COUNT,
 CLIENTE, 
 OT, 
 "STANDARD/HOC", 
 DONATING,
 DATA_EVENTO, 
 SETTIMANA_EVENTO,
 STATO_EVENTO, 
 CAUSALE,
 COD_RIFIUTO,
 "CODICE_POP-PRP", 
 STATO_ATTUALE,
 OPERATORE,
 CAUSALE_AGGREGATA,
 MESE_EVENTO)
AS
(
SELECT ROWNUM, r.aa, r.bb, r.cc, r.dd, r.ee, r.ff, r.gg, r.hh, r.ii, r.ll, r.mm, r.nn, r.oo, r.pp, r.ss
FROM 
(
SELECT 
Sum(MNP_DWH_REC_PUSH.RICHIESTE)aa, 
MNP_DWH_REC_PUSH.TIPO_CLIENTE bb, 
MNP_DWH_REC_PUSH.NOME_OT cc,
MNP_DWH_REC_PUSH.TIPO_RICHIESTA dd, 
MNP_DWH_REC_PUSH.DONATING ee, 
MNP_DWH_REC_PUSH.DATA_EVENTO ff, 
MNP_DWH_REC_PUSH.SETTIMANA_EVENTO gg, 
MNP_DWH_REC_PUSH.DESCR_STATO_EVENTO hh, 
MNP_DWH_REC_PUSH.CAUSALE_RIFIUTO ii,
MNP_DWH_REC_PUSH.COD_RIFIUTO ll,
MNP_DWH_REC_PUSH.CODICE_PRE_POST_PAGATO mm,
MNP_DWH_REC_PUSH.DESCR_STATO_ATTUALE nn, 
MNP_DWH_REC_PUSH.OPERATORE_INTERNO oo,
DECODE(COD_RIFIUTO,12,' ANNULLAMENTO PER RICHIESTE DA ALTRO RECIPIENT','ALTRO') pp,
TO_CHAR(MNP_DWH_REC_PUSH.DATA_EVENTO,'YY-MM') ss
FROM MNP_DWH_REC_PUSH
WHERE (MNP_DWH_REC_PUSH.STATO_ATTUALE IN (7,9))
GROUP BY 
MNP_DWH_REC_PUSH.TIPO_CLIENTE, 
MNP_DWH_REC_PUSH.NOME_OT, 
MNP_DWH_REC_PUSH.TIPO_RICHIESTA, 
MNP_DWH_REC_PUSH.DONATING, 
MNP_DWH_REC_PUSH.DATA_EVENTO, 
MNP_DWH_REC_PUSH.SETTIMANA_EVENTO, 
MNP_DWH_REC_PUSH.DESCR_STATO_EVENTO,
MNP_DWH_REC_PUSH.CAUSALE_RIFIUTO, 
MNP_DWH_REC_PUSH.COD_RIFIUTO, 
MNP_DWH_REC_PUSH.CODICE_PRE_POST_PAGATO,
MNP_DWH_REC_PUSH.DESCR_STATO_ATTUALE, 
MNP_DWH_REC_PUSH.OPERATORE_INTERNO,
DECODE(COD_RIFIUTO,12,' ANNULLAMENTO PER RICHIESTE DA ALTRO RECIPIENT','ALTRO'),
TO_CHAR(MNP_DWH_REC_PUSH.DATA_EVENTO,'YY-MM')
)r);

--- CREA LA VISTA MNP_DWH_DON_DATI_RIFIUTI_VIEW

CREATE OR REPLACE VIEW MNP_DWH_DON_DATI_RIFIUTI_VIEW
(ID_PROG,
 COUNT,
 CLIENTE,
 OT,
 "STANDARD/HOC",
 MARCAGGIO,
 RECIPIENT,
 DATA_EVENTO,
 SETTIMANA_EVENTO,
 MESE_EVENTO,
 STATO_EVENTO,
 COD_RIFIUTO,
 CAUSALE,
 CAUSALE_AGGREGATA,
 SOTTOMARCAGGIO,
 "CODICE_POP-PRP",
 OPERATORE)
AS
(SELECT ROWNUM, r.aa, r.bb, r.cc, r.dd, r.ee, r.ff, r.gg, r.hh, r.ii, r.ll, r.mm, r.nn, r.oo, r.pp, r.qq, r.ss
FROM
(
SELECT 
Sum(MNP_DWH_DON.RICHIESTE)aa, 
MNP_DWH_DON.TIPO_CLIENTE bb, 
MNP_DWH_DON.NOME_OT cc,
MNP_DWH_DON.TIPO_RICHIESTA dd, 
MNP_DWH_DON.MARCAGGIO ee,
MNP_DWH_DON.CODICE_OPERATORE_RECIPIENT ff,
MNP_DWH_DON.DATA_EVENTO gg,
MNP_DWH_DON.SETTIMANA_EVENTO hh,
TO_CHAR(MNP_DWH_DON.DATA_EVENTO,'YY-MM') ii,
MNP_DWH_DON.DESCR_STATO_EVENTO ll,
MNP_DWH_DON.COD_RIFIUTO mm,
MNP_DWH_DON.CAUSALE_RIFIUTO nn,
DECODE(COD_RIFIUTO,12,'ANNULLAMENTO PER RICHIESTE DA ALTRO RECIPIENT','ALTRO') oo,
MNP_DWH_DON.SOTTOMARCAGGIO pp,
MNP_DWH_DON.CODICE_PRE_POST_PAGATO qq,
MNP_DWH_DON.OPERATORE_INTERNO ss
FROM MNP_DWH_DON
WHERE MNP_DWH_DON.STATO_ATTUALE=10
GROUP BY 
MNP_DWH_DON.TIPO_CLIENTE, 
MNP_DWH_DON.NOME_OT,
MNP_DWH_DON.TIPO_RICHIESTA, 
MNP_DWH_DON.MARCAGGIO,
MNP_DWH_DON.CODICE_OPERATORE_RECIPIENT,
MNP_DWH_DON.DATA_EVENTO,
MNP_DWH_DON.SETTIMANA_EVENTO,
TO_CHAR(MNP_DWH_DON.DATA_EVENTO,'YY-MM'),
MNP_DWH_DON.DESCR_STATO_EVENTO,
MNP_DWH_DON.COD_RIFIUTO,
MNP_DWH_DON.CAUSALE_RIFIUTO,
DECODE(COD_RIFIUTO,12,'ANNULLAMENTO PER RICHIESTE DA ALTRO RECIPIENT','ALTRO'),
MNP_DWH_DON.SOTTOMARCAGGIO,
MNP_DWH_DON.CODICE_PRE_POST_PAGATO,
MNP_DWH_DON.OPERATORE_INTERNO)r);

--- CREA LA VISTA  MNP_DWH_DON_DATI_VIEW

CREATE OR REPLACE VIEW MNP_DWH_DON_DATI_VIEW
(ID_PROG,
 COUNT,
 CLIENTE,
 OT,
 "STANDARD/HOC",
 MARCAGGIO,
 RECIPIENT,
 DATA_EVENTO,
 SETTIMANA_EVENTO,
 MESE_EVENTO,
 STATO_EVENTO,
 STATO_ATTUALE,
 SOTTOMARCAGGIO,
 "CODICE_POP-PRP",
 OPERATORE)
AS
(SELECT ROWNUM, r.aa, r.bb, r.cc, r.dd, r.ee, r.ff, r.gg, r.hh, r.ii, r.ll, r.mm, r.nn, r.oo, r.pp
FROM
(
SELECT 
Sum(MNP_DWH_DON.RICHIESTE)aa, 
MNP_DWH_DON.TIPO_CLIENTE bb, 
MNP_DWH_DON.NOME_OT cc,
MNP_DWH_DON.TIPO_RICHIESTA dd, 
MNP_DWH_DON.MARCAGGIO ee,
MNP_DWH_DON.CODICE_OPERATORE_RECIPIENT ff,
MNP_DWH_DON.DATA_EVENTO gg,
MNP_DWH_DON.SETTIMANA_EVENTO hh,
TO_CHAR(MNP_DWH_DON.DATA_EVENTO,'YY-MM') ii,
MNP_DWH_DON.DESCR_STATO_EVENTO ll,
MNP_DWH_DON.DESCR_STATO_ATTUALE mm,
MNP_DWH_DON.SOTTOMARCAGGIO nn,
MNP_DWH_DON.CODICE_PRE_POST_PAGATO oo,
MNP_DWH_DON.OPERATORE_INTERNO pp
FROM MNP_DWH_DON
GROUP BY 
MNP_DWH_DON.TIPO_CLIENTE, 
MNP_DWH_DON.NOME_OT,
MNP_DWH_DON.TIPO_RICHIESTA, 
MNP_DWH_DON.MARCAGGIO,
MNP_DWH_DON.CODICE_OPERATORE_RECIPIENT,
MNP_DWH_DON.DATA_EVENTO,
MNP_DWH_DON.SETTIMANA_EVENTO,
TO_CHAR(MNP_DWH_DON.DATA_EVENTO,'YY-MM'),
MNP_DWH_DON.DESCR_STATO_EVENTO,
MNP_DWH_DON.DESCR_STATO_ATTUALE,
MNP_DWH_DON.SOTTOMARCAGGIO,
MNP_DWH_DON.CODICE_PRE_POST_PAGATO,
MNP_DWH_DON.OPERATORE_INTERNO)r);

----Delibera Donating
-- modificata per ORA-00972: identifier is too long

CREATE OR REPLACE VIEW mnp_delibera_don_view
(ID_PROG, 
OLO,
RIC_RICEVUTE,
RIC_PIC, 
DATA_EVENTO)
AS
(SELECT ROWNUM, r.aa, r.bb, r.cc, r.dd
FROM 
(
select 
desc_olo aa,
nvl(rric,0) bb,
nvl(rpic,0) cc,
nvl(data_Evento,FUN_GIORNI_MENO (trunc(sysdate),1)) dd
from
(select DATA_EVENTO,CODICE_OPERATORE_RECIPIENT,sum(RICHIESTE) as rric
from mnp_dwh_don
where DATA_EVENTO = FUN_GIORNI_MENO (trunc(sysdate),1) 
AND STATO_EVENTO = 1
AND TIPO_RICHIESTA ='Standard'
group by DATA_EVENTO,CODICE_OPERATORE_RECIPIENT) riric,
(select CODICE_OPERATORE_RECIPIENT,sum(RICHIESTE) as rpic
from mnp_dwh_don
where DATA_EVENTO = FUN_GIORNI_MENO (trunc(sysdate),1) 
AND STATO_EVENTO = 1
AND STATO_ATTUALE <> 4
AND TIPO_RICHIESTA ='Standard'
group by CODICE_OPERATORE_RECIPIENT) ripic,
(select desc_olo
from mnp.mnp_olo
where flag_terze_parti = 1) olo
where desc_olo=ripic.CODICE_OPERATORE_RECIPIENT (+)
and desc_olo=riric.CODICE_OPERATORE_RECIPIENT (+)) r);

-------------------------------------------------------------------------------

spool off;
exit
