Answer the question
In order to leave comments, you need to log in
How to make COUNT work with GROUP BY?
In general, the situation is this. It is necessary to count the number of lines in the response of a query like:
SELECT o.id AS cnt
FROM "order" AS o
LEFT JOIN product p ON (p.order_id = o.id)
GROUP BY o.id
HAVING sum(p.price) >= 10
Answer the question
In order to leave comments, you need to log in
It is necessary to count the number of lines in the response of a query like
SELECT COUNT(*) AS cnt
FROM (
SELECT 1
FROM "order" AS o
LEFT JOIN product p ON (p.order_id = o.id)
GROUP BY o.id
HAVING sum(p.price) >= 10
) x
SELECT o.id, COUNT(o.id) OVER () AS cnt
FROM "order" AS o
LEFT JOIN product p ON (p.order_id = o.id)
GROUP BY o.id
HAVING sum(p.price) >= 10
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question