zoukankan      html  css  js  c++  java
  • sql 聚合函数

    聚合函数,根据输入的多行数据返回一个结果。只能出现在select语句,ORDER BY语句、having子句中。

    1.COUNT()

    该函数返回记录集合中所有非NULL值得总和。

    2.AVG()

    返回所有非NULL值得平均值。

    可以用AVG(DISTINCT xxxx)排除平均值计算的重复值

    3.MAX()和MIN()

    返回最大值或者最小值,用于任何数据类型。日期的最大值是最迟的日期,最小值是最早的日期。

    4.STDDEV()

    标准差函数

    5.SUM(X)

    返回X中的所有值的和

    6.VARIANCE()

    方差函数

    COUNT()是数个数、SUM() 是求和。

    例题【转】:

     有员工表empinfo 

    Fempno varchar2(10) not null pk, 
    Fempname varchar2(20) not null, 
    Fage number not null, 
    Fsalary number not null 
    ); 
    假如数据量很大约1000万条;写一个你认为最高效的SQL,用一个SQL计算以下四种人: 
    fsalary>9999 and fage > 35 
    fsalary>9999 and fage < 35 
    fsalary <9999 and fage > 35 
    fsalary <9999 and fage < 35 
    每种员工的数量; 
    select sum(case when fsalary > 9999 and fage > 35
    then 1
    else 0end) as "fsalary>9999_fage>35",
    sum(case when fsalary > 9999 and fage < 35
    then 1
    else 0
    end) as "fsalary>9999_fage<35",
    sum(case when fsalary < 9999 and fage > 35
    then 1
    else 0
    end) as "fsalary<9999_fage>35",
    sum(case when fsalary < 9999 and fage < 35
    then 1
    else 0
    end) as "fsalary<9999_fage<35"
    from empinfo;v

  • 相关阅读:
    Hbuilder——报错The keyword 'export' is reserved
    控制器里路径变量的使用
    Spring 控制器重定向
    Spring A 标签链接使用
    Spring switch的使用
    thymeleaf如何遍历数据 each循环的使用
    spring 机制 扫描包
    Spring分层次建包
    什么是MVC模型
    如何运行spring boot 工程
  • 原文地址:https://www.cnblogs.com/wangyameng/p/3904552.html
Copyright © 2011-2022 走看看