Search code examples
sqlqsqlquery

MAX and COUNT function doesn't work together


I want to count id_r and then return the maxim value of count using

 MAX(COUNT(id_r))

but shows me this error

the error

Thanks :)


Solution

  • Try this:

    SELECT MAX(e1) as Expr1 FROM (
    SELECT COUNT(id_r) as e1
    FROM Angajat) as t1
    

    COUNT(id_r) wil return only 1 result since there is no group by clause. Hence, there is no use of max.

    You need to add a group by clause in subquery:

    SELECT MAX(e1) as Expr1 FROM (
    SELECT column1, COUNT(id_r) as e1
    FROM Angajat
    GROUP BY column1
    ) as t1