Reputation: 15374
I am trying to count the sum of 2 columns from a subset of results, so far I have put this together
SELECT COUNT(*)
FROM table_name
WHERE column_1 + column_2 >= 3 IN (SELECT id
FROM table_name
where id = 16
AND (name = 'Richard')
ORDER BY column_3
DESC LIMIT 10);
What I am expecting as output is the number of cases where the sum is >=3
, so as an example 5
At the moment I get the error
ERROR: operator does not exist: integer >= boolean
I feel I am on the wrong path though. How can I construct this query please
Upvotes: 0
Views: 37
Reputation: 461
You are not giving a value for the IN statement, which returns a boolean stating if a value is within the results of your SELECT statement
The easiest change to your query for it to work would be:
SELECT COUNT(*) FROM table_name
WHERE column_1 + column_2 >= 3
AND id IN(
SELECT id FROM table_name
WHERE id = 16 AND (name = 'Richard')
ORDER BY column_3 DESC LIMIT 10
);
However, the correct approach in case that table_name is actually used twice would be:
SELECT COUNT(*) FROM table_name
WHERE id = 16 AND (name = 'Richard')
AND column_1 + column_2 >= 3
ORDER BY column_3 DESC LIMIT 10;
Upvotes: 1