I tried the following query to select the number of groups resulting from a GROUP BY query:
SELECT count(*)
FROM (
SELECT count(*)
FROM MyTable
WHERE Col2 = 'x'
GROUP BY Col1
)
Unfortunately, this query is not valid: Incorrect syntax near ')'.
.
Note: I want to get exactly one row (I already found a solution with n times the result, where n = the group count).
SELECT COUNT(*)
FROM (
SELECT value = COUNT(*)
FROM MyTable
WHERE Col2 = 'x'
GROUP BY Col1
) е
but i think - you need to try this query -
SELECT COUNT(DISTINCT Col1)
FROM MyTable
WHERE Col2 = 'x'
SELECT count(*)
FROM (
SELECT 1 as dummy
FROM MyTable
WHERE Col2 = 'x'
GROUP BY Col1
) dt
No need to count rows in the sub-query, the result will be the same anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With