我使用 MySql 表来保存我通过 RS485 收集的传感器值。传感器汇总已进入电池的电量。放电时为正极,充电时为负极。
可悲的是,我意外重置了测量模块。所以我的电荷从一排到另一排下降了 161 A/h,新数据与那个零点有关。在这里,您可以看到我的表结构,其中包含费用、工作和时间的重置值。发生事件的行以蓝色突出显示。
我怎样才能以最好的方式纠正那个重置? 我想创建一个以 resetId(s) 作为索引的表(以防它再次发生)和列的偏移量。但我还没有找到一种方法将其整合到我的查询中。
我用数据做统计。最重要的是几小时或几天之间的收费差异,或者是收费最好的日子的 Top10。dayStats 的示例查询(事件发生前的一段时间)如下所示:
SELECT
id AS _cid,
curtime AS mynow,
Date_format(curtime, '%H%:00') AS date,
Round(Min(CURRENT) / 10, 2) AS 'min current',
Round(Avg(CURRENT) / 10, 2) AS 'avg current',
Round(Max(CURRENT) / 10, 2) AS 'max current',
Round(Min(power) / 1000, 2) AS 'min power',
Round(Avg(power) / 1000, 2) AS 'avg power',
Round(Max(power) / 1000, 2) AS 'max power',
charge / 1000 AS 'charge',
Round(( ( (SELECT charge
FROM MeasurementData.SolarPower
WHERE Max(_cid) = id) - charge ) / 1000 ), 2) AS chgDiff
FROM MeasurementData.SolarPower
WHERE Day(curtime) = Day('2018-05-05 12:00:00')
GROUP BY Hour(curtime)
ORDER BY mynow DESC
周统计数据几乎是相同的查询。但是这里重置显示了它的效果。
SELECT
id AS _cid,
curtime AS mynow,
Date_format(curtime, '%d%.%m.%Y') AS date,
Date_format(curtime, '%W') AS weekday,
Round(Min(CURRENT) / 10, 2) AS 'cur-min',
Round(Avg(CURRENT) / 10, 2) AS 'cur-avg',
Round(Max(CURRENT) / 10, 2) AS 'cur-max',
Round(Min(power) / 1000, 2) AS 'pow-min',
Round(Avg(power) / 1000, 2) AS 'pow-avg',
Round(Max(power) / 1000, 2) AS 'pow-max',
Round((SELECT charge
FROM MeasurementData.SolarPower
WHERE Max(_cid) = id) / 1000, 2) AS chg,
Round((((SELECT charge
FROM MeasurementData.SolarPower
WHERE Max(_cid) = id) - charge ) / 1000 ), 2) AS chgDiff
FROM MeasurementData.SolarPower
WHERE curtime >= Date_sub('2018-05-05 12:00:00', interval 6 day)
GROUP BY DATE
ORDER BY mynow DESC
输出到:THAT这里重置事件清晰可见。
顺便说一句......我不太擅长那个 SQL 的东西,而且显示的查询很慢。关于如何改进它们的任何建议?
提前谢谢你们。