Analysis of group by and having usage in sql

-- Group by usage analysis in sql:
-- The Group By statement is literally understood in English as "grouping (Group) according to (by) certain rules".
-- Its function is to divide a data set into several small areas through certain rules, and then perform data processing for several small areas.
--Note: group by is sorted first and then grouped;
--Example description: If you want to use group by, the word "every" is generally used. For example, there is a table like this: how many people in each department To use the grouping technique
select DepartmentID as 'Department Name',
COUNT(*) as 'Number' from BasicDepartment group by DepartmentID

--This is to use group by + field for grouping, in which we can understand that we have
grouped the data set according to the name ID of the department --DepartmentID; and then how many statistics of each group are;
- - If instead of count(*) use a syntax like
select DepartmentID, DepartmentName from BasicDepartment group by DepartmentID

-- An error will occur
-- Msg 8120, Level 16, State 1, Line 1
-- The column 'BasicDepartment.DepartmentName' in the select list is invalid because it is not included in an aggregate function or GROUP BY clause.
--This is a point we need to pay attention to. If in the return set fields, these fields will either be included after the Group By statement,
-- as the basis for grouping; or they will be included in the aggregation function.
--Detailed explanation of the error: Let's look at the execution process of group by, first perform the select operation to return an assembly,
--then perform the grouping operation, at this time he will group according to the field behind group
by-- , and the same field is 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 who are not grouped
--The data system does not know where to put the data, so this error occurs
--At present, there is only one record in a grouping situation, and multiple values ​​cannot be placed in one data grid.
--So here, it is necessary to convert these multi-valued columns into single values ​​through certain processing, and then put them in the corresponding The
-- data grid, then the aggregate function is what completes this step. That's why these functions are called aggregate functions

--group by all syntax analysis: --If
the ALL keyword is used, the query results will include all groups produced by the GROUP BY clause, even if some groups do not have rows that match the search criteria.
--Without the ALL keyword, a SELECT statement containing a GROUP BY clause will not display groups with no rows that match the condition.
select DepartmentID,DepartmentName as 'Department Name',
COUNT(*) as 'Number' from BasicDepartment group by all DepartmentID,DepartmentName


-- group by and having Explanation: The premise must be to understand a special function in the sql language: aggregate function,
-- such as SUM, COUNT, MAX, AVG, etc. The fundamental difference between these functions and other functions is that they generally act on multiple records.
The --WHERE keyword cannot be used when using aggregate functions, so HAVING is added to the aggregate function to test whether the query results meet the conditions.
 create TABLE Table1
    (
        ID int identity(1,1) primary key NOT NULL,  
        classid int,
        sex varchar(10),
        age int,
    ) --Add
   
test data
    Insert into Table1 values(1,'male',20)
    Insert into Table1 values(2,'Female',22)
    Insert into Table1 values(3,'Male',23)
    Insert into Table1 values(4,'Male',22)
    Insert into Table1 values(1,'Male',24)
    Insert into Table1 values(2,'Female',19)
    Insert into Table1 values(4,'Male',
    Insert into Table1 values(1,'男',24)
    Insert into Table1 values(1,'男',20)
    Insert into Table1 values(2,'女',22)
    Insert into Table1 values(3,'男',23)
    Insert into Table1 values(4,'男',22)
    Insert into Table1 values(1,'男',24)
    Insert into Table1 values(2,'女',19


--Example description: Query the table table to query the number of males whose age is greater than 20 in each class
select COUNT(*)as '>20 years old',classid from Table1 where sex='male' group by classid,age having age>20 --Note
: When there are where clause, group by clause, having clause and aggregate function at the same time, the execution order is as follows:
--Execute where clause to find eligible data;
--Use group The by clause groups the data; the aggregate function is run on the groups formed by the group by clause to calculate the value of each group; finally, the having clause is used to remove the groups that do not meet the conditions.
Every element in the --having clause must also appear in the select list. Some database exceptions, such as oracle.
--having clause and where clause can be used to set constraints to make query results meet certain conditions.
The --having clause restricts groups, not rows. Aggregate functions cannot be used in the where clause, but can be used in the having clause.

Guess you like

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