Jeff
Jeff

Reputation: 14279

SQL SUM a COUNT from an inner query

How do I sum up a column from an inner query like this? Obviously this doesnt work but it is essentially what I am trying to do. There are a lot of similar posts to this but this is unique in that mine has a HAVING clause.

SELECT SUM(cnt)
FROM
    (
        SELECT COUNT(*) as cnt
        FROM users
        GROUP BY email
        HAVING COUNT(*)>1
    )

Upvotes: 0

Views: 107

Answers (1)

Conrad Frix
Conrad Frix

Reputation: 52645

What you wrote should work provided you have an alias for your query

SELECT SUM(cnt)
FROM
    (
        SELECT COUNT(*) as cnt
        FROM users
        GROUP BY email
        HAVING COUNT(*)>1
    ) as t

SQL Fiddle

Upvotes: 1

Related Questions