• Oracle——分组函数


    AVG(平均值)和 SUM (合计)函数

    • 可以对数值型数据使用AVG 和 SUM 函数。
    • AVG组函数忽略空值
    --在组函数中使用NVL函数
    --求平均值
    sum(expr)/count(nvl(expr,0))

    MIN(最小值)和 MAX(最大值)函数

    • 可以对任意数据类型的数据使用 MIN 和 MAX 函数。
    SELECT MIN(hire_date), MAX(hire_date)
    FROM employees;

    COUNT(*) 返回表中记录总数,适用于任意数据类型。

    SELECT COUNT(*)
    FROM    employees
    WHERE department_id = 50;
    • COUNT(expr) 返回expr不为空的记录总数。
    • COUNT(DISTINCT expr)返回expr非空且不重复的记录总数
    SELECT COUNT(DISTINCT department_id)
    FROM employees;

    Group by

    可以使用GROUP BY子句将表中的数据分成若干组

    、WHERE一定放在FROM后面

    select department_id ,avg(salary)
    from employees
    --where department_id in(20,30,40)
    group by department_id

    ②、在SELECT 列表中所有未包含在组函数中的列都应该包含在 GROUP BY 子句中。

    SELECT department_id, AVG(salary)
    FROM employees
    GROUP BY department_id ;

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

    SELECT AVG(salary)
    FROM employees
    GROUP BY department_id ;

      ④、使用多个列分组

    select department_id ,job_id,avg(salary)
    from employees
    group by department_id,job_id

       ⑤、可以在 HAVING 子句中使用组函数

    SELECT department_id, AVG(salary)
    FROM employees
    HAVING AVG(salary) > 8000
    GROUP BY department_id;

    注意:不能在 WHERE 子句中使用组函数。

      ⑥、可以在order by 子句中使用组函数

    select department_id,avg(salary)
    from employees
    having avg(salary)>6000
    group by department_id
    order by avg(salary) 
  • 相关阅读:
    [LeetCode] 25.Reverse Nodes in k-Group-2
    [LeetCode] 25.Reverse Nodes in k-Group[UNSOLVED]
    [Leetcode] 24. Swap Nodes in Pairs
    [ProcessTree]How to list process tree / 如何显示进程树
    [Windows事件管理器]安全审核的文档
    [vdebench]文件系统的联机测试
    [Windows]多网卡配置网卡优先级
    [Ubuntu]开机紫屏的解决方法
    [USDA]查询食物热量和微量元素
    [Python]使用timer测量代码的执行速度
  • 原文地址:https://www.cnblogs.com/realshijing/p/8289639.html
Copyright © 2020-2023  润新知