I have created sum to calculate the positive and negative of a data table column. I am now trying to set a LIMIT on the query but I cant seem to get it working. Here is the query that I am wanting to LIMIT.
SELECT SUM(CASE WHEN label > 0 THEN label ELSE 0 END) POSITIVE_BALANCE,
SUM(CASE WHEN label < 0 THEN label ELSE 0 END) NEGATIVE_BALANCE
FROM units
CodePudding user response:
As @Jon Armstrong said, the LIMIT clause only limits the amount of results you get, not the amount of rows you use to get those results (unless you group). For your case, you can first select the rows you want and then sum them.
SELECT SUM(CASE WHEN label > 0 THEN label ELSE 0 END) POSITIVE_BALANCE,
SUM(CASE WHEN label < 0 THEN label ELSE 0 END) NEGATIVE_BALANCE
from (select label
FROM units
limit 30) temp