Group by grouping usage

   Group means grouping. If you add by , group by means grouping according to what. Its function is to divide a data set into several area segments according to the rules behind by , and the value of each area segment is the same, and then process the data of each area.

  When explaining the group by statement, " how many (which) XXX are in each XXX " is often used

  

Execution order of SQL statements with group by

  The following simple grouping statement seems to have this example in the previous university books, "how many people in each department," then you need to query the basic table according to the department ID: select departmentID,count(*) ftrom basicDepartment group by departmentID , This first group by will group the queried data, and then calculate the number of people in each group.

  ! ! ! If the sentence is as follows:

  select departmentID,departmentName,count(*) ftrom basicDepartment group by departmentID, a field of department name should be added to the query result, but the back of group by  remains unchanged, then an error will be reported: "can not find the field departmentName ", the reason is that departmentName does not have It is included in the clause or aggregate function of group by . For the specific reasons, please refer to the explanation given by others on the Internet. I think it is easy to understand: "If it is in the return set field, these fields should either be included in the back of the Group By statement. , as the basis for grouping; or it will be included in the aggregation function.

Detailed explanation of the error : Let's take a look at the execution process of group by  , first perform the select  operation to return an assembly, and then perform the grouping operation. At this time, he will group according to the fields behind group by  and group the same fields. Also called a column of data, if there is no such field after group by  , it will be divided into a lot of data. But grouping can only divide the same data into two columns of data, and only one field can be placed in one column, so those data systems that are not grouped do not know where to put the data, so this error occurs.”

  In a grouping situation, there is only one record, and a data grid cannot store multiple data, so it is necessary to convert the values ​​of multiple columns into a single value through certain processing, and then put them in the corresponding data grid to complete this The step is the aggregation function.

 

The difference between group by  and group by all

    Usually, there will be query conditions in the SQL query statement to filter out records that do not meet the conditions. If it is a combination of where and group by , then the where condition will work, and the group will not remove the unqualified records after the group meeting. If it is a combination of where and group by all , the where condition will be invalid, and the group will count all the records, regardless of whether they meet the conditions or not, together.

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326357382&siteId=291194637