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>