MySql query statement solves the problem of "This column is not included in the aggregate function or the groupby clause", the groupby clause

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.