GROUP_ID函数可以辨识出GROUP
BY分组后的冲突的结果集,他可以用来过滤冲突的结果集,它返回一个oracle NUMBER
类型的数据来表示结果集,只适用于使用Select语句并包含GROUP
BY的情况。如果一个结果集中有n个冲突的记录,
GROUP_ID将会返回0-(n-1)间的数字。
Examples:
The following example assigns the value 1 to the duplicate co.country_region grouping from a query on the sample tables sh.countries and sh.sales:
SELECT co.country_region, co.country_subregion,
SUM(s.amount_sold) "Revenue", GROUP_ID() g
FROM sales s, customers c, countries co
WHERE s.cust_id = c.cust_id
AND c.country_id = co.country_id
AND s.time_id = '1-JAN-00'
AND co.country_region IN ('Americas', 'Europe')
GROUP BY GROUPING SETS ( (co.country_region, co.country_subregion),
(co.country_region, co.country_subregion) )
ORDER BY co.country_region, co.country_subregion, "Revenue", g;
COUNTRY_REGION COUNTRY_SUBREGION Revenue G
-------------------- ------------------------------ ---------- ----------
Americas Northern America 944.6 0
Americas Northern America 944.6 1
Europe Western Europe 566.39 0
Europe Western Europe 566.39 1
To ensure that only rows with GROUP_ID < 1 are returned, add the following HAVING clause to the end of the statement :
HAVING GROUP_ID() < 1
GROUPING
函数可以区分出正常结果集中的非正常结果,例如ROLLUP
和CUBE
扩展产生的所有值均为null的集合,使用GROUPING
函数,你可以用一个其他值类取代正常结果集中的null值。
GROUPING
函数中的表达式必须与GROUP BY子句中的表达式相匹配。如果代表地所有值的集合的该行中expr的值是null的话,那么该函数返回值为1。否则,它将返回零。分组函数返回的值的数据类型是Oracle NUMBER。
Examples:
In the following example, which uses the sample tables hr.departments and hr.employees, if the GROUPING function returns 1 (indicating a superaggregate row rather than a regular row from the table), then the string "All Jobs" appears in the "JOB" column instead of the null that would otherwise appear:
SELECT
DECODE(GROUPING(department_name), 1, 'ALL DEPARTMENTS', department_name)
AS department,
DECODE(GROUPING(job_id), 1, 'All Jobs', job_id) AS job,
COUNT(*) "Total Empl",
AVG(salary) * 12 "Average Sal"
FROM employees e, departments d
WHERE d.department_id = e.department_id
GROUP BY ROLLUP (department_name, job_id)
ORDER BY department, job;
DEPARTMENT JOB Total Empl Average Sal
------------------------------ ---------- ---------- -----------
ALL DEPARTMENTS All Jobs 106 77481.0566
Accounting AC_ACCOUNT 1 99600
Accounting AC_MGR 1 144096
Accounting All Jobs 2 121848
Administration AD_ASST 1 52800
Administration All Jobs 1 52800
Executive AD_PRES 1 288000
Executive AD_VP 2 204000
Executive All Jobs 3 232000
Finance All Jobs 6 103216
Finance FI_ACCOUNT 5 95040
. . .
GROUPING_ID
函数:GROUPING_ID
函数返回与一行相关联的满足GROUPING
函数的数字。GROUPING_ID
只适用于Select查询并包含有分组的情况,例如ROLLUP
或CUBE
, and a GROUPING
函数。在有多个分组查询的情况下并需要根据特定的行来确定组需要许多分组函数,这会导致很复杂的SQL,此时适合适用GROUPING_ID
函数。
groupingid在功能上等价于获取多个分组函数的结果,并将它们连接到一个位向量(一串1和0)。通过使用groupingid,您可以避免对多个分组函数的需要,并使行过滤条件更容易表达。行过滤更容易使用groupingid,因为所需的行可以用groupingid=n的单一条件来标识,当在单个表中储存多个级别的聚合时,该函数特别有用。
Examples:
The following example shows how to extract grouping IDs from a query of the sample table sh.sales:
SELECT channel_id, promo_id, sum(amount_sold) s_sales,
GROUPING(channel_id) gc,
GROUPING(promo_id) gp,
GROUPING_ID(channel_id, promo_id) gcp,
GROUPING_ID(promo_id, channel_id) gpc
FROM sales
WHERE promo_id > 496
GROUP BY CUBE(channel_id, promo_id)
ORDER BY channel_id, promo_id, s_sales, gc;
CHANNEL_ID PROMO_ID S_SALES GC GP GCP GPC
---------- ---------- ---------- ---------- ---------- ---------- ----------
2 999 25797563.2 0 0 0 0
2 25797563.2 0 1 1 2
3 999 55336945.1 0 0 0 0
3 55336945.1 0 1 1 2
4 999 13370012.5 0 0 0 0
4 13370012.5 0 1 1 2
999 94504520.8 1 0 2 1
94504520.8 1 1 3 3
文档链接:https://docs.oracle.com/database/121/SQLRF/functions081.htm#SQLRF00648