电表统计sql优化

zw
tq 6 months ago
parent 3965906566
commit 57dd51bfae

@ -16,20 +16,44 @@ public class EmeterEnergyChangeService : BaseServices<EmeterEnergyChange>
public async Task<List<EmeterEnergyChange>> SevenDaysElectrical() public async Task<List<EmeterEnergyChange>> SevenDaysElectrical()
{ {
// 根据code分组,取最近7天数据,每天最大值-最小值=当日用电量,如果当天目前只有一条数据,取当天最大值-前一天最大值=当天耗电量
string sql = @" string sql = @"
WITH daily_max_min AS (
SELECT
`code`,
DATE(`upload_time`) AS `UploadTime`,
MAX(`value`) AS `MaxValue`,
MIN(`value`) AS `MinValue`
FROM
`emeter_energy_change`
WHERE
`upload_time` >= NOW() - INTERVAL 8 DAY
GROUP BY
`code`, DATE(`upload_time`)
), previous_day_max AS (
SELECT
`code`,
`UploadTime`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `UploadTime`) AS `PreviousMaxValue`
FROM
daily_max_min
)
SELECT SELECT
code, d.`code`,
date(upload_time) AS UploadTime, d.`UploadTime`,
MAX(value) - MIN(value) AS Value CASE
WHEN d.`MaxValue` = d.`MinValue` THEN COALESCE(d.`MaxValue` - p.`PreviousMaxValue`, d.`MaxValue`)
ELSE d.`MaxValue` - d.`MinValue`
END AS `Value`
FROM FROM
emeter_energy_change daily_max_min d
LEFT JOIN
previous_day_max p ON d.`code` = p.`code` AND d.`UploadTime` = p.`UploadTime`
WHERE WHERE
upload_time >= NOW() - INTERVAL 7 DAY d.`UploadTime` >= NOW() - INTERVAL 7 DAY
GROUP BY
code, date(upload_time)
ORDER BY ORDER BY
code, date(upload_time); d.`code`, d.`UploadTime`;
"; ";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies; return emeterEnergies;
@ -55,19 +79,41 @@ public class EmeterEnergyChangeService : BaseServices<EmeterEnergyChange>
public async Task<List<EmeterEnergyChange>> GetTodayHourlyElectricalData() public async Task<List<EmeterEnergyChange>> GetTodayHourlyElectricalData()
{ {
string sql = @" string sql = @"
SELECT WITH hourly_max_min AS (
code, SELECT
DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') AS Hour, `code`,
MAX(value) - MIN(value) AS Value DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00') AS `Hour`,
FROM MAX(`value`) AS `MaxValue`,
emeter_energy_change MIN(`value`) AS `MinValue`,
WHERE COUNT(*) AS `record_count`
DATE(upload_time) = CURDATE() FROM
GROUP BY `emeter_energy_change`
code, DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') WHERE
ORDER BY DATE(`upload_time`) = CURDATE()
code, Hour; GROUP BY
"; `code`, DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00')
), previous_hour_max AS (
SELECT
`code`,
`Hour`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue`
FROM
`hourly_max_min`
)
SELECT
h.`code`,
h.`Hour`,
CASE
WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue`
ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0)
END AS `Value`
FROM
`hourly_max_min` h
LEFT JOIN
`previous_hour_max` p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour`
ORDER BY
h.`code`, h.`Hour`;
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies; return emeterEnergies;
} }

@ -20,19 +20,43 @@ public class EmeterEnergyService : BaseServices<EmeterEnergy>
/// <returns></returns> /// <returns></returns>
public async Task<List<EmeterEnergy>> SevenDaysElectrical() public async Task<List<EmeterEnergy>> SevenDaysElectrical()
{ {
// 根据code分组,取最近7天数据,每天最大值-最小值=当日用电量,如果当天目前只有一条数据,取当天最大值-前一天最大值=当天耗电量
string sql = @" string sql = @"
WITH daily_max_min AS (
SELECT SELECT
code, `code`,
date(upload_time) AS UploadTime, DATE(`upload_time`) AS `UploadTime`,
MAX(value) - MIN(value) AS Value MAX(`value`) AS `MaxValue`,
MIN(`value`) AS `MinValue`
FROM FROM
emeter_energy `emeter_energy`
WHERE WHERE
upload_time >= NOW() - INTERVAL 7 DAY `upload_time` >= NOW() - INTERVAL 8 DAY
GROUP BY GROUP BY
code, date(upload_time) `code`, DATE(`upload_time`)
ORDER BY ), previous_day_max AS (
code, date(upload_time); SELECT
`code`,
`UploadTime`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `UploadTime`) AS `PreviousMaxValue`
FROM
daily_max_min
)
SELECT
d.`code`,
d.`UploadTime`,
CASE
WHEN d.`MaxValue` = d.`MinValue` THEN COALESCE(d.`MaxValue` - p.`PreviousMaxValue`, d.`MaxValue`)
ELSE d.`MaxValue` - d.`MinValue`
END AS `Value`
FROM
daily_max_min d
LEFT JOIN
previous_day_max p ON d.`code` = p.`code` AND d.`UploadTime` = p.`UploadTime`
WHERE
d.`UploadTime` >= NOW() - INTERVAL 7 DAY
ORDER BY
d.`code`, d.`UploadTime`;
"; ";
List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql); List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies; return emeterEnergies;
@ -67,19 +91,41 @@ public class EmeterEnergyService : BaseServices<EmeterEnergy>
public async Task<List<EmeterEnergy>> GetTodayHourlyElectricalData() public async Task<List<EmeterEnergy>> GetTodayHourlyElectricalData()
{ {
string sql = @" string sql = @"
SELECT WITH hourly_max_min AS (
code, SELECT
DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') AS Hour, `code`,
MAX(value) - MIN(value) AS Value DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00') AS `Hour`,
FROM MAX(`value`) AS `MaxValue`,
emeter_energy MIN(`value`) AS `MinValue`,
WHERE COUNT(*) AS `record_count`
DATE(upload_time) = CURDATE() FROM
GROUP BY `emeter_energy`
code, DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') WHERE
ORDER BY DATE(`upload_time`) = CURDATE()
code, Hour; GROUP BY
"; `code`, DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00')
), previous_hour_max AS (
SELECT
`code`,
`Hour`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue`
FROM
`hourly_max_min`
)
SELECT
h.`code`,
h.`Hour`,
CASE
WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue`
ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0)
END AS `Value`
FROM
`hourly_max_min` h
LEFT JOIN
`previous_hour_max` p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour`
ORDER BY
h.`code`, h.`Hour`;
";
List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql); List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies; return emeterEnergies;

Loading…
Cancel
Save