How do I get the count for a SQL query containing a GROUP BY statement?

Mike H-R

So I have a SQL query like this:

SELECT p.*
FROM parent p JOIN join_table j ON p.id=j.parent_id
WHERE j.child_id=1 OR j.child_id=2
GROUP BY j.parent_id
HAVING COUNT(j.child_id)=2;

Which I got from this excellent answer here. Now I would like to modify the query to return the count of all the parent elements there. unfortunately due to the GROUP BY statement I am unable to do this (as it will give the count of each grouped statement).

My attempt was the obvious and naive approach:

SELECT COUNT(DISTINCT(p.id))
FROM parent p JOIN join_table j ON p.id=j.parent_id
WHERE j.child_id=1 OR j.child_id=2
GROUP BY j.parent_id
HAVING COUNT(j.child_id)=2;

Thanks for helping.

MinhD

Try this:

SELECT COUNT(*) FROM
   (SELECT p.*
   FROM parent p JOIN join_table j ON p.id=j.parent_id
   WHERE j.child_id=1 OR j.child_id=2
   GROUP BY j.parent_id
   HAVING COUNT(j.child_id)=2) AS res;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I convert Months from Numeral to String Form in SQL Query Containing GROUP BY?

From Dev

How do I group by the count value in sql

From Dev

How do I count this SQL query?

From Dev

How do I get my LINQ Query to generate the SQL statement I expect?

From Dev

How do I get count of a row in SQL

From Dev

Iterate through LINQ to SQL query containing COUNT and GROUP BY

From Dev

How do I get the following SQL query right to choose the top N results within a group?

From Dev

How do I get a "select count(*) group by" using laravel eloquent

From Dev

How do I count specific rows in GROUP BY SQL

From Dev

SQL query to get count and group by output values

From Dev

How do I run an SQL update query using a like statement

From Dev

How do I declare a query in an oracle pl/sql statement?

From Dev

How do I run an SQL update query using a like statement

From Dev

How do I transfer this SQL statement into a LINQ query?

From Dev

How do I return multiple columns and rows in a SQL Count Query?

From Dev

Using SQL, how do I COUNT the number of results from a query?

From Dev

how do i get last result in group by MySQL query (not the first)

From Dev

How do I group by minimum values in a SQL query?

From Dev

SQL query how do i group by name in a table

From Dev

How do I find the average of the result of a group by SQL query?

From Dev

How can I do this SQL query (aggregate group by and where in clause)?

From Dev

SQL Query Group by statement

From Dev

SQL Query with Group By statement

From Dev

How to count rows by group in a SQL query

From Dev

How do I get the average of a count in Microsoft SQL Server 2008

From Dev

"group by" needed in count(*) SQL statement?

From Dev

How to get count of distinct following GROUP BY in SQL?

From Dev

How do I convert this tSQL statement to LINQ using group by in a sub query

From Dev

How do I group by month the count of a column?

Related Related

  1. 1

    How do I convert Months from Numeral to String Form in SQL Query Containing GROUP BY?

  2. 2

    How do I group by the count value in sql

  3. 3

    How do I count this SQL query?

  4. 4

    How do I get my LINQ Query to generate the SQL statement I expect?

  5. 5

    How do I get count of a row in SQL

  6. 6

    Iterate through LINQ to SQL query containing COUNT and GROUP BY

  7. 7

    How do I get the following SQL query right to choose the top N results within a group?

  8. 8

    How do I get a "select count(*) group by" using laravel eloquent

  9. 9

    How do I count specific rows in GROUP BY SQL

  10. 10

    SQL query to get count and group by output values

  11. 11

    How do I run an SQL update query using a like statement

  12. 12

    How do I declare a query in an oracle pl/sql statement?

  13. 13

    How do I run an SQL update query using a like statement

  14. 14

    How do I transfer this SQL statement into a LINQ query?

  15. 15

    How do I return multiple columns and rows in a SQL Count Query?

  16. 16

    Using SQL, how do I COUNT the number of results from a query?

  17. 17

    how do i get last result in group by MySQL query (not the first)

  18. 18

    How do I group by minimum values in a SQL query?

  19. 19

    SQL query how do i group by name in a table

  20. 20

    How do I find the average of the result of a group by SQL query?

  21. 21

    How can I do this SQL query (aggregate group by and where in clause)?

  22. 22

    SQL Query Group by statement

  23. 23

    SQL Query with Group By statement

  24. 24

    How to count rows by group in a SQL query

  25. 25

    How do I get the average of a count in Microsoft SQL Server 2008

  26. 26

    "group by" needed in count(*) SQL statement?

  27. 27

    How to get count of distinct following GROUP BY in SQL?

  28. 28

    How do I convert this tSQL statement to LINQ using group by in a sub query

  29. 29

    How do I group by month the count of a column?

HotTag

Archive