我正在尝试构建一个查询,该查询将给出每天由3个泵移动的水的总和。
所有值均为每个泵的总和
问题是最终结果应该是(泵1 +泵2) - 泵3.我正在使用的查询是:
SELECT DISTINCT(date_time), SUM(rec_value)
FROM
rec_address_change
WHERE (address_id = 7 OR address_id = 11 OR address_id = 15)
GROUP BY date_time ORDER BY date_time;
目前,这只是每天所有3台泵的总和
答案 0 :(得分:2)
SELECT date_time, sum(
CASE WHEN address_id in (7,11) then rec_value
ELSE -rec_value END)
from rec_address_change
WHERE address_id IN (7,11,15)
GROUP BY date_time
ORDER BY date_time;