Answer the question
In order to leave comments, you need to log in
How to write such a query to the table?
There is a forms table
id count_forms
1 800
2 900
3 10
4 60
5 596
You need to make the following query:
Select id from the forms table where SUM(count_forms) is greater than 1000.
BUT the sum should be closest to 1000.
SELECT id, SUM(count_forms) as sum_count_forms FROM forms WHERE sum_count_forms>1000
Answer the question
In order to leave comments, you need to log in
Use HAVING.
More or less like this:
SELECT id, SUM(count_forms) as sum_count_forms
FROM forms
HAVING sum_count_forms > 1000
ORDER BY sum_count_forms ASC
LIMIT 1
Is the task set correctly? If we have one table, then the sum function will simply sum all the values \u200b\u200band give the final amount. And then it will be compared with 1000. Is there a grouping in the table?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question