Grouping函数:
可以接受一列,返回0或1。如果列值为空,则返回1,非空则返回0。它只能在rollup和或cube函数中使用,因为在统计中显示“全部”的那一项统计值时,那一项的标签通常是空的,这时grouping就非常有用,还可以在grouping的基础上进行decode, case等进行美化。
SQL> select grouping(index_type) g_ind, grouping(status) g_st, index_type, status, count(*)
2 from t group by rollup(index_type, status) order by 1, 2;
G_IND G_ST INDEX_TYPE STATUS COUNT(*)
---------- ---------- --------------------------- -------- ----------
0 0 LOB N/A 1
0 0 LOB VALID 572
0 0 FUNCTION-BASED NORMAL VALID 17
0 0 FUNCTION-BASED DOMAIN VALID 1
0 0 IOT - TOP VALID 115
0 0 CLUSTER VALID 10
0 0 NORMAL VALID 4557
0 0 NORMAL N/A 56
0 0 DOMAIN VALID 1
0 0 BITMAP VALID 8
0 0 BITMAP N/A 7
0 1 IOT - TOP 115
0 1 FUNCTION-BASED DOMAIN 1
0 1 DOMAIN 1
0 1 CLUSTER 10
0 1 BITMAP 15
0 1 FUNCTION-BASED NORMAL 17
0 1 NORMAL 4613
0 1 LOB 573
1 1 5345
20 rows selected
Grouping_id函数:
比grouping还强点,可以接收多个列,这几个列都不为空时,返回0,只要有一个为空,则返回1,如果都为空,则返回3。
下面这条查询把grouping_id牛刀杀鸡,当做grouping用了,可以看到结果跟用grouping是完全一样。
SQL> select grouping_id(index_type) g_ind, grouping_id(status) g_st, index_type, status, count(*)
2 from t group by rollup(index_type, status) order by 1, 2;
G_IND G_ST INDEX_TYPE STATUS COUNT(*)
---------- ---------- --------------------------- -------- ----------
0 0 LOB N/A 1
0 0 LOB VALID 572
0 0 FUNCTION-BASED NORMAL VALID 17
0 0 FUNCTION-BASED DOMAIN VALID 1
0 0 IOT - TOP VALID 115
0 0 CLUSTER VALID 10
0 0 NORMAL VALID 4557
0 0 NORMAL N/A 56
0 0 DOMAIN VALID 1
0 0 BITMAP VALID 8
0 0 BITMAP N/A 7
0 1 IOT - TOP 115
0 1 FUNCTION-BASED DOMAIN 1
0 1 DOMAIN 1
0 1 CLUSTER 10
0 1 BITMAP 15
0 1 FUNCTION-BASED NORMAL 17
0 1 NORMAL 4613
0 1 LOB 573
1 1 5345
20 rows selected
下面这条查询才体现了grouping_id的作用。
SQL> select grouping_id(index_type, status) g_st, index_type, status, count(*)
2 from t group by rollup(index_type, status) order by 1, 2;
G_ST INDEX_TYPE STATUS COUNT(*)
---------- --------------------------- -------- ----------
0 BITMAP VALID 8
0 BITMAP N/A 7
0 CLUSTER VALID 10
0 DOMAIN VALID 1
0 FUNCTION-BASED DOMAIN VALID 1
0 FUNCTION-BASED NORMAL VALID 17
0 IOT - TOP VALID 115
0 LOB N/A 1
0 LOB VALID 572
0 NORMAL N/A 56
0 NORMAL VALID 4557
1 BITMAP 15
1 CLUSTER 10
1 DOMAIN 1
1 FUNCTION-BASED DOMAIN 1
1 FUNCTION-BASED NORMAL 17
1 IOT - TOP 115
1 LOB 573
1 NORMAL 4613
3 5345
20 rows selected
根据grouping_id接收不同的列的组合,可以美化出很神奇的结果。