SQL:Group Functions,GROUP BY,HAVING

GROUP FUNCTIONS operate on sets of rows to give one result per group.

Types:
- AVG
- COUNT
- MAX
- MIN
- STDDEV:标准偏差
- SUM
- VARIANCE:方差[@more@]Syntax:
SELECT [column,] group_function(column),..
FROM table
[WHERE condition]
[GROUP BY column]
[HAVING group_condion]
[ORDER BY column];


1. You can use AVG and SUM for numeric data.

2. You can use MIN and MAX for any data type.

3. COUNT(*) returns the number of rows in a table.

4. Using the DISTINCT Keyword: COUNT(DISTINCT expr) returns the number of distinct non-null values of the expr.

5. In Oracle,Group function ignore null values in the column.【在Oracle环境中,在实现聚合函数处理时,聚合函数其缺省情况它会忽略NULL值。如:COUNT(expr) returns the number of rows with non-null values for the expr.】
eg:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select avg(deptno) from emp;

AVG(DEPTNO)
-----------
22.1428571

SQL> select avg(distinct deptno) from emp;

AVG(DISTINCTDEPTNO)
-------------------
20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6. Using the NVL Function with Group Function:
The NVL function forces group functions to include null values.
eg:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select sum(mgr) from emp;

SUM(MGR)
----------
100611

SQL> select sum(nvl(mgr,100000000)) from emp;

SUM(NVL(MGR,100000000))
-----------------------
100100611
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

7. Group Function can be nested.
eg:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select sum(avg(empno)) from emp group by deptno

SUM(AVG(EMPNO))
---------------
23237.8667

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

8. Group Functions' location is in SELECT list and HAVING clause.

-------------------------------------------------------------------------------------------------------------------------
Creating Groups of Data : The GROUP BY Clause Syntax
- Divide rows in a table into smaller groups by using the GROUP BY clause.
eg:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select sum(empno) from emp group by deptno;

SUM(EMPNO)
----------
23555
38501
46116
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.
- The GROUP BY column does not have to be in the SELECT list.
eg:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select ename,sum(empno) from emp group by deptno;
select ename,sum(empno) from emp group by deptno
*
第1行出现错误:
ORA-00979: 不是 GROUP BY 表达式


SQL> select ename,sum(empno) from emp group by deptno,ename;

ENAME SUM(EMPNO)
---------- ----------
KING 7839
CLARK 7782
MILLER 7934
FORD 7902
ADAMS 7876
JONES 7566
SCOTT 7788
SMITH 7369
WARD 7521
ALLEN 7499
BLAKE 7698
JAMES 7900
MARTIN 7654
TURNER 7844

已选择14行

SQL> select ename,sum(empno),deptno from emp group by deptno,ename;

ENAME SUM(EMPNO) DEPTNO
---------- ---------- ----------
KING 7839 10
CLARK 7782 10
MILLER 7934 10
FORD 7902 20
ADAMS 7876 20
JONES 7566 20
SCOTT 7788 20
SMITH 7369 20
WARD 7521 30
ALLEN 7499 30
BLAKE 7698 30
JAMES 7900 30
MARTIN 7654 30
TURNER 7844 30

已选择14行

SQL> select ename,sum(empno),deptno from emp group by ename,deptno;

ENAME SUM(EMPNO) DEPTNO
---------- ---------- ----------
FORD 7902 20
KING 7839 10
WARD 7521 30
ADAMS 7876 20
ALLEN 7499 30
BLAKE 7698 30
CLARK 7782 10
JAMES 7900 30
JONES 7566 20
SCOTT 7788 20
SMITH 7369 20
MARTIN 7654 30
MILLER 7934 10
TURNER 7844 30

已选择14行
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-------------------------------------------------------------------------------------------------------------------------
Excluding Group Results: The HAVING Clause
Use the HAVING clause to restrict groups:
1. Rows are grouped.
2. The group function is applied.
3. Groups matching the HAVING clause are displayed.
eg:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select ename,mgr,deptno from emp group by deptno,ename,mgr having avg(empno)>7700

ENAME MGR DEPTNO
---------- ---------- ----------
KING 10
CLARK 7839 10
MILLER 7782 10
FORD 7566 20
ADAMS 7788 20
SCOTT 7566 20
JAMES 7698 30
TURNER 7698 30

已选择8行
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-------------------------------------------------------------------------------------------------------------------------
Illegal (非法的) Queries Using Group Functions:
- You cannot use the WHERE clause to restrict (约定、限制) groups.
- You use the HAVING clause to restrict groups.
- You cannot use group functions in the WHERE clause.
eg:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> select ename,mgr from emp where avg(mgr>7650;
select ename,mgr from emp where avg(mgr>7650
*
第1行出现错误:
ORA-00934: 此处不允许使用分组函数


SQL> select ename,mgr from emp where mgr>(select avg(mgr) from emp);

ENAME MGR
---------- ----------
SMITH 7902
JONES 7839
BLAKE 7839
CLARK 7839
ADAMS 7788
MILLER 7782

已选择6行

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

转载于:http://blog.itpub.net/8633028/viewspace-907783/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值