Search code examples
mysqldateworkbench

MySQL: Error 1055 When attempting to Return Count of Referrer by Month in Year 2020


I'm getting the following error code:

Error Code: 1055. Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'usergrowthhomework.weblogs.day' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

This code is a result of me attempting to run the following query, which is supposed to return the number of times a 'referrer' appears each month in the year 2020:

SELECT MONTH(day) MONTH, referrer, COUNT(referrer) AS cumulative_pageviews
FROM weblogs
WHERE YEAR(day) >= '2020'
GROUP BY MONTH(day), referrer
ORDER BY day, referrer;

My table's columns look like this:

user_no - int name - varchar(200) referrer - varchar(200) start_date - timestamp day - date

Can anyone explain why I'm getting such an error? I clearly included all appropriate expressions in the GROUP BY and SELECT clauses.

I'm using the MySQL workbench to run this query.


Solution

  • As the error suggests, the day column in the order by clause isn't in the group by clause, and thus can't be used there. You should order by MONTH(day) instead:

    SELECT   MONTH(day) MONTH, referrer, COUNT(referrer) AS cumulative_pageviews
    FROM     weblogs
    WHERE    YEAR(day) >= 2020
    GROUP BY MONTH(day), referrer
    ORDER BY MONTH(day), referrer;
    -- Here -^