MYSQL||报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated

Broken thoughts:

        Brother Meng, I’m back with Hu Hansan (。・∀・)ノ゙ Hi, I was in the final exam recently, and I accidentally didn’t post for 20 days, and I didn’t succeed in the KPI at the end of the year, so I’ll make it up today §(*  ̄▽ ̄*)§

1. Problem description

        Today, I am doing a SQL question on Niuke.com: Analysis of Niuke's Course Orders (5), and those who are interested in the specific content will search for it by themselves. Here is a brief description~~

        The encoding requirements are as follows:

         The code I wrote is as follows (it's an old woman's foot wrap, smelly and long, who called me a rookie? 〒▽〒

WITH base_table AS (
    SELECT user_id,date,DENSE_RANK() over (partition by user_id order by date) AS rnk
    FROM order_info
    WHERE date>'2025-10-15'
    AND status='completed'
    AND product_name IN ('C++','Java','Python')
),
table1 AS (
    SELECT user_id, date as first_buy_date
    FROM base_table
    WHERE rnk=1
),
table2 AS (
    SELECT user_id, date as second_buy_date
    FROM base_table
    WHERE rnk=2
),
table3 AS (
    SELECT user_id, COUNT(user_id) AS cnt
    FROM base_table
)
SELECT base_table.user_id,first_buy_date,second_buy_date,cnt
FROM base_table,table1,table2,table3
WHERE base_table.user_id=table1.user_id
AND base_table.user_id=table2.user_id
AND base_table.user_id=table3.user_id

        Then click on the self-test to run, and it will report an error gloriously, okay! The content is: SQL_ERROR_INFO: "In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'base_table.user_id'; this is incompatible with sql_mode=only_full_group_by" 


2. Reason analysis

        The error statement is simply translated as: "In an aggregate query without GROUP BY, expression 1 of the SELECT list contains the non-aggregated column 'base_table.user_id'; this is incompatible with sql_mode=only_full_group_by".

        Then we will check where the statement uses 'base_table.user_id'. It can be seen from the code that 'base_table.user_id' appears in table1, table2, table3 and the final query body;

        The error statement prompts that the group by statement is not used where the aggregate function is used . This is not talking about table3 hhh, table3 uses the count function, but does not add the group by statement.


3. Solutions

        After analyzing the problem, you will naturally know how to solve it~~ Just add the GROUP BY statement to the table3 query statement!

         PS: Some solutions on the Internet are to directly modify the configuration file to make it compatible with sql_mode=only_full_group_by, maybe this is one of the solutions, but I am using Niuke.com for programming, and I cannot modify the configuration file o( ̄┰  ̄*)ゞ It’s better to modify your code obediently

        After this post, the water post +1 ο(=•ω<=)ρ⌒☆

Guess you like

Origin blog.csdn.net/Inochigohan/article/details/122380585