Skip to content
Advertisement

COUNT results from SQL Query with a HAVING clause

Are you able to use COUNT in a query with a HAVING clause so that the COUNT returns the number of rows? When I try, Im getting the count of the number of times the ID shows up in the table. Here is the query:

I get back 6 rows, which is fine, but i’d like to just get back the number 6.

I found I could do it this way, but was wondering if there was another, more elegant way:

`

Advertisement

Answer

Using COUNT with a GROUP BY clause will provide a count for each group. If you want the count of the number of groups, it will have to be a separate query (like your CTE example).

I would just use a simple subquery, instead of the CTE:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement