首页 > 代码库 > 【练习】组函数

【练习】组函数

1.可以对数值类型使用min,max,avg,sum:

SQL> select avg(salary),max(salary),min(salary),sum(salary)
  2  from employees
  3  where job_id like %REP%;

AVG(SALARY) MAX(SALARY) MIN(SALARY) SUM(SALARY)
----------- ----------- ----------- -----------
 8272.72727       11500        6000      273000

2.可以对数值型,日期型,字符型使用min,max函数:

SQL> select min(hire_date),max(hire_date)
  2  from employees;

MIN(HIRE_ MAX(HIRE_
--------- ---------
13-JAN-01 21-APR-08

3.使用count函数
(1)count(*)返回表中行数:

SQL> select count(*) 
  2  from employees
  3  where department_id=50;

  COUNT(*)
----------
        45

(2)COUNT(expr) 返回非空值的 expr 的行数:

SQL> select count(commission_pct)
  2  from employees
  3  where department_id=80;

COUNT(COMMISSION_PCT)
---------------------
                   34

(3)COUNT(DISTINCT expr) 返回 expr 非空且不重复的记录数。

SQL> select count(distinct department_id)
  2  from employees;

COUNT(DISTINCTDEPARTMENT_ID)
----------------------------
                          11

(4)组函数忽略空值,NVL函数使分组函数无法忽略空值:

SQL> select avg(commission_pct)
  2  from employees;

AVG(COMMISSION_PCT)
-------------------
         .222857143

SQL> select avg(nvl(commission_pct,0))
  2  from employees;

AVG(NVL(COMMISSION_PCT,0))
--------------------------
                .072897196

(5)group by 字句:在SELECT 列表中所有未包含在组函数中的列都应该包含在GROUP BY 子句中。

SQL> select department_id,avg(salary)
  2  from employees
  3  group by department_id;

DEPARTMENT_ID AVG(SALARY)
------------- -----------
          100  8601.33333
           30        4150
                     7000
           20        9500
           70       10000
           90  19333.3333
          110       10154
           50  3475.55556
           40        6500
           80  8955.88235
           10        4400

DEPARTMENT_ID AVG(SALARY)
------------- -----------
           60        5760

12 rows selected.

包含在 GROUP BY 子句中的列不必包含在SELECT 列表中:

SQL> select avg(salary)
  2  from employees
  3  group by department_id;

AVG(SALARY)
-----------
 8601.33333
       4150
       7000
       9500
      10000
 19333.3333
      10154
 3475.55556
       6500
 8955.88235
       4400

AVG(SALARY)
-----------
       5760

12 rows selected.

 在 GROUP BY 子句中包含多个列:

SQL> select department_id dept_id,job_id,sum(salary)
  2  from employees
  3  group by department_id,job_id
  4  order by department_id;

   DEPT_ID JOB_ID     SUM(SALARY)
---------- ---------- -----------
        10 AD_ASST           4400
        20 MK_MAN           13000
        20 MK_REP            6000
        30 PU_CLERK         13900
        30 PU_MAN           11000
        40 HR_REP            6500
        50 SH_CLERK         64300
        50 ST_CLERK         55700
        50 ST_MAN           36400
        60 IT_PROG          28800
        70 PR_REP           10000

   DEPT_ID JOB_ID     SUM(SALARY)
---------- ---------- -----------
        80 SA_MAN           61000
        80 SA_REP          243500
        90 AD_PRES          24000
        90 AD_VP            34000
       100 FI_ACCOUNT       39600
       100 FI_MGR           12008
       110 AC_ACCOUNT        8300
       110 AC_MGR           12008
           SA_REP            7000

20 rows selected.

SQL> 

 

【练习】组函数