[英]MySQL get difference between max and min values for select rows
I want a query that gets the difference between the max and min values grouped by day. 我想要一个查询,该查询获取按天分组的最大值和最小值之间的差异。 My attempt does not work:
我的尝试不起作用:
SELECT date(`date`),
(max(value) - min(value)) as value,
FROM `sub_meter_data`
where date(sub_meter_data.date) > '2012-10-01'
and sub_meterID in('58984','58985','58986','58987')
group by date(`date`);
Each sub_meter has a value that might be >3000, but only differs by < 10 per day. 每个sub_meter的值可能大于3000,但每天相差仅<10。 I want the difference, ie a result <10.
我想要差异,即结果<10。 With the query above I get results >3000.
通过上面的查询,我得到的结果> 3000。
This query below, just selects one meter, and give the correct results, the max (17531), the min (17523), and the difference (8). 下面的查询仅选择一个仪表,并给出正确的结果,即最大值(17531),最小值(17523)和差(8)。
SELECT date(sub_meter_data.date) as date,
max(value) as max_meter,
min(value) as min_meter,
max(value) - min(value) as diff,
FROM `sub_meter_data`
where date(sub_meter_data.date) > '2012-10-01'
and sub_meterID in('57636')
group by date(sub_meter_data.date)
But adding another meter into the in clause, give a bad result, the max is 17531, and the min is 3021, the diff is 14510. But I want the diff for each meter, then summed together. 但是在in子句中添加另一个仪表,会得出不好的结果,最大值是17531,最小值是3021,差异是14510。但是我想要每个仪表的差异,然后求和。
SELECT date(sub_meter_data.date) as date,
max(value) as max_meter,
min(value) as min_meter,
max(value) - min(value) as diff,
FROM `sub_meter_data`
where date(sub_meter_data.date) > '2012-10-01'
and sub_meterID in('57636', '57628')
group by date(sub_meter_data.date)
Another attempt I've tried is: 我尝试过的另一种尝试是:
SELECT date(sub_meter_data.date) as date,
sum(CASE WHEN sub_meterID = '57628' OR sub_meterID = '57636' THEN (max(value) - min(value)) ELSE 0 END) as value
FROM `sub_meter_data`
where date(sub_meter_data.date) > '2012-10-01'
The query is only grouping by day (date), but you want to group also by meter, so you need to add that into your group by
: 该查询仅按天(日期)分组,但您也想按米分组,因此需要按以下方式将其添加到
group by
:
select sub_meterID, date(`date`) as day, max(value) - min(value) as value
from `sub_meter_data`
where date(`date`) > '2012-10-01'
and sub_meterID in ('58984','58985','58986','58987')
group by sub_meterID, date(`date`);
Then if you want to sum the differences by day you can do: 然后,如果您想按天总结差异,则可以执行以下操作:
select day, sum(diff) as total_diff
from (
select sub_meterID, date(`date`) as day, max(value) - min(value) as diff
from `sub_meter_data`
where date(`date`) > '2012-10-01'
and sub_meterID in ('58984','58985','58986','58987')
group by sub_meterID, date(`date`)
) a
group by day
Or if you want to sum by meter: 或者,如果您想按米求和:
select sub_meterID, sum(diff) as total_diff
from (
select sub_meterID, date(`date`) as day, max(value) - min(value) as diff
from `sub_meter_data`
where date(`date`) > '2012-10-01'
and sub_meterID in ('58984','58985','58986','58987')
group by sub_meterID, date(`date`)
) a
group by sub_meterID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.