-
Notifications
You must be signed in to change notification settings - Fork 0
/
SP_EGELP_REPORT_GENESIS.sql
65 lines (63 loc) · 3.46 KB
/
SP_EGELP_REPORT_GENESIS.sql
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
CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_EGELP_REPORT_GENESIS`(PAR_METER_ID TEXT,PAR_IN_DATE_S TEXT,PAR_IN_DATE_ST TEXT)
BEGIN
IF PAR_METER_ID IS NULL OR PAR_METER_ID = "" THEN
select * ,
round(peak_demand * bill_kw,2) as total_demand ,
round((kwhr_final - kwhr_begin ) * bill_unit,2) as total_unit ,
round((kwhr_final - kwhr_begin ) * bill_ft,2) as total_ft
from
(select tb.hour_id ,
tb.meter_id ,
meter.meter_name,
max(kw)peak_demand ,
min(CASE WHEN kwhr_final >0 THEN kwhr_final END) kwhr_begin ,
max(kwhr_final)kwhr_final,
bill_unit,
bill_kw,
bill_service,
bill_ft,
bill_vat
from(
select concat(hour_id,'00')hour_id ,meter_id ,SPLIT_DECIMAL(eg_00,',',1 ) as kw ,SPLIT_DECIMAL(eg_00,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
union all
select concat(hour_id,'15')hour_id ,meter_id ,SPLIT_DECIMAL(eg_15,',',1 )askw , SPLIT_DECIMAL(eg_15,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
union all
select concat(hour_id,'30')hour_id ,meter_id ,SPLIT_DECIMAL(eg_30,',',1 ) as kw, SPLIT_DECIMAL(eg_30,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
union all
select concat(hour_id,'45')hour_id,meter_id ,SPLIT_DECIMAL(eg_45,',',1 ) as kw, SPLIT_DECIMAL(eg_45,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
)tb
inner join dbrtftyav2.eg_ms_meter meter on tb.meter_id = meter.meter_id
LEFT JOIN dbrtftyav2.egelp_ms_bill bill ON substr(tb.hour_id,1,6) = bill.bill_id
group by meter_id ) as listbill;
ELSE
select * ,
round(peak_demand * bill_kw,2) as total_demand ,
round((kwhr_final - kwhr_begin ) * bill_unit,2) as total_unit ,
round((kwhr_final - kwhr_begin ) * bill_ft,2) as total_ft
from (
select tb.hour_id ,
tb.meter_id ,
meter.meter_name,
max(kw)peak_demand ,
min(CASE WHEN kwhr_final >0 THEN kwhr_final END)kwhr_begin ,
max(kwhr_final)kwhr_final,
bill_unit,
bill_kw,
bill_service,
bill_ft,
bill_vat
from(
select concat(hour_id,'00')hour_id ,meter_id ,SPLIT_DECIMAL(eg_00,',',1 ) as kw ,SPLIT_DECIMAL(eg_00,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
union all
select concat(hour_id,'15')hour_id ,meter_id ,SPLIT_DECIMAL(eg_15,',',1 )askw , SPLIT_DECIMAL(eg_15,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
union all
select concat(hour_id,'30')hour_id ,meter_id ,SPLIT_DECIMAL(eg_30,',',1 ) as kw, SPLIT_DECIMAL(eg_30,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
union all
select concat(hour_id,'45')hour_id,meter_id ,SPLIT_DECIMAL(eg_45,',',1 ) as kw, SPLIT_DECIMAL(eg_45,',',7 )as kwhr_final from dbrtftyrv2.eg_tr_15minute where hour_id between PAR_IN_DATE_S and PAR_IN_DATE_ST
)tb
inner join dbrtftyav2.eg_ms_meter meter on tb.meter_id = meter.meter_id
LEFT JOIN dbrtftyav2.egelp_ms_bill bill ON substr(tb.hour_id,1,6) = bill.bill_id
WHERE tb.meter_id in (PAR_METER_ID)
group by meter_id ) as listbill;
END IF;
END