我想要一個查詢,獲取按天分組的最大值和最小值之間的差異。我嘗試不起作用:MySQL獲取選定行的最大值和最小值之間的差異
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`);
每個sub_meter有可能是> 3000的值,但只能通過< 10每天不同。我想要的差異,即結果< 10.通過上面的查詢,我得到的結果> 3000。
下面的查詢,只選擇一個米,並給出正確的結果,最大值(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)
但添加另一個米到條款,給出一個壞的結果,最大爲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)
我已經試過另一種嘗試:
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'
相關:http://stackoverflow.com/questions/4152781/sql-max-and-min-time-between-two-fields-changing ?rq = 1 http://stackoverflow.com/questions/8072231/mysql-how-to-select-minium-and-maximum-in-one-union-query?rq=1 – twodayslate
什麼是不適合你的查詢?錯誤消息,無效數據?你能提供一個答案的例子嗎? – doublesharp
我得到一個結果,但數字太高。我會詳細介紹上面的預期結果。 – user1745767