MySql query statement solves the problem of "This column is not included in the aggregate function or the groupby clause", the groupby clause
First, introduce the statement source. The table structure and data are as follows:
Requirement: Find out the total salary (salary) of employees (personname) in different stores, store output for the same store, And multi_store output for different stores.
The correct query statement is as follows:
SELECT personname, (case when count (distinct Store)> 1 then 'multi _ store' else MAX (store) end), sum (Salary) FROM dbo. StaffInformation
Group by PersonName
First Thought of statement: (but cannot reach the desired result or directly report an error: "The column 'column name' in the selection list is invalid because this column is not included in the aggregate function or group by clause ")
SELECT personname, (case when count (distinct Store)> 1 then 'multi _ store' else store end), sum (Salary) FROM dbo. StaffInformation
Group by PersonName
A similar problem occurs: First, check whether the output field is needed. If necessary, but not in group by (because the group by field will not work, but if you do not put it, an error is returned)
We need to use a function to process a field such as store. In fact, it is quite simple to think about it. When you encounter some problems, you will be able to get your own solution.
If you have any questions, please feel free to comment. I usually come every day. Questions are discussed, learned, and improved.