This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10836|Reply: 0

[Source] Analysis of group by two or more conditions

[Copy link]
Posted on 3/11/2015 10:39:02 AM | | | |
First of all, a simple explanation of group by:
   Group by generally makes sense to use it with aggregation functions, such as count sum avg, etc., using two elements of group by:
   (1) The field that appears after select is either in the aggregation function or in the group by.
   (2) To filter the results, you can use where first and then group by or group by first and then having
Let's take a look at the analysis of multiple conditions of group by:
Enter the following statement in the SQL queryer
create table test
(
a varchar(20),
b varchar(20),
c varchar(20)
)
insert into test values(1,'a','a')
insert into test values(1,'a','a')
insert into test values(1,'a','a')
insert into test values(1,'a','a')
insert into test values(1,'a','b')
insert into test values(1,'b','b')
insert into test values(1,'b','b')
insert into test values(1,'b','b')
First query
select * from test; The result is shown below:
In the results, according to column b: 5 a and 3 b.

According to column C: 4 A and 4 B.

The second group by column b code is as follows
select   count(a),b from test group by b
The third group according to column C is as follows
select count(a),c from test group by c

The fourth time is grouped according to the two conditions of b and c
select count(a),b,c from test group by b,c
The fifth time is grouped in the order of c and b
select count(a),b,c from test group by c,b
It can be seen that group by two conditions work in the working process:
First, the values of column b of the first condition are grouped into the first group: 1-5, and the second group is 6-8, and then the two existing groupings are grouped with the values of column c of condition 2, and it is found that the first group can be divided into two groups 1-4,5





Previous:How to change the font in plsql developer?
Next:Oracle query results automatically generate serial numbers
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com