149 lines
5.6 KiB
MySQL
149 lines
5.6 KiB
MySQL
|
|
#namespace("Degree")
|
|||
|
|
-- 指定时间范围内,指定场站的充电度数,有峰平谷和总量
|
|||
|
|
#sql("getDegree")
|
|||
|
|
WITH station_list AS (
|
|||
|
|
SELECT id AS station_id, station_name
|
|||
|
|
FROM t_station
|
|||
|
|
WHERE 1=1
|
|||
|
|
#if(station_names)
|
|||
|
|
AND station_name IN (#para(station_names))
|
|||
|
|
#end
|
|||
|
|
),
|
|||
|
|
charge_data AS (
|
|||
|
|
-- 12月指定场站的充电信息,计算每条记录的充电量和时段
|
|||
|
|
SELECT
|
|||
|
|
teco.station_id,
|
|||
|
|
sl.station_name,
|
|||
|
|
teco.charge_end_degree - teco.charge_begin_degree AS charge_amount,
|
|||
|
|
HOUR(teco.charge_begin_time) AS hour_of_day
|
|||
|
|
FROM t_equipment_charge_order teco
|
|||
|
|
INNER JOIN station_list sl ON teco.station_id = sl.station_id
|
|||
|
|
WHERE 1=1
|
|||
|
|
#if(start_time)
|
|||
|
|
AND teco.charge_begin_time >= #para(start_time)
|
|||
|
|
#end
|
|||
|
|
#if(end_time)
|
|||
|
|
AND teco.charge_begin_time <= #para(end_time)
|
|||
|
|
#end
|
|||
|
|
)
|
|||
|
|
-- 统计各场站峰、平、谷时段的充电量(保留3位小数)
|
|||
|
|
SELECT
|
|||
|
|
station_id,
|
|||
|
|
station_name,
|
|||
|
|
-- 峰时段:8:00-11:00,18:00-21:00
|
|||
|
|
ROUND(SUM(CASE
|
|||
|
|
WHEN (hour_of_day >= 8 AND hour_of_day < 11)
|
|||
|
|
OR (hour_of_day >= 18 AND hour_of_day < 21)
|
|||
|
|
THEN charge_amount ELSE 0
|
|||
|
|
END), 3) AS peak_charging_amount,
|
|||
|
|
|
|||
|
|
-- 平时段:11:00-18:00,21:00-22:00
|
|||
|
|
ROUND(SUM(CASE
|
|||
|
|
WHEN (hour_of_day >= 11 AND hour_of_day < 18)
|
|||
|
|
OR (hour_of_day >= 21 AND hour_of_day < 22)
|
|||
|
|
THEN charge_amount ELSE 0
|
|||
|
|
END), 3) AS flat_charging_amount,
|
|||
|
|
|
|||
|
|
-- 谷时段:22:00-次日8:00(跨天处理)
|
|||
|
|
ROUND(SUM(CASE
|
|||
|
|
WHEN hour_of_day >= 22 OR hour_of_day < 8
|
|||
|
|
THEN charge_amount ELSE 0
|
|||
|
|
END), 3) AS valley_charging_amount,
|
|||
|
|
|
|||
|
|
-- 充电量总量
|
|||
|
|
ROUND(SUM(charge_amount), 3) AS total_charging_amount
|
|||
|
|
FROM charge_data
|
|||
|
|
GROUP BY station_id, station_name
|
|||
|
|
ORDER BY total_charging_amount DESC
|
|||
|
|
#if(!station_names)
|
|||
|
|
LIMIT 10
|
|||
|
|
#end
|
|||
|
|
;
|
|||
|
|
#end
|
|||
|
|
|
|||
|
|
#sql("getStationList")
|
|||
|
|
select id,station_name from t_station
|
|||
|
|
#end
|
|||
|
|
|
|||
|
|
-- 查询近3个月的充电趋势及变化
|
|||
|
|
#sql("getTrendChange")
|
|||
|
|
WITH monthly_data AS (
|
|||
|
|
SELECT
|
|||
|
|
ts.station_name,
|
|||
|
|
DATE_FORMAT(teco.charge_begin_time, '%Y-%m') as month_str,
|
|||
|
|
SUM(teco.charge_end_degree - teco.charge_begin_degree) as monthly_total
|
|||
|
|
FROM t_equipment_charge_order teco
|
|||
|
|
JOIN t_station ts ON teco.station_id = ts.id
|
|||
|
|
WHERE teco.charge_begin_time >= #para(start_time)
|
|||
|
|
AND teco.charge_begin_time <= #para(end_time)
|
|||
|
|
GROUP BY ts.station_name, month_str
|
|||
|
|
),
|
|||
|
|
pivoted_data AS (
|
|||
|
|
SELECT
|
|||
|
|
station_name,
|
|||
|
|
MAX(CASE WHEN month_str = #para(month1) THEN monthly_total ELSE 0 END) as m1_total,
|
|||
|
|
MAX(CASE WHEN month_str = #para(month2) THEN monthly_total ELSE 0 END) as m2_total,
|
|||
|
|
MAX(CASE WHEN month_str = #para(month3) THEN monthly_total ELSE 0 END) as m3_total
|
|||
|
|
FROM monthly_data
|
|||
|
|
GROUP BY station_name
|
|||
|
|
)
|
|||
|
|
SELECT
|
|||
|
|
station_name,
|
|||
|
|
ROUND(m1_total, 2) as month1_total,
|
|||
|
|
ROUND(m2_total, 2) as month2_total,
|
|||
|
|
ROUND(m3_total, 2) as month3_total,
|
|||
|
|
ROUND(m3_total - m1_total, 2) as net_change,
|
|||
|
|
ROUND(ABS(m3_total - m1_total), 2) as abs_change
|
|||
|
|
FROM pivoted_data
|
|||
|
|
ORDER BY abs_change DESC
|
|||
|
|
LIMIT 10
|
|||
|
|
#end
|
|||
|
|
|
|||
|
|
-- 获取指定时间内,按企业充电量由大到小TOP 10
|
|||
|
|
#sql("getCompanyTop10")
|
|||
|
|
WITH charge_data AS (
|
|||
|
|
SELECT
|
|||
|
|
tc.company_name,
|
|||
|
|
teco.charge_degree as charge_amount,
|
|||
|
|
HOUR(teco.charge_begin_time) AS hour_of_day
|
|||
|
|
FROM t_equipment_charge_order teco
|
|||
|
|
INNER JOIN t_user as tu ON teco.user_id = tu.id
|
|||
|
|
INNER JOIN t_company tc ON tc.id = tu.user_owner_id
|
|||
|
|
WHERE 1=1
|
|||
|
|
#if(start_time)
|
|||
|
|
AND teco.charge_begin_time >= #para(start_time)
|
|||
|
|
#end
|
|||
|
|
#if(end_time)
|
|||
|
|
AND teco.charge_begin_time <= #para(end_time)
|
|||
|
|
#end
|
|||
|
|
)
|
|||
|
|
SELECT
|
|||
|
|
company_name,
|
|||
|
|
-- 峰时段:8:00-11:00,18:00-21:00
|
|||
|
|
ROUND(SUM(CASE
|
|||
|
|
WHEN (hour_of_day >= 8 AND hour_of_day < 11)
|
|||
|
|
OR (hour_of_day >= 18 AND hour_of_day < 21)
|
|||
|
|
THEN charge_amount ELSE 0
|
|||
|
|
END), 2) AS peak_charging_amount,
|
|||
|
|
|
|||
|
|
-- 平时段:11:00-18:00,21:00-22:00
|
|||
|
|
ROUND(SUM(CASE
|
|||
|
|
WHEN (hour_of_day >= 11 AND hour_of_day < 18)
|
|||
|
|
OR (hour_of_day >= 21 AND hour_of_day < 22)
|
|||
|
|
THEN charge_amount ELSE 0
|
|||
|
|
END), 2) AS flat_charging_amount,
|
|||
|
|
|
|||
|
|
-- 谷时段:22:00-次日8:00(跨天处理)
|
|||
|
|
ROUND(SUM(CASE
|
|||
|
|
WHEN hour_of_day >= 22 OR hour_of_day < 8
|
|||
|
|
THEN charge_amount ELSE 0
|
|||
|
|
END), 2) AS valley_charging_amount,
|
|||
|
|
|
|||
|
|
-- 总量
|
|||
|
|
ROUND(SUM(charge_amount), 2) as total_charging_amount
|
|||
|
|
FROM charge_data
|
|||
|
|
GROUP BY company_name
|
|||
|
|
ORDER BY total_charging_amount DESC
|
|||
|
|
LIMIT 10
|
|||
|
|
#end
|
|||
|
|
#end
|