SQL基础--分组查询

时间:2022-06-14 22:28:38

SQL基础–分组查询


分组函数

函数名 功能 备注
SUM 求和
AVG 求平均值
COUNT 统计数目 COUNT(*)与COUNT(列名) / COUNT(DISTINCT(列名)),后者不包括列值为NULL的行
MAX 分组内最大值 任何数据类型
MIN 分组内最小值 任何数据类型

注意

字段为NULL,对分组函数计算结果的影响

函数名 影响 原因
AVG 求取的平均值不同 总和不变,个数减少
COUNT 统计数目 COUNT(*)与COUNT(列名) / COUNT(DISTINCT(列名)),后者不包括列值为NULL的行

分组函数具体到某个字段时,会跳过该字段为NULL的记录

SELECT AVG(NVL(commission_pct, 0)) FROM employees;
SELECT AVG(commission_pct) FROM employees;

SELECT中包含的非分组函数中的字段必须出现在GROUP BY 中

高级分组查询

ROLLUP、CUBE、GROUPING、GROUPING SETS

ROLLUP分组规则:

select group_id,job,name,avg(salary) from group_test group by rollup(group_id, job,name);



PL/SQL Developer Export









































    GROUP_ID JOB NAME AVG(SALARY)
1 10 Coding Bruce 1000
2 10 Coding   1000
3 10 Director Hill 1000
4 10 Director   1000
5 10 Architect Gideon 1000
6 10 Architect   1000
7 10 Programmer Clair 1000
8 10 Programmer   1000
9 10     1000
10 20 Coding Jason 2000
11 20 Coding   2000
12 20 Director Michael 2000
13 20 Director   2000
14 20 Architect Martin 2000
15 20 Architect   2000
16 20 Programmer Joey 2000
17 20 Programmer   2000
18 20     2000
19 30 Coding Rebecca 3000
20 30 Coding   3000
21 30 Director Sabrina 3000
22 30 Director   3000
23 30 Architect Richard 3000
24 30 Architect   3000
25 30 Programmer Rex 3000
26 30 Programmer   3000
27 30     3000
28 40 Coding Samuel 4000
29 40 Coding   4000
30 40 Director Wendy 4000
31 40 Director   4000
32 40 Architect Tina 4000
33 40 Architect   4000
34 40 Programmer Susy 4000
35 40 Programmer   4000
36 40     4000
37       2500


从右往左,先按group_id和job分组,再在组内按group_id分组,然后不分组;

对于GROUP BY ROLLUP(C1,c2, c3,…,cn):
从右往左,对c1…cn-1,cn, 依次减少一个进行分组,最后不分组统计;

CUBE分组规则:

select group_id,job,name,sum(salary) from group_test group by CUBE(group_id, job,name);



PL/SQL Developer Export





























































































    GROUP_ID JOB NAME SUM(SALARY)
1       40000
2     Rex 3000
3     Hill 1000
4     Joey 2000
5     Susy 4000
6     Tina 4000
7     Bruce 1000
8     Clair 1000
9     Jason 2000
10     Wendy 4000
11     Gideon 1000
12     Martin 2000
13     Samuel 4000
14     Michael 2000
15     Rebecca 3000
16     Richard 3000
17     Sabrina 3000
18   Coding   10000
19   Coding Bruce 1000
20   Coding Jason 2000
21   Coding Samuel 4000
22   Coding Rebecca 3000
23   Director   10000
24   Director Hill 1000
25   Director Wendy 4000
26   Director Michael 2000
27   Director Sabrina 3000
28   Architect   10000
29   Architect Tina 4000
30   Architect Gideon 1000
31   Architect Martin 2000
32   Architect Richard 3000
33   Programmer   10000
34   Programmer Rex 3000
35   Programmer Joey 2000
36   Programmer Susy 4000
37   Programmer Clair 1000
38 10     4000
39 10   Hill 1000
40 10   Bruce 1000
41 10   Clair 1000
42 10   Gideon 1000
43 10 Coding   1000
44 10 Coding Bruce 1000
45 10 Director   1000
46 10 Director Hill 1000
47 10 Architect   1000
48 10 Architect Gideon 1000
49 10 Programmer   1000
50 10 Programmer Clair 1000
51 20     8000
52 20   Joey 2000
53 20   Jason 2000
54 20   Martin 2000
55 20   Michael 2000
56 20 Coding   2000
57 20 Coding Jason 2000
58 20 Director   2000
59 20 Director Michael 2000
60 20 Architect   2000
61 20 Architect Martin 2000
62 20 Programmer   2000
63 20 Programmer Joey 2000
64 30     12000
65 30   Rex 3000
66 30   Rebecca 3000
67 30   Richard 3000
68 30   Sabrina 3000
69 30 Coding   3000
70 30 Coding Rebecca 3000
71 30 Director   3000
72 30 Director Sabrina 3000
73 30 Architect   3000
74 30 Architect Richard 3000
75 30 Programmer   3000
76 30 Programmer Rex 3000
77 40     16000
78 40   Susy 4000
79 40   Tina 4000
80 40   Wendy 4000
81 40   Samuel 4000
82 40 Coding   4000
83 40 Coding Samuel 4000
84 40 Director   4000
85 40 Director Wendy 4000
86 40 Architect   4000
87 40 Architect Tina 4000
88 40 Programmer   4000
89 40 Programmer Susy 4000


从右往左,先按name分组,再按job分组汇总,在按job分组内部在从右至左按name分组,最后按group分组时重复以上步骤

对于GROUP BY CUBE(C1,c2, c3,…,cn):
从右往左,现不分组统计,在对ci分组,完成后按ci-1分组,在组内对ci…cn重复之前步骤

GROUPING函数:

没有被GROUPING返回1, 被GROUPING返回0

GROUPING SETS:

按不同SET分组,再合并查询结果,如果SET之间有特殊包含关系,输出结果次序可能不同;