Group BY is a literal understanding of grouping data according to the rules specified by by, which is the grouping of a dataset into several "small areas", and then data processing for several "small areas".
Select category, sum (quantity) as quantity and from
A
Group BY category
Returns the following table, which is actually a subtotal.
Select category, sum (quantity) as quantity and from
A Group by category order by
sum (quantity) desc
Returns the following table of results
You cannot use the and DESC of order by quantity in Access, but you can in SQL Server.
5. The field limit specified by select in Group by
Select category, sum (quantity) as quantity sum, summary from
A Group by category Order by
category desc
Example 3 prompts for an error after execution, as shown in the following illustration. It is important to note that the field specified in the select is either to be included after the group by statement, as a basis for grouping, or to be included in an aggregate function.
Select category, Summary, sum (quantity) as quantity and from
A
Group BY all category, summary
In Example 4, you can specify a summary field because the summary field is included in multiple-column groupings, and its results are as follows
"Multiple-column groupings" are actually grouped by Dole (category + summary) merged values, and in example 4 you can see "A, a2001, 13" as "A, a2001, 11" and "A, a2001, 2" two records merge.
Although group by all is supported in SQL Server, a future version of Microsoft SQL Server removes group by all, avoiding the use of group by all in new development work. "GROUP BY all" is not supported in access, but multiple-column groupings are also supported in Access, and SQL in SQL Server above can be written in Access
Select category, Summary, sum (quantity) as quantity and from
A
Group by category, summary
7. Group by and aggregate functions
As mentioned in Example 3, the field specified by select in the group BY statement must be a group by field, and other fields must be included in the aggregate function if they want to appear in the Select, and the common aggregate functions are the following table:
| function |
function |
of Support |
| SUM (column name) |
Sum |
|
| Max (column name) |
Maximum Value |
|
| Min (column name) |
Minimum value |
|
| AVG (column name) |
Average |
|
| A (column name) |
First record |
Access support only |
| Last (column name) |
Last record |
Access support only |
| Count (column name) |
Number of statistical records |
Notice the difference with count (*) |
Example 5: Find the average of each group
Select category, AVG (quantity) as average from A Group by category;
Example 6: Find the number of records in each group
Select category, COUNT (*) as record number from A Group by category;
Example 7: Find the number of records in each group
8. The difference between having and where
- The action of the WHERE clause is to remove rows that do not conform to the where condition before grouping the query results, that is, to filter the data before the group, where the cluster function cannot be included, and the Where condition to filter out a particular row.
- The HAVING clause is used to filter the groups that meet the criteria, that is, to filter the data after grouping, often including the clustering function, using the having condition to filter out specific groups, or multiple grouping criteria for grouping.
Select category, sum (quantity) as quantity and from A
Group by category having
sum (quantity) > 18
Example 9:having and where to use the federated method
Select category, sum (quantity) from A
where quantity gt;8
Group by category having
SUM (quantity) GT; 10
9, Compute and Compute by
SELECT * from A where quantity > 8
SELECT * from
A
where quantity >8
compute max (quantity), min (quantity), AVG (quantity)
The results of the implementation are as follows:
The COMPUTE clause is able to observe the data details of the query results or to count the columns of data (such as Max, Min, and Avg in Example 10), and the results are composed of the select list and the compute statistic results.
SELECT *
from A
where quantity >8 order
by category
compute max (quantity), min (quantity), AVG (quantity) by category
The results of the implementation are as follows:
Example 11 compares the "Order by category" and "... by category" with example 10, and the results of example 10 are actually displayed by grouping (a, B, c), each of which consists of a list of changes and the number of reorganizations, plus:
- The COMPUTE clause must be used in conjunction with the ORDER BY clause
- Compute...by the group by is only able to get the statistical results of each set of data and not to see the groups of data
Compute and compute by are not very important in actual development, SQL Server supports compute and compute by, and access does not support