Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select max() from count() [duplicate]

Tags:

Possible Duplicate:
every derived table must have its own alias

I need to find maximum of actions per user

table columns: action_id, action_status, user

request:

SELECT MAX(`counted`) FROM
(
SELECT COUNT(*) AS `counted`
FROM `table_actions`
WHERE `status` = "good"
GROUP BY `user`
)

error message: "Every derived table must have its own alias"

what is wrong?..

like image 722
Zdomb Avatar asked Apr 28 '12 12:04

Zdomb


People also ask

Can we use max and count together in SQL?

And the short answer to the above question is, no. You can't. It is not possible to nest aggregate functions.

Does count (*) include duplicate values?

Yes, when using the COUNT() function on a column in SQL, it will include duplicate values by default. It essentially counts all rows for which there is a value in the column. If you wanted to count only the unique values in a column, then you can utilize the DISTINCT clause within the COUNT() function.

How do you find the maximum occurrence in SQL?

Select TOP 1 City, Count(City) AS 'MAX_COUNT' FROM Customer Group By City Order By 'MAX_COUNT' DESC; Hope this simple query will help many one. Show activity on this post. If you are using Oracle Database, you can simply use stats_mode function this will return single value with highest occurrences.

Does Count ignore duplicate values?

Yes, we can ignore duplicate rows in COUNT using DISTINCT.


1 Answers

That just means MySQL insists that you give the inner SELECT a name, like:

SELECT MAX(counted) FROM
(
    SELECT COUNT(*) AS counted
    FROM table_actions
    WHERE status = "good"
    GROUP BY user
) AS counts;
like image 61
Michael Slade Avatar answered Sep 27 '22 19:09

Michael Slade