I am trying to do a count and a from on an aliased subquery, but can't figure out how to add the filtering portion:
SELECT webpages.*, (select count(*)
from webpage_opportunities
where webpage_opportunities.webpage_id = webpages.id) as test_count
FROM "webpages"
WHERE (webpages.clicks IS NOT NULL) AND "webpages"."website_id" = 144 AND test_count = 0
GROUP BY webpages.id ORDER BY "webpages"."clicks" DESC LIMIT 50 OFFSET 0
The test_count
column shows up in the query results when I remove the AND test_count = 0
but once I add it, the query fails. I also need to make sure the subquery connects to the webpages table, as stated in thje join above.