
SELECT DISTINCT
    organ_code AS 片区id,
    organ_name AS 片区名称
FROM dw.dws_engine_meter_dosage_m
where 1=1
${if(GETUSERDEPARTMENTS(2)="领悦集团总部","","and organ_name = '"+GETUSERDEPARTMENTS(2)+"'")}




-- 【能耗环比】

-- 折线图
select
 list_ym
, MeterType
,SUM(Dosage) /10000 Dosage -- 能耗
,SUM(Amount)  AS Amount-- 能耗金额
from dw.dws_engine_meter_dosage_m
where 1=1
and MeterType in ('水表','电表')
AND list_ym < date_format(current_date(),'%Y%m')
and left(list_ym,4) = ${left(p_ym,4)}
${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
-- ${IF(LEN(P_MeterType)>0," AND MeterType IN ('"+JOINARRAY(P_MeterType,"','")+"')", "")}
GROUP by
list_ym
, MeterType
ORDER BY
list_ym

-- 明细表






-- 【能耗同比】
SELECT
 MeterType
, list_ym
,SUM(Dosage) /10000 Dosage
,SUM(Amount)  AS Amount
FROM dw.dws_engine_meter_dosage_m
where 1=1
AND list_ym <> DATE_FORMAT(CURDATE(), '%Y%m')
AND list_ym >= '${FORMAT(MONTHDELTA(CONCATENATE(p_ym,"-01"),-60),"yyyyMM")}'
${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_ym)>0," AND right(list_ym,2) ='" +right(p_ym,2) + "'","")}
${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
and MeterType in ('水表','电表')
group by
MeterType
,list_ym
order by
list_ym




-- 【明细】

select
${if(len(p_area) == 0,"organ_name","comm_name")} as p_area  -- 片区/项目
, MeterType
,SUM(Dosage)  Dosage -- 能耗
,SUM(Amount)  AS Amount-- 能耗金额
from dw.dws_engine_meter_dosage_m
where 1=1
and MeterType in ('水表','电表')
and list_ym = ${replace(p_ym,'-','')}
${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
${IF(LEN(P_MeterType)>0," AND MeterType IN ('"+JOINARRAY(P_MeterType,"','")+"')", "")}
GROUP BY
${if(len(p_area) == 0,"organ_name","comm_name")}
 , MeterType


