转自:

http://blog.csdn.net/xiadingling/article/details/7876120

GROUPING函数可以接受一列,返回0或者1。如果列值为空,那么GROUPING()返回1;如果列值非空,那么返回0。GROUPING只能在使用ROLLUP或CUBE的查询中使用。当需要在返回空值的地方显示某个值时,GROUPING()就非常有用。

关于ROLLUP和CUBE函数的使用,请参见我的另一篇文章。

http://blog.csdn.net/wh62592855/archive/2009/11/16/4817920.aspx

1、在ROLLUP中对单列使用GROUPING()

SQL> select division_id,sum(salary)
  2  from employees2
  3  group by rollup(division_id)
  4  order by division_id;

DIV SUM(SALARY)
--- -----------
BUS     1610000
OPE     1320000
SAL     4936000
SUP     1015000
        8881000

加上GROUPING来看看

SQL> select grouping(division_id),division_id,sum(salary)
  2  from employees2
  3  group by rollup(division_id)
  4  order by division_id;

GROUPING(DIVISION_ID) DIV SUM(SALARY)
--------------------- --- -----------
                    0 BUS     1610000
                    0 OPE     1320000
                    0 SAL     4936000
                    0 SUP     1015000
                    1         8881000
可以看到,为空的地方返回1,非空的地方返回0。

2、使用CASE转换GROUPING()的返回值

可能你会觉得前面的0和1太枯燥了,代表不了任何意义,说白了就是不够人性化,呵呵。这个时候我们可以使用CASE来转换为一些有意义的值。

SQL> select
  2  case grouping(division_id)
  3  when 1 then 'all divisions'
  4  else division_id
  5  end as div,
  6  sum(salary)
  7  from employees2
  8  group by rollup(division_id)
  9  order by division_id;

DIV           SUM(SALARY)
------------- -----------
BUS               1610000
OPE               1320000
SAL               4936000
SUP               1015000
all divisions     8881000

3、使用CASE和GROUPING()转换多个列的值

SQL> select
  2  case grouping(division_id)
  3  when 1 then 'all divisions'
  4  else division_id
  5  end as div,
  6  case grouping(job_id)
  7  when 1 then 'all jobs'
  8  else job_id
  9  end as job,
 10  sum(salary)
 11  from employees2
 12  group by rollup(division_id,job_id)
 13  order by division_id,job_id;

DIV           JOB      SUM(SALARY)
------------- -------- -----------
BUS           MGR           530000
BUS           PRE           800000
BUS           WOR           280000
BUS           all jobs     1610000
OPE           ENG           245000
OPE           MGR           805000
OPE           WOR           270000
OPE           all jobs     1320000
SAL           MGR          4446000
SAL           WOR           490000
SAL           all jobs     4936000

DIV           JOB      SUM(SALARY)
------------- -------- -----------
SUP           MGR           465000
SUP           TEC           115000
SUP           WOR           435000
SUP           all jobs     1015000
all divisions all jobs     8881000

16 rows selected.

4、CUBE与GROUPING()结合使用

SQL> select
  2  case grouping(division_id)
  3  when 1 then 'all divisions'
  4  else division_id
  5  end as div,
  6  case grouping(job_id)
  7  when 1 then 'all jobs'
  8  else job_id
  9  end as job,
 10  sum(salary)
 11  from employees2
 12  group by cube(division_id,job_id)
 13  order by division_id,job_id;

DIV           JOB      SUM(SALARY)
------------- -------- -----------
BUS           MGR           530000
BUS           PRE           800000
BUS           WOR           280000
BUS           all jobs     1610000
OPE           ENG           245000
OPE           MGR           805000
OPE           WOR           270000
OPE           all jobs     1320000
SAL           MGR          4446000
SAL           WOR           490000
SAL           all jobs     4936000

DIV           JOB      SUM(SALARY)
------------- -------- -----------
SUP           MGR           465000
SUP           TEC           115000
SUP           WOR           435000
SUP           all jobs     1015000
all divisions ENG           245000
all divisions MGR          6246000
all divisions PRE           800000
all divisions TEC           115000
all divisions WOR          1475000
all divisions all jobs     8881000

21 rows selected.

5、使用GROUPING SETS子句

使用GROUPING SETS子句可以只返回小计记录。

SQL> select division_id,job_id,sum(salary)
  2  from employees2
  3  group by grouping sets(division_id,job_id)
  4  order by division_id,job_id;

DIV JOB SUM(SALARY)
--- --- -----------
BUS         1610000
OPE         1320000
SAL         4936000
SUP         1015000
    ENG      245000
    MGR     6246000
    PRE      800000
    TEC      115000
    WOR     1475000

9 rows selected.

grouping Function相关推荐

  1. MySQL 8.0 Server层最新架构详解

    简介:本文基于MySQL 8.0.25源码进行分析和总结.这里MySQL Server层指的是MySQL的优化器.执行器部分.我们对MySQL的理解还建立在5.6和5.7版本的理解之上,更多的是对比P ...

  2. 庖丁解牛-图解MySQL 8.0优化器查询解析篇

    简介:本文重点介绍了优化器的基于规则的其中一部分优化,更多的偏重于SQL中的基本操作符 一  背景和架构 我们都知道,利用编写程序来动态实现我们应用所需要的逻辑,从而程序执行时得到我们需要的结果.那么 ...

  3. php 邮箱管理软件,Tmail 一个非专业的强大的小型邮件管理工具,用来读取 列表数据发送大量的 ,简单 WEB(ASP,PHP,...) 256万源代码下载- www.pudn.com...

    文件名称: Tmail下载  收藏√  [ 5  4  3  2  1 ] 开发工具: PHP 文件大小: 1161 KB 上传时间: 2016-05-09 下载次数: 0 详细说明:一个非专业的强大 ...

  4. Apache Solr7.4 入门教程

    由于最近公司业务可能需要用到solr所以花了几天时间研究了一下,发现solr在网上的教程没有太好的入门文章,要么文章所写的solr版本太老,要么就是介绍的内容不够完整.所有我打算写一篇有完整使用流程的 ...

  5. [1-2] Dependence-Aware Service Function Chain Design and Mapping

    文献名称:Dependence-Aware Service Function Chain Design and Mapping 文献类型(期刊.硕论.博论):会议:Globecom 发表年份:2017 ...

  6. azure 入门_Azure Function应用程序入门

    azure 入门 In this article, I am going to explain how to get started with Azure Function Apps. In my p ...

  7. 深入理解JavaScript内部原理(5): function

    本文是翻译http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#introduction 概要 In this article we w ...

  8. 【Ext.Net学习笔记】05:Ext.Net GridPanel的用法(包含Filter、Sorter、Grouping、汇总(Summary)的用法)...

    GridPanel是用来显示数据的表格,与ASP.NET中的GridView类似. GridPanel用法 直接看代码: <ext:GridPanel runat="server&qu ...

  9. 【cookbook pandas】学习笔记 chapter9 grouping,aggregation,filtration,and transformation

    unleash its powers 释放它的力量 introduction # perform the simplest aggregation involving only a single gr ...

最新文章

  1. mysql trim前后空格_MySQL清除字符串首尾空格函数trim
  2. Coursera吴恩达《神经网络与深度学习》课程笔记(4)-- 浅层神经网络
  3. hibernate映射-单向n-n关联关系
  4. 拼多多砍价小程序源码 流量主系列
  5. 使用openvswitch网桥连接不同的network namespace
  6. 西门子em235模块的功能_图文讲解PLC模拟量模块与传感器接线方法和注意事项
  7. (转)petshop4.0中的Profile理解(匿名用户身份)
  8. 网站日志分析工具:WebLog Expert Lite
  9. SpringBoot学习之文件结构和配置文件
  10. DropDownList的项按字母顺序排列
  11. Ubuntu 切换中文目录为英文目录
  12. Sql server2005 char/varchar/text和nchar/nvarchar/ntest的区别
  13. android打飞机游戏、MVP句子迷App、悬浮窗、RxJava+Retrofit、加载动画、定制计划App等源码...
  14. java 根据拼音查询汉字_java根据拼音搜索,但数据库为汉字的解决方案
  15. php开发视频直播平台技术,视频直播网站开发千万不能忘的一个知识点
  16. 副族元素从上到下原子半径_原子的大小以原子半径来表示
  17. mysql数据库递归访问数据
  18. java毕业设计学生考勤系统Mybatis+系统+数据库+调试部署
  19. Practical Pigment Mixing for Digital Painting文献简单翻译
  20. C语言题目——扫雷小游戏

热门文章

  1. MySQL—视图(一)
  2. Kali 渗透测试—Metasploit
  3. 【攻防世界017】re4-unvm-me
  4. 关于Uncaught SyntaxError: Unexpected identifier
  5. 152. 城市游戏【单调栈】
  6. 1088 Rational Arithmetic (20 分)【难度: 简单 / 知识点: 模拟】
  7. 【PAT乙级】1075 链表元素分类 (25 分)
  8. 3.1.3 覆盖与交换
  9. Zookeeper的Windows安装
  10. Redis的List操作