The SELECT statement groupby field in SQL Server

Source: Internet
Author: User

SQLIn the group aggregation operation, why are other expressions included in the group by clause in the query column apart from the aggregate function operation?

Reward score: 10 | resolution time: | Questioner: ristal11

Hope you can advise

Best Answer

TheFieldMust be inGroup ByAmong them !~

You can simply understand this as a rule !~

For more information, see the following example !~

TableFieldAnd the data is as follows:

A B C

1 1 1

1 2 2

Select a, B, Sun (c) as sum_c from tableGroup ByA

Because C is collected by group A, the SQLQueryQuery Result

There will be a value equal to 1.

There will be a C value equal to 3.

Two different values are not grouped by B, so there is no way to handle them. This violates the sum function !~

Question

Thank you, but I 'd like to try again. We both group by A and Group by B, thenQueryQuery ResultYesReturn byYesBy B?

Answer

Both groups !~

 

 

 

Why?Group ByIt must be followed byFunctionAllField

Reward score: 10-resolution time:

For example, select store_name, sum (sales) from store_informationGroup ByStore_name is acceptable.
While
Select store_name, address, sum (sales) from store_informationGroup ByStore_name does not work.
Required
Select store_name, address, sum (sales) from store_informationGroup ByStore_name, address.

Questioner: yearningyou-Grade 1

Best Answer

ThisYesSQL syntax rules, useGroup By,FieldBesides aggregationFunctionMust appear inGroup ByYou can have lessGroup ByInField, But cannot containGroup ByNot inField

Respondent: a_li213-manager level 4 8-15

I will comment>

 

 

 

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.