简体   繁体   中英

GROUP BY Error when using one Count() operator in WHERE clause

I am getting an Error Code 1111. Invalid use of group function when using a Count() operator in the where condition. I believe the issue is that I am not allowed to use count operators in the WHERE clause, but I am otherwise uncertain how to get the count of the rows into a usable condition.

The aim of the query is to select fields where the Subject has seen more than one movie. The trick is that the subject_id is a composite key with movie_id. I am having issues filtering out rows where the subject have only seen one movie.

Query:

SELECT 
    measures.Subject_id, 
    measures.Movie_id, 
    measures.Median_heart_rate AS Variation 
FROM measures 
WHERE COUNT(measures.Subject_id) > 1;

Result:

Subject_id     Movie_id     Variation
001            1            45
001            35           15
002            42           2        
003            1            4
003            2            5

Expected results:

Subject_id     Movie_id     Variation
001            1            45
001            35           15   
003            1            4
003            2            5

COUNT should be in the SELECT clause and not in the WHERE clause.
To filter non-repeating subject_id rows, an inner query with COUNT can be used:

SELECT `subject_id`, 
       `movie_id`, 
       `median_heart_rate` AS Variation 
FROM   `measures` m1
WHERE  (SELECT Count(1) 
        FROM   `measures` m2
        WHERE  m2.`subject_id` = m1.`subject_id`) > 1; 

In MySQL 8.0, you can use window function COUNT(...) OVER(...) to check how many movies each user has seen. Then the outer query just filters out records based on this value:

SELECT *
FROM (
    SELECT 
        Subject_id, 
        Movie_id, 
        Variation,
        COUNT(*) OVER (PARTITION BY Subject_id) cnt
    FROM measures
) x
WHERE cnt > 1

The Solution ended up as such:

SELECT measures.Subject_id, measures.Movie_id, measures.Median_heart_rate AS Variation 
FROM measures 
WHERE measures.Subject_id IN (SELECT  DISTINCT Subject_id FROM measures GROUP BY Subject_id HAVING COUNT(Subject_id) > 1);

I would simply use exists :

SELECT m.Subject_id, m.Movie_id, 
       m.Median_heart_rate AS Variation 
FROM measures m
WHERE EXISTS (SELECT 1
              FROM measures m2
              WHERE m2.Subject_id = m.Subject_id AND
                    m2.Movie_id <> m.Movie_id
             );

If your primary key is indeed measures(Subject_id, Movie_id) , then this should also have very good performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM