Analysis of group by and having usage in Oracle 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 和having 解释:前提必须了解sql语言中一种特殊的函数:聚合函数,
--例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根本区别就是它们一般作用在多条记录上。 
--WHERE关键字在使用集合函数时不能使用,所以在集合函数中加上了HAVING来起到测试查询结果是否符合条件的作用。
 create TABLE Table1
    (
        ID int identity(1,1) primary key NOT NULL,   
        classid int, 
        sex varchar(10),
        age int, 
    ) 
    
--添加测试数据
    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)
    Insert into Table1 values(4,'男',26)
    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


--举例子说明:查询table表查询每一个班级中年龄大于20,性别为男的人数
select COUNT(*)as '>20岁人数',classid  from Table1 where sex='男' group by classid,age having age>20 
--需要注意说明:当同时含有where子句、group by 子句 、having子句及聚集函数时,执行顺序如下:
--执行where子句查找符合条件的数据;
--使用group by 子句对数据进行分组;对group by 子句形成的组运行聚集函数计算每一组的值;最后用having 子句去掉不符合条件的组。
--having 子句中的每一个元素也必须出现在select列表中。有些数据库例外,如oracle.
--having子句和where子句都可以用来设定限制条件以使查询结果满足一定的条件限制。
--having子句限制的是组,而不是行。where子句中不能使用聚集函数,而having子句中可以。

 

Guess you like

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