I have a table like this:
CREATE TABLE test(
id INT AUTO_INCREMENT PRIMARY KEY,
server_id VARCHAR(15) NOT NULL,
secret_number INT NOT NULL,
processing_status VARCHAR(15) DEFAULT 'UNPROCESSED',
ts TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
);
I want to get the SUM(secret_number)
of EXACTLY top 5 records ORDER BY id
which are in UNPROCESSED status. The below query works EXCEPT that it gives me the sum even when there are less than 5 matching records.
SELECT SUM(secret_number)
FROM (
SELECT secret_number
FROM test
WHERE processing_status = 'UNPROCESSED' AND server_id = 'R_SERVER'
ORDER BY id LIMIT 5
) t1;
How can I modify this (or write a new query) that either returns NULL
when there are less than 5 records matching the criteria? Or, if it can return me the COUNT
along with SUM
then in my Python code, I can check if returned count is less than 5, then ignore the results.
3
Answers
You can do this using
row_number()
:Here is one way to do it with window functions and aggregation:
The subquery enumerates the record by
id
; then the outer query ensures that the subquery returned at least 5 records (if not, an empty resultset is returned), and takes the sum of the top 5.If you do want a
null
value instead of an empty resultset when there are less than 5 records, then:Two solutions based on top of your original query without over complicating stuff:
Use
CASE
:Use
HAVING
: