zoukankan      html  css  js  c++  java
  • 数据库中的聚合函数

    常用的聚合函数

     1  count    2   sum    3   avg    4   max    5   min 

    使用规范时 默认的是all   

    distinct   指定所有的唯一非空值行

    count  (行的数目)

    select count (  列或者*)  from  表

    sum  (总和)

    select  sum(计数规范)from

    分组  (group by)

    select  列a  , 聚合函数 from 表      where 过滤条件      group  by  列a (以列a来分组)

    过滤聚合函数(having)

     select  列a  , 聚合函数 from 表      where 过滤条件      group  by  列a    having  聚合函数过滤条件 

    要注意的是  SQL语句的执行顺序

     (5)select  列a  , 聚合函数  (1) from 表    (2)  where 过滤条件    (3)  group by  列a  (4)  having   聚合函数过滤条件  (6) order   by

    课后练习

    select * from nobel

    -- 1获奖总人数
    select count (distinct winner) from nobel

    -- 2 诺贝尔 物理奖的获奖总次数
    select count(subject)from nobel where subject='Physics'

    -- 3 显示每个奖项的获奖总次数
    select subject,count(subject)from nobel group by subject

    -- 4显示每个奖项第一次获得的年份
    select min(yr) 年份,subject 奖项 from nobel group by subject

    -- 5 显示每个奖项在2000年来获得的人数
    select subject 奖项,yr 年份 from nobel where yr=2000 group by subject ,yr

    -- 6 显示每个奖项不同获奖者的人数
    select subject 奖项,count (distinct winner)from nobel group by subject

    -- 7  显示每个奖项 有多少年获奖
    select subject 奖项,count (distinct yr)from nobel group by subject

    -- 8 显示当年有三个Physic的年份
    select subject 奖项,yr 年份 from nobel where  subject='Physics' group by yr, subject having count(winner)=3

    -- 9 得奖大于1的winner
    select winner 获奖者 from nobel group by winner having count(subject)>1

    --10 得到多个奖项的winner
    select winner 获奖者 from nobel group by winner having count(distinct subject)>1

  • 相关阅读:
    BZOJ2705[SDOi2012]Longge的问题
    Ubuntu 18.04 打不开1.1.0版本网易云音乐的解决方法汇总
    BZOJ3295[CQOI2011]动态逆序对(CDQ分治)
    hdu-4638-Group(树状数组)
    hdu-3333-Turing Tree(树状数组)
    UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]
    ZOJ-3597-Hit the Target!(线段树+扫描线)
    POJ-1177-Picture(线段树+扫描线+离散化)[矩形周长并]
    POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
    LightOJ 1135(线段树)
  • 原文地址:https://www.cnblogs.com/shizhijie/p/7778273.html
Copyright © 2011-2022 走看看