ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Oracle over函数的学习笔记六 grouping groupid

Oracle over函数的学习笔记六 grouping groupid

原创 Linux操作系统 作者:lucy_lxy 时间:2011-11-02 14:16:11 0 删除 编辑

GROUP_ID


Text description of group_id

GROUP_ID用于区别GROUP BY语句结果中重复的组. 因此对于查询结果中国旅重复的组非常有用。它返回一个Oracle NUMBER 来唯一地辨别重复的组.这个函数只在含有GROUP BY clause的SELECT语句中可以使用。

如果对于特定的分组有n 个重复值存在,那么GROUP_ID返回范围为 0 to n-1的数值。

例子:SH/SH 用户

下面的例子赋值“1”给重复的co.country_region值,其结果来自对样例表 sh.countries and sh.sales的分组查询:

SELECT co.country_region, co.country_subregion sub,
   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 = '23-11月-01' AND
   co.country_region IN ('Americas', 'Europe')
GROUP BY co.country_region,co.country_subregion,
   ROLLUP (co.country_region, co.country_subregion);

COUNTRY_REGION       SUBREGION    Revenue        G

---------------- --------------- ---------- ----------

Americas       Northern America       202140.2          0

Europe         Western Europe         80839.81          0

Americas       Northern America       202140.2          2

Europe         Western Europe         80839.81          2

Americas       Northern America       202140.2          1

Europe         Western Europe         80839.81          1

GROUP BY不带--co.country_subregion

SELECT co.country_region, co.country_subregion sub,
   SUM(s.amount_sold) "Revenue",
   GROUP_ID() g2
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 = '23-11月-01' AND
   co.country_region IN ('Americas', 'Europe')
GROUP BY co.country_region,--co.country_subregion,
   ROLLUP (co.country_region, co.country_subregion);

COUNTRY_REGION      SUBREGION            Revenue          G2

------------- -----------------    ---------- ----------

Europe             Western Europe        80839.81          0

Americas           Northern America      202140.2          0

Europe                                   80839.81          0

Americas                                 202140.2          0

Europe                                   80839.81          1

Americas                                 202140.2          1

已选择6行。

您能添加下面的 HAVING子句到句子的尾部,以确保只有行满足GROUP_ID < 1的行数才被返回:

HAVING GROUP_ID() < 1

GROUPING

 

GROUPING从常规的分组行数里面辨别超级集合函数行. GROUP BY 扩展例如ROLLUP 和 CUBE 产生超级集合函数行,这些集合中的所有的值被表示为null. 使用GROUPING 函数, 你可以从常规行的null中辨别出超级集合行中的表示所有值的null.

在GROUPING 函数中的expr 必须与GROUP BY clause中的一项表达式相匹配. 该函数返回一个值1,如果该行expr 的值是表示所有值的null.否则,返回0.由GROUPING 函数返回值的类型是Oracle NUMBER.

See Also:

有关这些项目的讨论,可以查看SELECT 语句的group_by_clause

在下面的示例中,使用示例表hr.departments 和 hr.employees, 如果GROUPING 函数返回1 (表示一个超级集合行而不是表中的一个常规行),  那么在"JOB" 列显示串"All Jobs",而不是显示为null:

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);
DEPARTMENT                     JOB        Total Empl Average Sal
------------------------------ ---------- ---------- -----------
Accounting                     AC_ACCOUNT          1       99600
Accounting                     AC_MGR              1      144000
Accounting                     All Jobs            2      121800
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                        FI_ACCOUNT          5       95040
Finance                        FI_MGR              1      144000
Finance                        All Jobs            6      103200
……

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7177735/viewspace-710104/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-09-27

  • 博文量
    124
  • 访问量
    391605