I have a table in Postgres (custdata) that looks like this:
customerid | mID | count
1 3 13
1 2 17
1 2 3
1 5 14
1 4 7
And a query like this:
SELECT
c.customerid,
c.mID,
c.count,
SUM (c.count)
FROM custdata c
JOIN tableB b
ON b.customerid = c.customerid
WHERE c.mID <> 2
AND b.starttimestamp = ? and b.endtimestamp = ?
I want to get the sum of the values in the count column of the values whose mID does not equal 2. In this case, the correct sum would be 34. But in my case, the result is returning 2 different rows with the incorrect sum. How can I get the sum of the values in the count column where the mID is not 2? Any help would be appreciated.
GROUP BY. Also, your query is invalid without aGROUP BY. Please post the query you ran & clarify your expected results. (F.ex. should the sum be calculated percustomerids?)