-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL_report.py
1042 lines (1018 loc) · 59.7 KB
/
SQL_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 22 15:18:09 2017
@author: utente
Queries per il report commerciale
"""
from __future__ import division
from __future__ import unicode_literals
import cx_Oracle
import csv
from collections import OrderedDict
import sys
import time
import pandas as pd
import datetime
dns = cx_Oracle.makedsn('192.168.0.60', '1521', 'argon')
connection = cx_Oracle.connect('read_only', 'read_only', dns)
cursor = connection.cursor()
txt = open('C:/Users/utente/Documents/SQL/Argon_File_1.txt','r').read()
sql = '''
select extract (year from dp.d_periodo) anno_competenza,
d.ds_intestatario cliente,
d.cd_intestatario cod_cliente,
d.s_cf_int codice_fiscale,
d.s_p_iva_int partita_iva,
dp.s_pod_pdr pod,
d.d_emissione data_fattura,
dp.n_pot_disp potenza_disp,
extract (month from d.d_emissione) mese_emiss,
extract (month from dp.d_periodo) mese_comp,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F0' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f0,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F1' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f1,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F2' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f2,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F3' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f3,
nvl (cast (null as number),0) kwh_f4,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%FP' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_opi,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%FOP' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_ofp,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F0' then rb.n_qta else null end),0) perdite_f0,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F1' then rb.n_qta else null end),0) perdite_f1,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F2' then rb.n_qta else null end),0) perdite_f2,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F3' then rb.n_qta else null end),0) perdite_f3,
nvl (cast (null as number),0) perdite_f4,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%FP' then rb.n_qta else null end),0) perdite_opi,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%FOP' then rb.n_qta else null end),0) perdite_ofp,
from mnh_billing.t_documenti d
join mnh_billing.r_doc_periodi dp
on dp.id_documento = d.id_documento
JOIN mnh_common.t_agenti agenti
ON d.cd_Agente = agenti.cd_Agente
JOIN mnh_common.t_agenzie agenzie
ON agenzie.cd_agenzia = agenti.cd_agenzia
left join mnh_billing.v_aut_rg_billing rb
on rb.id_documento = dp.id_documento
and rb.id_fornitura = dp.id_fornitura
and rb.d_periodo = dp.d_periodo
and rb.fl_contabilizza = 'S'
left join mnh_billing.t_componenti c
on c.cd_componente = rb.cd_componente
where d.d_emissione between [inizio_emissione:D:01/01/2013] and [inizio_emissione:D:01/02/2013]
and d.id_stato < 1000 and dp.fl_virtuale = 'N'
group by d.id_documento,
dp.d_periodo,
dp.s_pod_pdr,
dp.d_iniz_forn,
dp.d_fine_forn,
d.s_p_iva_int,
d.cd_intestatario,
d.ds_intestatario,
order by cliente, pod;
'''
cursor.execute(sql)
sql = u''
with open('C:/Users/utente/Documents/SQL/Argon_File_1.txt','r') as inserts:
for statement in inserts:
sql += statement
# cursor.execute(statement)
######## WORKS
sql_Argonfile2 = """SELECT id_contratto,
cliente_codice,
cliente_ragione_sociale,
cliente_partita_iva,
cliente_codice_fiscale,
cliente_indirizzo,
cliente_cap,
cliente_comune,
cliente_prov,
cliente_gruppo,
banca,
cliente_consorzio,
fornitura_codice,
fornitura_indirizzo,
fornitura_cap,
fornitura_comune,
fornitura_prov,
fornitura_contratto_da,
fornitura_contratto_a,
fornitura_presa,
fornitura_pod,
fornitura_tipo_mis,
fornitura_potenza,
fornitura_tensione,
trasporto_opz_tariff,
trasporto_distributore,
fattura_indirizzo,
fattura_cap,
fattura_comune,
fattura_prov,
banca_cliente,
iban_cc,
sottoscrittore_rid,
sottoscrittore_codice_fiscale,
perc_iva,
art_esenzione_iva,
doc_aut_iva,
esenzione_imposta_erariale,
esenzione_imposta_provinciale,
frequenza_fatturaz,
scadenza_pagamento,
codice_rid,
codice_rid_cliente,
id_cliente,
id_sito,
d_valido_dal,
d_valido_al,
cd_tp_punto,
agenzia,
agente,
descrizione_prodotto,
prezzo_f0,
prezzo_f1_fp,
prezzo_f2_fop,
prezzo_f3,
consumo_contr_annuo,
consumo_contr_f1_fp,
consumo_contr_f2_fop,
consumo_contr_f3,
tipo_garanzia,
referente,
telefono_cliente,
fax_cliente,
mail_cliente,
cod_cli_asso,
versamento_diretto,
codice_ditta,
matricola_misuratore,
matricola_correttore,
coeff_c_misuratore,
cod_remi,
profilo_prelievo,
accise_gas,
cliente_gruppo_effettivo,
stato_domiciliazione,
tipo_di_rapporto,
entita_fatturabile,
cons_inizio_anno_solare,
cons_inizio_anno_termico,
SHIPPER,
consumo_distributore,
prezzo_pfor,
prezzo_cmem,
prezzo_pfisso,
prezzo_pfisso_axo,
codice_punto,
codice_classe_misuratore,
sconto_fedelta,
recesso_sconto,
calcolo_perequazione,
(select s_label from mnh_common.t_cst_prop_enum forn_enum where id_enum= stato_fornitura and cd_cst_prop='STATO_FORNITURA') as stato_fornitura,
note
FROM mnh_common.v_aut_simil_template
WHERE cd_tp_punto = 'EE' """
sql_Argonfile4 = """
select min (ag.cd_agenzia) cd_agenzia,
min (az.s_denominazione) ds_agenzia,
min (trim (ag.s_cognome || ' ' || ag.s_nome)) ds_agente,
min (cl.s_denominazione) ds_cliente,
fl.cd_punto,
min (fl.s_pod_pdr) s_pod,
min (ct.d_firma) d_firma,
min (fl.d_iniz_forn) over (partition by fl.cd_contratto) d_contr_dal,
case
when sum (case when fl.d_fine_forn is null then 1 else 0 end) over (partition by fl.cd_contratto) = 0 then
max (fl.d_fine_forn) over (partition by fl.cd_contratto)
else
null
end
d_contr_al,
fl.cd_prodotto cd_listino,
min (nvl (sc.n_cons_attualizz, sc.n_cons_contr)) n_energia_aa,
max (case when extract (month from fp.d_periodo) = 1 then dm.n_valore else null end) n_cons_gen,
max (case when extract (month from fp.d_periodo) = 2 then dm.n_valore else null end) n_cons_feb,
max (case when extract (month from fp.d_periodo) = 3 then dm.n_valore else null end) n_cons_mar,
max (case when extract (month from fp.d_periodo) = 4 then dm.n_valore else null end) n_cons_apr,
max (case when extract (month from fp.d_periodo) = 5 then dm.n_valore else null end) n_cons_mag,
max (case when extract (month from fp.d_periodo) = 6 then dm.n_valore else null end) n_cons_giu,
max (case when extract (month from fp.d_periodo) = 7 then dm.n_valore else null end) n_cons_lug,
max (case when extract (month from fp.d_periodo) = 8 then dm.n_valore else null end) n_cons_ago,
max (case when extract (month from fp.d_periodo) = 9 then dm.n_valore else null end) n_cons_set,
max (case when extract (month from fp.d_periodo) = 10 then dm.n_valore else null end) n_cons_ott,
max (case when extract (month from fp.d_periodo) = 11 then dm.n_valore else null end) n_cons_nov,
max (case when extract (month from fp.d_periodo) = 12 then dm.n_valore else null end) n_cons_dic,
max (case when extract (month from stm.d_periodo) = 1 then stm.n_ener_attiva else null end) n_stima_gen,
max (case when extract (month from stm.d_periodo) = 2 then stm.n_ener_attiva else null end) n_stima_feb,
max (case when extract (month from stm.d_periodo) = 3 then stm.n_ener_attiva else null end) n_stima_mar,
max (case when extract (month from stm.d_periodo) = 4 then stm.n_ener_attiva else null end) n_stima_apr,
max (case when extract (month from stm.d_periodo) = 5 then stm.n_ener_attiva else null end) n_stima_mag,
max (case when extract (month from stm.d_periodo) = 6 then stm.n_ener_attiva else null end) n_stima_giu,
max (case when extract (month from stm.d_periodo) = 7 then stm.n_ener_attiva else null end) n_stima_lug,
max (case when extract (month from stm.d_periodo) = 8 then stm.n_ener_attiva else null end) n_stima_ago,
max (case when extract (month from stm.d_periodo) = 9 then stm.n_ener_attiva else null end) n_stima_set,
max (case when extract (month from stm.d_periodo) = 10 then stm.n_ener_attiva else null end) n_stima_ott,
max (case when extract (month from stm.d_periodo) = 11 then stm.n_ener_attiva else null end) n_stima_nov,
max (case when extract (month from stm.d_periodo) = 12 then stm.n_ener_attiva else null end) n_stima_dic
from mnh_common.v_punti_fornitura fl
join mnh_common.t_prodotti pr
on pr.cd_prodotto = fl.cd_prodotto
left join mnh_common.t_agenti ag
on ag.cd_agente = fl.cd_agente
left join mnh_common.t_agenzie az
on ag.cd_agenzia = az.cd_agenzia
join mnh_common.t_clienti cl
on cl.cd_cliente = fl.cd_intestatario
left join mnh_common.t_contratti ct
on ct.cd_contratto = fl.cd_contratto
left join mnh_billing.t_stime_ee_aa sc
on sc.cd_punto = fl.cd_punto
left join mnh_billing.r_forniture_periodi fp
on fp.id_fornitura = fl.id_fornitura and fp.d_periodo between to_date('01/01/2017','dd/mm/yyyy') and to_date('31/12/2017','dd/mm/yyyy')
left join mnh_billing.t_doc_misure dm
on dm.id_documento = fp.id_doc_fatt
and dm.d_periodo = fp.d_periodo
and dm.cd_punto = fl.cd_punto
and dm.cd_tp_grandezza_fatt = 'E_MM_TOT'
and dm.cd_fascia = 'F0'
left join mnh_logistica.t_misure_ee stm
on stm.cd_tp_misura = 'CURVE_STIMA'
and stm.id_stato in (10, 20, 30)
and stm.cd_punto = fl.cd_punto
and stm.d_periodo = fp.d_periodo
where fl.cd_tp_punto = 'EE'
group by fl.cd_punto,
fl.d_iniz_forn,
fl.cd_contratto,
fl.d_fine_forn,
fl.cd_prodotto
"""
####################################################################################################
sql_Argonfile3 = """
SELECT riep.cd_agenzia,
riep.ds_agenzia,
dett.ds_agente,
dett.ds_intestatario_fatt as ds_cliente,
punti.cd_punto,
dett.s_pod_pdr,
contr.d_firma,
punti.d_valido_dal,
punti.d_valido_al,
punti.cd_prodotto as cd_listino,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '1' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_gen,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '2' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_feb,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '3' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_mar,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '4' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_apr,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '5' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_mag,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '6' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_giu,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '7' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_lug,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '8' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_ago,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '9' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_set,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '10' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_ott,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '11' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_nov,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '12' AND dett.cd_regola_provv like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS gett_dic,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '1' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_gen,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '2' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_feb,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '3' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_mar,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '4' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_apr,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '5' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_mag,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '6' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_giu,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '7' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_lug,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '8' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_ago,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '9' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_set,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '10' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_ott,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '11' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_nov,
SUM (CASE
WHEN EXTRACT (MONTH FROM riep.d_periodo) = '12' AND dett.cd_regola_provv not like '%GETTONE_%'
THEN dett.n_imp
ELSE 0
END) AS provv_dic
FROM mnh_crm.v_age_calcolo_provvigioni riep
JOIN mnh_crm.v_age_remunerazioni_dett dett
ON dett.id_remunerazione = riep.id_remunerazione
LEFT JOIN mnh_common.v_forniture_punti_lookup punti
ON punti.s_pod_pdr = dett.s_pod_pdr
LEFT JOIN mnh_common.t_contratti contr
ON contr.cd_contratto = punti.cd_contratto
WHERE TO_CHAR (riep.d_periodo, 'yyyy') = '2017'
and riep.id_fornitore = 1
GROUP BY riep.cd_agenzia,
riep.ds_agenzia,
dett.ds_agente,
dett.ds_intestatario_fatt,
punti.cd_punto,
dett.s_pod_pdr,
contr.d_firma,
punti.d_valido_dal,
punti.d_valido_al,
punti.cd_prodotto
"""
####################################################################################################
sql= """
with ta as (
-- T8 marg
select /*+ materialize */ * from (
SELECT sub.id_fornitura,
to_char(extract( month from (trunc (sub.d_periodo,'mm')))) d_periodo,
SUM(
CASE WHEN vend_ee_f1_prz IS NOT NULL THEN NVL(ROUND (vend_ee_f1_qta * mnh_report.get_prz_acq
(rdp.cd_prodotto,'ACQ_F1',to_date('01/'||extract( month from rdp.d_periodo) ||'/'||extract( year from rdp.d_periodo) ,'dd/mm/yyyy')),2),0) ELSE 0 END + -- IMP_ACQ_F1,
CASE WHEN vend_ee_f2_prz IS NOT NULL THEN NVL(ROUND
(vend_ee_f2_qta * mnh_report.get_prz_acq
(rdp.cd_prodotto,'ACQ_F2',to_date('01/'||extract( month from rdp.d_periodo) ||'/'||extract( year from rdp.d_periodo) ,'dd/mm/yyyy')),2),0) ELSE 0 END + -- IMP_ACQ_F2,
CASE WHEN vend_ee_f3_prz IS NOT NULL THEN NVL(ROUND (vend_ee_f3_qta * mnh_report.get_prz_acq
(rdp.cd_prodotto,'ACQ_F3',to_date('01/'||extract( month from rdp.d_periodo) ||'/'||extract( year from rdp.d_periodo) ,'dd/mm/yyyy')),2),0) ELSE 0 END + -- IMP_ACQ_F3,
CASE WHEN vend_ee_f0_prz IS NOT NULL THEN NVL(ROUND (vend_ee_f0_qta * mnh_report.get_prz_acq
(rdp.cd_prodotto,'ACQ_F0',to_date('01/'||extract( month from rdp.d_periodo) ||'/'||extract( year from rdp.d_periodo) ,'dd/mm/yyyy')),2),0) ELSE 0 END + -- IMP_ACQ_F0,
CASE WHEN vend_ee_fp_prz IS NOT NULL THEN NVL(ROUND (vend_ee_fp_qta * mnh_report.get_prz_acq
(rdp.cd_prodotto,'ACQ_F1',to_date('01/'||extract( month from rdp.d_periodo) ||'/'||extract( year from rdp.d_periodo) ,'dd/mm/yyyy')),2),0) ELSE 0 END + -- IMP_ACQ_FP,
CASE WHEN vend_ee_fop_prz IS NOT NULL THEN NVL(ROUND (vend_ee_fop_qta * mnh_report.get_prz_acq
(rdp.cd_prodotto,'ACQ_F2',to_date('01/'||extract( month from rdp.d_periodo) ||'/'||extract( year from rdp.d_periodo) ,'dd/mm/yyyy')),2),0) ELSE 0 END + -- IMP_ACQ_FOP
CASE WHEN pun_aritm_f1_prz IS NOT NULL THEN NVL(ROUND (pun_aritm_f1_qta * (pun_aritm_f1_prz - NVL((COALESCE(cp_punti_pun.n_valore, cp_prod_pun.n_valore)), 0)/1000 + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)),2), 0) ELSE 0 END +
--NVL(pun_aritm_f1_imp,0) + --,
CASE WHEN pun_f1_prz IS NOT NULL THEN NVL(ROUND (pun_f1_qta * (pun_f1_prz + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)), 2), 0) ELSE 0 END +
--NVL(pun_f1_imp,0) +
CASE WHEN gppb_f1_prz IS NOT NULL THEN NVL(ROUND (gppb_f1_qta * (gppb_f1_prz + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)), 2), 0) ELSE 0 END +
--NVL(gppb_f1_imp,0) + --,
CASE WHEN pun_aritm_f2_prz IS NOT NULL THEN NVL(ROUND (pun_aritm_f2_qta * (pun_aritm_f2_prz - NVL((COALESCE(cp_punti_pun.n_valore, cp_prod_pun.n_valore)), 0)/1000 + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)), 2), 0) ELSE 0 END +
--NVL(pun_aritm_f2_imp,0) + --,
CASE WHEN pun_f2_prz IS NOT NULL THEN NVL(ROUND (pun_f2_qta * (pun_f2_prz + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)), 2), 0) ELSE 0 END +
--NVL(pun_f2_imp,0) + --,
CASE WHEN gppb_f2_prz IS NOT NULL THEN NVL(ROUND (gppb_f2_qta * (gppb_f2_prz + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)), 2), 0) ELSE 0 END +
-- NVL(gppb_f2_imp,0) + --,
CASE WHEN pun_aritm_f3_prz IS NOT NULL THEN NVL(ROUND (pun_aritm_f3_qta * (pun_aritm_f3_prz - NVL((COALESCE(cp_punti_pun.n_valore, cp_prod_pun.n_valore)), 0)/1000 + NVL((COALESCE(cp_punti.n_valore, cp_prod.n_valore)), 0)), 2), 0) ELSE 0 END +
--NVL(pun_aritm_f3_imp,0) + --,
CASE WHEN pun_f3_prz IS NOT NULL THEN NVL(ROUND (pun_f3_qta * (pun_f3_prz + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0)), 2), 0) ELSE 0 END +
-- NVL(pun_f3_imp,0) + --,
CASE WHEN gppb_f3_prz IS NOT NULL THEN NVL(ROUND (gppb_f3_qta * gppb_f3_prz + NVL(COALESCE(cp_punti.n_valore, cp_prod.n_valore), 0), 2), 0) ELSE 0 END
-- NVL(gppb_f3_imp,0)
) as somma_mese
FROM mnh_billing.t_documenti doc
join mnh_billing.v_report_componenti_margini SUB on (doc.id_documento=sub.id_documento and doc.id_stato in (40,60)
)
join mnh_billing.r_doc_periodi rdp
on (sub.id_documento=rdp.id_documento and sub.d_periodo=rdp.d_periodo and sub.id_fornitura=rdp.id_fornitura
and TRUNC(rdp.d_periodo,'YYYY') = to_date('01/01/2017','dd/mm/yyyy')
)
LEFT JOIN mnh_common.V_SYS_CST_PROP_PUNTI cp_punti
on cp_punti.cd_punto = rdp.cd_punto
and cp_punti.CD_CST_PROP = 'ACQ_SPREAD_ENERGIA'
and rdp.d_periodo between cp_punti.D_VALIDO_DAL and nvl(cp_punti.D_VALIDO_AL, rdp.d_periodo)
LEFT JOIN mnh_common.V_SYS_CST_PROP_PRODOTTI cp_prod
ON cp_prod.cd_prodotto = rdp.cd_prodotto
and cp_prod.CD_CST_PROP = 'ACQ_SPREAD_ENERGIA'
and rdp.d_periodo between cp_prod.D_VALIDO_DAL and nvl(cp_prod.D_VALIDO_AL, rdp.d_periodo)
LEFT JOIN mnh_common.V_SYS_CST_PROP_PUNTI cp_punti_pun
on cp_punti_pun.cd_punto = rdp.cd_punto
and cp_punti_pun.CD_CST_PROP = 'SPREAD_PUN'
and rdp.d_periodo between cp_punti_pun.D_VALIDO_DAL and nvl(cp_punti_pun.D_VALIDO_AL, rdp.d_periodo)
LEFT JOIN mnh_common.V_SYS_CST_PROP_PRODOTTI cp_prod_pun
ON cp_prod_pun.cd_prodotto = rdp.cd_prodotto
and cp_prod_pun.CD_CST_PROP = 'SPREAD_PUN'
and rdp.d_periodo between cp_prod_pun.D_VALIDO_DAL and nvl(cp_prod_pun.D_VALIDO_AL, rdp.d_periodo)
group by sub.id_fornitura,trunc (sub.d_periodo,'mm')) tt
pivot (
max(somma_mese)
FOR d_periodo
in ('1' as gen, '2' as feb, '3' as mar, '4' as apr, '5' as mag, '6' as giu, '7' as lug,
'8' as ago, '9' as sett, '10' as ott, '11' as nov, '12' as dic) ))
-- Inizio query
select * from (
select az.s_denominazione AS agenzia,
ag.s_nome || ' ' || ag.s_cognome AS agente,
temp.s_pod AS pod,
MAX (pot.ragione_sociale)
KEEP (DENSE_RANK LAST ORDER BY doc.id_documento)
AS ragione_sociale,
MAX (pot.s_indirizzo_forn) KEEP (DENSE_RANK LAST ORDER BY doc.id_documento) AS indirizzo,
MAX (pot.ds_localita_forn) KEEP (DENSE_RANK LAST ORDER BY doc.id_documento) AS citta,
PRODOTTO_1,
PRODOTTO_1_DA,
PRODOTTO_2,
PRODOTTO_2_DA,
PRODOTTO_3,
PRODOTTO_3_DA,
PRODOTTO_4,
PRODOTTO_4_DA,
PRODOTTO_5,
PRODOTTO_5_DA,
PRODOTTO_6,
PRODOTTO_6_DA,
MIN (inizio_forniura_pod) AS inizio_fornitura_pod,
MAX (fine_fornitura_pod) AS fine_fornitura_pod,
CASE
WHEN SYSDATE BETWEEN MIN (pot.inizio_forniura_pod)
AND MAX (fine_fornitura_pod)
OR (SYSDATE >= MIN (pot.inizio_forniura_pod)
AND MAX (fine_fornitura_pod) IS NULL)
THEN
'IN FORNITURA'
WHEN SYSDATE < MIN (pot.inizio_forniura_pod)
THEN
'FUTURA FORNITURA'
ELSE
'USCITO'
END
AS stato_attuale_POD,
MAX (NVL (pot.n_potenza_imp, pot.n_potenza_disp)) AS potenza_kW,
SUM(CASE
WHEN rb.cd_componente LIKE '%PCV%' THEN rb.n_imp
ELSE NULL
END) pcv,
COUNT (DISTINCT TRUNC (rdp.d_periodo, 'mm')) AS num_mesi_fatturati,
sum (case when extract (month from rdp.d_periodo) = 1 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.gen)*(max(parv.n_valore / 100) + 1) margine_gen,
sum (case when extract (month from rdp.d_periodo) = 2 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.feb)*(max(parv.n_valore / 100) + 1) margine_feb,
sum (case when extract (month from rdp.d_periodo) = 3 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.mar)*(max(parv.n_valore / 100) + 1) margine_mar,
sum (case when extract (month from rdp.d_periodo) = 4 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.apr)*(max(parv.n_valore / 100) + 1) margine_apr,
sum (case when extract (month from rdp.d_periodo) = 5 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.mag)*(max(parv.n_valore / 100) + 1) margine_mag,
sum (case when extract (month from rdp.d_periodo) = 6 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.giu)*(max(parv.n_valore / 100) + 1) margine_giu,
sum (case when extract (month from rdp.d_periodo) = 7 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.lug)*(max(parv.n_valore / 100) + 1) margine_lug,
sum (case when extract (month from rdp.d_periodo) = 8 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.ago)*(max(parv.n_valore / 100) + 1) margine_ago,
sum (case when extract (month from rdp.d_periodo) = 9 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.sett)*(max(parv.n_valore / 100) + 1) margine_sett,
sum (case when extract (month from rdp.d_periodo) = 10 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.ott)*(max(parv.n_valore / 100) + 1) margine_ott,
sum (case when extract (month from rdp.d_periodo) = 11 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.nov)*(max(parv.n_valore / 100) + 1) margine_nov,
sum (case when extract (month from rdp.d_periodo) = 12 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') and c.cd_componente not in ('ONERE_GARANZIA_EE','ONERE_SPOT','POWER_ZOOM','ONERE_SPOT_EE') then rb.n_imp else null end) -
max(marg.dic)*(max(parv.n_valore / 100) + 1) margine_dic,
sum (case when extract (month from rdp.d_periodo) = 1 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_gen,
sum (case when extract (month from rdp.d_periodo) = 2 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_feb,
sum (case when extract (month from rdp.d_periodo) = 3 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_mar,
sum (case when extract (month from rdp.d_periodo) = 4 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_apr,
sum (case when extract (month from rdp.d_periodo) = 5 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_mag,
sum (case when extract (month from rdp.d_periodo) = 6 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_giu,
sum (case when extract (month from rdp.d_periodo) = 7 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_lug,
sum (case when extract (month from rdp.d_periodo) = 8 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_ago,
sum (case when extract (month from rdp.d_periodo) = 9 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_set,
sum (case when extract (month from rdp.d_periodo) = 10 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_ott,
sum (case when extract (month from rdp.d_periodo) = 11 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_nov,
sum (case when extract (month from rdp.d_periodo) = 12 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE','SCONTO_EE','VARIE_EE') then rb.n_imp else null end) fatt_energia_dic,
sum (case when extract (month from rdp.d_periodo) = 1 then rb.n_imp else null end) fatturato_tot_forn_gen,
sum (case when extract (month from rdp.d_periodo) = 2 then rb.n_imp else null end) fatturato_tot_forn_feb,
sum (case when extract (month from rdp.d_periodo) = 3 then rb.n_imp else null end) fatturato_tot_forn_mar,
sum (case when extract (month from rdp.d_periodo) = 4 then rb.n_imp else null end) fatturato_tot_forn_apr,
sum (case when extract (month from rdp.d_periodo) = 5 then rb.n_imp else null end) fatturato_tot_forn_mag,
sum (case when extract (month from rdp.d_periodo) = 6 then rb.n_imp else null end) fatturato_tot_forn_giu,
sum (case when extract (month from rdp.d_periodo) = 7 then rb.n_imp else null end) fatturato_tot_forn_lug,
sum (case when extract (month from rdp.d_periodo) = 8 then rb.n_imp else null end) fatturato_tot_forn_ago,
sum (case when extract (month from rdp.d_periodo) = 9 then rb.n_imp else null end) fatturato_tot_forn_set,
sum (case when extract (month from rdp.d_periodo) = 10 then rb.n_imp else null end) fatturato_tot_forn_ott,
sum (case when extract (month from rdp.d_periodo) = 11 then rb.n_imp else null end) fatturato_tot_forn_nov,
sum (case when extract (month from rdp.d_periodo) = 12 then rb.n_imp else null end) fatturato_tot_forn_dic,
sum (case when extract (month from rdp.d_periodo) = 1 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_gen,
sum (case when extract (month from rdp.d_periodo) = 2 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_feb,
sum (case when extract (month from rdp.d_periodo) = 3 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_mar,
sum (case when extract (month from rdp.d_periodo) = 4 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_apr,
sum (case when extract (month from rdp.d_periodo) = 5 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_mag,
sum (case when extract (month from rdp.d_periodo) = 6 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_giu,
sum (case when extract (month from rdp.d_periodo) = 7 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_lug,
sum (case when extract (month from rdp.d_periodo) = 8 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_ago,
sum (case when extract (month from rdp.d_periodo) = 9 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_set,
sum (case when extract (month from rdp.d_periodo) = 10 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_ott,
sum (case when extract (month from rdp.d_periodo) = 11 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_nov,
sum (case when extract (month from rdp.d_periodo) = 12 and c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE')and c.cd_componente not in('PREZZO_UNICO_CONG_F0','INDICE_PUN') then rb.n_qta else null end) consumo_dic,
MAX (gett_age_pod.gett_agente) AS gettone_agente,
MAX (fee_gen) fee_gen,
MAX (fee_feb) fee_feb,
MAX (fee_mar) fee_mar,
MAX (fee_apr) fee_apr,
MAX (fee_mag) fee_mag,
MAX (fee_giu) fee_giu,
MAX (fee_lug) fee_lug,
MAX (fee_ago) fee_ago,
MAX (fee_set) fee_set,
MAX (fee_ott) fee_ott,
MAX (fee_nov) fee_nov,
MAX (fee_dic) fee_dic,
MAX (CASE
WHEN PRODOTTO_6 is not null THEN (select MAX(N_VALORE * 1000) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_6 and cd_cst_prop = 'CORR_SQUIL_PEREQ')
WHEN PRODOTTO_5 is not null THEN (select MAX(N_VALORE * 1000) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_5 and cd_cst_prop = 'CORR_SQUIL_PEREQ')
WHEN PRODOTTO_4 is not null THEN (select MAX(N_VALORE * 1000) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_4 and cd_cst_prop = 'CORR_SQUIL_PEREQ')
WHEN PRODOTTO_3 is not null THEN (select MAX(N_VALORE * 1000) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_3 and cd_cst_prop = 'CORR_SQUIL_PEREQ')
WHEN PRODOTTO_2 is not null THEN (select MAX(N_VALORE * 1000) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_2 and cd_cst_prop = 'CORR_SQUIL_PEREQ')
WHEN PRODOTTO_1 is not null THEN (select MAX(N_VALORE * 1000) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_1 and cd_cst_prop = 'CORR_SQUIL_PEREQ')
END)
PRZ_PEREQ,
SUM(CASE
WHEN rb.cd_componente = 'CORR_SQUIL_PEREQ' THEN rb.n_imp
ELSE NULL
END)
importo_pereq,
MAX (CASE
WHEN PRODOTTO_6 is not null THEN (select MAX(N_VALORE) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_6 and cd_cst_prop = 'SCONTO_FEDELTA')
WHEN PRODOTTO_5 is not null THEN (select MAX(N_VALORE) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_5 and cd_cst_prop = 'SCONTO_FEDELTA')
WHEN PRODOTTO_4 is not null THEN (select MAX(N_VALORE) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_4 and cd_cst_prop = 'SCONTO_FEDELTA')
WHEN PRODOTTO_3 is not null THEN (select MAX(N_VALORE) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_3 and cd_cst_prop = 'SCONTO_FEDELTA')
WHEN PRODOTTO_2 is not null THEN (select MAX(N_VALORE) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_2 and cd_cst_prop = 'SCONTO_FEDELTA')
WHEN PRODOTTO_1 is not null THEN (select MAX(N_VALORE) from mnh_billing.r_cst_prop_prodotti
where cd_prodotto = PRODOTTO_1 and cd_cst_prop = 'SCONTO_FEDELTA')
END)
SCONTO_FEDELTA,
MAX(cp_recesso.n_valore) RECESSO_SCONTO
FROM
--- T1 temp
(
SELECT
s_pod,
id_fornitura,
MAX(DECODE (q.occorrenza, 1, q.cd_prodotto)) AS PRODOTTO_1,
MAX(DECODE (q.occorrenza, 1, q.da)) AS PRODOTTO_1_DA,
MAX(DECODE (q.occorrenza, 2, q.cd_prodotto)) AS PRODOTTO_2,
MAX(DECODE (q.occorrenza, 2, q.da)) AS PRODOTTO_2_DA,
MAX(DECODE (q.occorrenza, 3, q.cd_prodotto)) AS PRODOTTO_3,
MAX(DECODE (q.occorrenza, 3, q.da)) AS PRODOTTO_3_DA,
MAX(DECODE (q.occorrenza, 4, q.cd_prodotto)) AS PRODOTTO_4,
MAX(DECODE (q.occorrenza, 4, q.da)) AS PRODOTTO_4_DA,
MAX(DECODE (q.occorrenza, 5, q.cd_prodotto)) AS PRODOTTO_5,
MAX(DECODE (q.occorrenza, 5, q.da)) AS PRODOTTO_5_DA,
MAX(DECODE (q.occorrenza, 6, q.cd_prodotto)) AS PRODOTTO_6,
MAX(DECODE (q.occorrenza, 6, q.da)) AS PRODOTTO_6_DA
FROM
(SELECT y.occorrenza, x.s_pod, x.da, x.cd_prodotto, x.id_fornitura
FROM (SELECT a.*, row_number () OVER (PARTITION BY a.s_pod,a.id_fornitura ORDER BY da) AS riga
FROM (SELECT s_pod, vfl.cd_prodotto, MIN(vfl.d_valido_dal) AS da, id_fornitura
FROM mnh_common.v_forniture_lookup vfl, mnh_common.t_punti_el_st tpes
WHERE vfl.cd_punto = tpes.cd_punto
GROUP BY s_pod, vfl.cd_prodotto, vfl.id_fornitura) a
) x,
(SELECT LEVEL occorrenza FROM DUAL CONNECT BY LEVEL <= 6) y
WHERE y.occorrenza = x.riga(+)
) q
WHERE s_pod IS NOT NULL
GROUP BY s_pod,id_fornitura) temp
--T9
JOIN
( SELECT vfpl.s_pod_pdr,
vfpl.cd_agente,
vfpl.id_fornitura,--agg
MAX (vfpl.n_potenza_imp)
KEEP (DENSE_RANK LAST ORDER BY vfpl.d_valido_dal)
AS n_potenza_imp,
MAX (vfpl.n_potenza_disp)
KEEP (DENSE_RANK LAST ORDER BY vfpl.d_valido_dal)
AS n_potenza_disp,
MIN (vfpl.d_iniz_forn) AS inizio_forniura_pod,
MAX (vfpl.d_fine_forn)
KEEP (DENSE_RANK LAST ORDER BY vfpl.d_valido_dal)
AS fine_fornitura_pod,
max(tef.s_destinatario) KEEP (DENSE_RANK LAST ORDER BY vfpl.d_valido_dal) as ragione_sociale,
max(vfpl.s_indirizzo_forn) KEEP (DENSE_RANK LAST ORDER BY vfpl.d_valido_dal) as s_indirizzo_forn,
max(vfpl.ds_localita_forn) KEEP (DENSE_RANK LAST ORDER BY vfpl.d_valido_dal) as ds_localita_forn
FROM mnh_common.v_forniture_punti_lookup vfpl, mnh_common.t_entita_fatt tef
WHERE cd_tp_punto = 'EE'
and vfpl.cd_entita_fatt = tef.cd_entita_fatt
GROUP BY s_pod_pdr,
cd_agente,--aggiunto
id_fornitura--aggiunto
) pot
ON pot.s_pod_pdr = temp.s_pod and pot.id_fornitura=temp.id_fornitura
join ta marg on marg.id_fornitura= temp.id_fornitura
--T2 rdp
LEFT OUTER JOIN mnh_billing.r_doc_periodi rdp
ON rdp.s_pod_pdr = temp.s_pod --
and rdp.id_fornitura=temp.id_fornitura
AND TRUNC (rdp.d_periodo, 'yyyy') = to_date('01/01/2017','dd/mm/yyyy')
and rdp.id_documento in ( select id_documento from mnh_billing.t_documenti doc
where doc.id_stato IN (40, 60)
and cd_sezionale='FATT_EE')
--T3 doc
LEFT OUTER JOIN mnh_billing.t_documenti doc
on doc.id_documento = rdp.id_documento
--T4 rb
left join mnh_billing.t_doc_rg_billing rb
on rb.id_documento = rdp.id_documento
and rb.id_fornitura = rdp.id_fornitura
and rb.d_periodo = rdp.d_periodo
and rb.fl_contabilizza = 'S'
and rb.cd_componente not like 'IVA_%'
--T5 c
LEFT OUTER JOIN mnh_billing.t_componenti c
ON c.cd_componente = rb.cd_componente
--T6 par
LEFT OUTER join
(select par.cd_tp_tensione, parv.* from
mnh_billing.t_parametri par
join mnh_billing.t_param_valori parv
on parv.id_parametro = par.id_parametro
and par.cd_parametro = 'PERDITE_EE') parv
on rdp.cd_tp_tensione = parv.cd_tp_tensione
and rdp.d_periodo between parv.d_valido_dal and nvl ( parv.d_valido_al, rdp.d_periodo)
--T10 ag
LEFT OUTER JOIN mnh_common.t_agenti ag ON ag.cd_agente = pot.cd_agente
--T11 az
JOIN mnh_common.t_agenzie az ON ag.cd_agenzia = az.cd_agenzia
--T12 gett_age_pod
LEFT OUTER JOIN
( SELECT gett.cd_agente,
to_date('01/01/2017','dd/mm/yyyy')
AS perido_anno,
od.s_pod,
SUM (gett.n_imp) AS gett_agente
FROM mnh_crm.t_age_remunerazioni prov,
mnh_crm.t_age_remunerazioni_dett gett,
mnh_crm.t_ord_dett od
WHERE prov.id_stato in (20, 30)
AND gett.id_remunerazione = prov.id_remunerazione
AND gett.cd_regola_provv IN
('GETT_FIGLIA',
'GETTONE_CONF_EE',
'GETTONE_EE',
'GETTONE_EE_CONTR',
'GETTONE_13M_EE')
AND gett.id_ordine = od.id_ordine
AND gett.n_riga = od.n_riga
and TRUNC (gett.d_periodo, 'yyyy') = to_date('01/01/2017','dd/mm/yyyy')
GROUP BY gett.cd_agente,
od.s_pod) gett_age_pod
ON gett_age_pod.cd_agente = ag.cd_agente
AND gett_age_pod.perido_anno =
TRUNC (rdp.d_periodo, 'yyyy')
AND gett_age_pod.s_pod = rdp.s_pod_pdr
--T13
LEFT OUTER JOIN
(select * from mnh_crm.v_report_fee) fee_age
ON fee_age.cd_agente = doc.cd_agente
AND fee_age.id_documento = doc.id_documento
AND fee_age.id_fornitura = rdp.id_fornitura
AND fee_age.d_periodo = rdp.d_periodo
--
LEFT OUTER JOIN
mnh_common.V_SYS_CST_PROP_PUNTI cp_recesso
on cp_recesso.cd_punto = rdp.cd_punto
and cp_recesso.CD_CST_PROP = 'RECESSO_SCONTO'
and rdp.d_periodo between cp_recesso.D_VALIDO_DAL and nvl(cp_recesso.D_VALIDO_AL, rdp.d_periodo)
group by temp.s_pod,
ag.cd_agenzia,
ag.cd_agente,
az.s_denominazione,
ag.s_nome || ' ' || ag.s_cognome,
PRODOTTO_1,
PRODOTTO_1_DA,
PRODOTTO_2,
PRODOTTO_2_DA,
PRODOTTO_3,
PRODOTTO_3_DA,
PRODOTTO_4,
PRODOTTO_4_DA,
PRODOTTO_5,
PRODOTTO_5_DA,
PRODOTTO_6,
PRODOTTO_6_DA)"""
sql = "select * from MNH_REPORT.V__RPT__000000000217"
cursor.execute(sql)
####################################################################################################
sql = """
select extract (year from dp.d_periodo) anno_competenza,
d.ds_intestatario cliente,
d.cd_intestatario cod_cliente,
d.s_cf_int codice_fiscale,
d.s_p_iva_int partita_iva,
i.s_indirizzo indirizzo,
i.s_cap cap,
lf.ds_localita comune,
prf.s_sigla prov,
dp.s_pod_pdr pod,
pe.s_presa presa,
distr.ds_distributore distributore,
d.s_fattura_def num,
case when max (d.n_importo) >= 0 then 'FAT' else 'NCR' end tipo,
max (rb.n_imp_doc) fattura_totale_impo,
max (d.n_da_pagare) fattura_totale,
max (d.n_importo) - max (rb.n_imp_doc) iva_totale,
sum (case when rb.cd_componente like 'IVA_%' then 0 else rb.n_imp end) sito_imponibile,
round (sum (case when rb.cd_componente like 'IVA_%' then 0 else rb.n_imp end * rb.n_aliquota_iva / 100), 2)
sito_iva,
sum (case when rb.cd_componente like 'IVA_%' then 0 else rb.n_imp end)
+ round (sum (case when rb.cd_componente like 'IVA_%' then 0 else rb.n_imp end * rb.n_aliquota_iva / 100), 2)
sito_totale,
nvl(max(case when rb.cd_componente != 'BOLLO' then rb.s_codice_iva else null end),'') iva,
d.d_emissione data_fattura,
dp.n_pot_disp potenza_disp,
extract (month from d.d_emissione) mese_emiss,
extract (month from dp.d_periodo) mese_comp,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F0' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f0,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F1' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f1,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F2' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f2,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F3' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_f3,
nvl (cast (null as number),0) kwh_f4,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%FP' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_opi,
nvl(sum (case when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%FOP' and c.cd_componente not like ('SC%') then rb.n_qta else null end),0) kwh_ofp,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F0' then rb.n_qta else null end),0) perdite_f0,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F1' then rb.n_qta else null end),0) perdite_f1,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F2' then rb.n_qta else null end),0) perdite_f2,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%F3' then rb.n_qta else null end),0) perdite_f3,
nvl (cast (null as number),0) perdite_f4,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%FP' then rb.n_qta else null end),0) perdite_opi,
nvl(sum (case when c.cd_tp_riga = 'PERDITE_EE' and c.cd_componente like '%FOP' then rb.n_qta else null end),0) perdite_ofp,
nvl( max (
case
when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F0' and rb.fl_acconto = 'N' then rb.n_prz
else null
end),0)
prezzo_f0,
nvl(max (
case
when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F1' and rb.fl_acconto = 'N' then rb.n_prz
else null
end),0)
prezzo_f1,
nvl(max (
case
when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F2' and rb.fl_acconto = 'N' then rb.n_prz
else null
end),0)
prezzo_f2,
nvl(max (
case
when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%F3' and rb.fl_acconto = 'N' then rb.n_prz
else null
end),0)
prezzo_f3,
nvl(cast (null as number),0) prezzo_f4,
nvl(max (
case
when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%FP' and rb.fl_acconto = 'N' then rb.n_prz
else null
end),0)
prezzo_opi,
nvl(max (
case
when c.cd_tp_riga = 'PREZZO_EE' and c.cd_componente like '%OP' and rb.fl_acconto = 'N' then rb.n_prz
else null
end),0)
prezzo_ofp,
nvl(sum (case when c.cd_tp_riga in ('PREZZO_EE', 'PERDITE_EE') then rb.n_imp else null end),0) approvvigionamento_energia,
nvl(sum (case when c.cd_tp_riga in ('VARIE_EE','SCONTO_EE') and rb.n_prz < 0 then rb.n_imp else null end),0) sconti_magg,
nvl(sum (case when c.cd_tp_riga in ('VARIE_EE') and rb.n_prz >= 0 then rb.n_imp else null end),0) spred,
nvl(sum (case when rrg.cd_grp_righe in ('EE_MAGGIORAZ') then rb.n_imp else null end),0) oneri_e_maggiorazioni,
nvl(sum (case when c.cd_tp_riga in ('DISTRIB_EE', 'MISURA_EE', 'TRAS_EE') then rb.n_imp else null end),0) trasporto,
nvl (sum (case when c.cd_tp_riga = 'REATTIVA_EE' and rb.n_imp <> 0 then rb.n_qta else null end), 0) kvarh,
nvl(sum (case when c.cd_tp_riga = 'REATTIVA_EE' then rb.n_imp else null end),0) imp_kvarh,
nvl(sum (case when c.cd_componente = 'DISP_COPERT_COSTI' then rb.n_imp else null end),0) imp_od,
nvl(sum (case when c.cd_componente = 'DISP_FUNZ_TERNA' then rb.n_imp else null end),0) imp_dis,
nvl(sum (case when c.cd_componente = 'DISPACC_CD' then rb.n_imp else null end),0) imp_cd,
nvl(sum (case when c.cd_componente = 'DISPACC_INT' then rb.n_imp else null end),0) imp_int,
nvl(sum (case when c.cd_componente = 'DISPACCIAMENTO_QF' then rb.n_imp else null end),0) imp_aggmis,
nvl(sum (case when c.cd_componente = 'DISPACC_ART_25' then rb.n_imp else null end),0) art_25_bis,
nvl(sum (case when c.cd_componente = 'DISPACC_ART_44_BIS' then rb.n_imp else null end),0) art_44_bis,
nvl(sum (case when c.cd_componente = 'CORR_SQUIL_PEREQ' then rb.n_imp else null end),0) corrispettivo_squil_pereq,
nvl(sum (case when c.cd_tp_riga in ('DISPACCIAM_EE', 'DISP_BT_EE') then rb.n_imp else null end),0) tot_dispacciamento,
nvl(sum (
case
when rrg.cd_grp_righe in ('EE_IMPOSTE') and rb.cd_componente not like '%ADDIZ%' then rb.n_qta
else null
end),0)
consumi_erario,
nvl(max (
case
when rrg.cd_grp_righe in ('EE_IMPOSTE') and rb.cd_componente not like '%ADDIZ%' then rb.n_prz
else null
end),0)
corrispettivo_erario,
nvl(sum (
case
when rrg.cd_grp_righe in ('EE_IMPOSTE') and rb.cd_componente not like '%ADDIZ%' then rb.n_imp
else null
end),0)
imponibile_erario,
nvl(sum (case when rb.cd_componente = 'BOLLO' then rb.n_imp else null end),0) bolli,
nvl(sum (case when c.cd_componente = 'COFER' then rb.n_imp else null end),0) qual_serv,
nvl(sum (case when c.cd_componente = 'ADJ_FATT_PASSIVA' then rb.n_imp else null end),0) gest_conn,
dp.cd_tp_misuratore,
dp.CD_PRODOTTO,
max(cfr.n_valore) n_prz_cofer,
agenzie.s_denominazione AGENZIA,
nvl(sum (
case
when rrg.cd_grp_righe in ('FATT_PASS') and rb.cd_componente = 'ADJ_FATT_PASSIVA_247' then rb.n_imp
else null
end),0)
ADJ_FATT_PASSIVA_247,
nvl(sum (
case
when rrg.cd_grp_righe in ('FATT_PASS') and rb.cd_componente = 'CORR_CMOR' then rb.n_imp
else null
end),0)
CORR_CMOR,
nvl(max (case when mis_ff.n_ener_attiva_f1 is null then misure.n_ener_attiva else null end), 0) f0_misura,
nvl(max (mis_ff.n_ener_attiva_f1), 0) f1_misura,
nvl(max (mis_ff.n_ener_attiva_f2), 0) f2_misura,
nvl(max (mis_ff.n_ener_attiva_f3), 0) f3_misura,
nvl(max (case when mis_ff_cng.n_ener_attiva_f1 is null then mis_cng.n_ener_attiva else null end), 0) f0_misura_cng,
nvl(max (mis_ff_cng.n_ener_attiva_f1), 0) f1_misura_cng,
nvl(max (mis_ff_cng.n_ener_attiva_f2), 0) f2_misura_cng,
nvl(max (mis_ff_cng.n_ener_attiva_f3), 0) f3_misura_cng,
nvl(SEZ.S_CAUSALE_REG, '') protocollo,
dp.d_iniz_forn data_inizio,
dp.d_fine_forn data_fine
from mnh_billing.t_documenti d
join mnh_billing.r_doc_periodi dp
on dp.id_documento = d.id_documento
left JOIN mnh_billing.poker_sezionali sez
ON sez.id_fornitore = d.id_fornitore
and sez.cd_sezionale = d.cd_sezionale
and fl_segno = CASE
WHEN d.n_importo < 0
THEN '-'
ELSE '+'
END
join mnh_common.t_punti_el_st pe
on pe.cd_punto = dp.cd_punto and dp.d_periodo between pe.d_valido_dal and nvl (pe.d_valido_al, dp.d_periodo)
JOIN mnh_common.t_agenti agenti
ON d.cd_Agente = agenti.cd_Agente
JOIN mnh_common.t_agenzie agenzie
ON agenzie.cd_agenzia = agenti.cd_agenzia
left join mnh_common.t_indirizzi i
on i.id_indirizzo = pe.id_indirizzo_forn
left join mnh_common.t_geo_localita lf
on lf.id_localita = i.id_localita
left join mnh_common.t_geo_province prf
on prf.id_provincia = lf.id_provincia
left join mnh_common.t_distributori distr
on distr.cd_distributore = dp.cd_distributore
left join mnh_billing.v_aut_rg_billing rb
on rb.id_documento = dp.id_documento
and rb.id_fornitura = dp.id_fornitura
and rb.d_periodo = dp.d_periodo
and rb.fl_contabilizza = 'S'
left join mnh_billing.t_componenti c
on c.cd_componente = rb.cd_componente
left join mnh_billing.r_righe_gruppi rrg
on rrg.cd_tp_riga = c.cd_tp_riga
and rrg.cd_grp_righe in
('EE_TRASP',
'EE_DISPACC',
'EE_VARIE',
'EE_IMPOSTE',
'EE_MAGGIORAZ',
'EE_APPROV',
'FATT_PASS',
'INT_MORA',
'BOLLO')
left join mnh_common.v_sys_cst_prop_punti cfr
on cfr.cd_punto = dp.cd_punto
and cfr.cd_cst_prop = 'COFER'
and dp.d_periodo between cfr.d_valido_dal and nvl(cfr.d_valido_al, dp.d_periodo)
left join mnh_billing.t_misure_ee misure
on misure.id_misura = dp.id_misura
left join
(select *
from (select id_misura, cd_fascia, n_ener_attiva
from mnh_billing.t_mis_ee_ff)
pivot (sum (n_ener_attiva)
for cd_fascia in ('F1' as n_ener_attiva_f1,
'F2' as n_ener_attiva_f2,
'F3' as n_ener_attiva_f3))) mis_ff
on (mis_ff.id_misura = misure.id_misura)
left join mnh_billing.r_doc_periodi dpcng
on (dpcng.id_documento = dp.id_doc_cng
and dpcng.d_periodo = dp.d_periodo