Bobkatt
Bobkatt

Reputation: 65

mysql distinct query using sum

I am trying to build a query that will give the the sum of totals for water moved by 3 pumps each day.

All values are the total for each pump

The problem is the end result should be (pump 1 + pump 2) - pump 3. The query I'm using is:

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;

Currently this just gives the total combined of all 3 pumps for each day

Upvotes: 2

Views: 72

Answers (1)

RichardTheKiwi
RichardTheKiwi

Reputation: 107716

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;

Upvotes: 2

Related Questions