zoukankan      html  css  js  c++  java
  • 数据库 --- Group by的使用心得

    Group By 从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理。

    1. 从表中筛选出那些重复的数据 :

    select * from table group by name having count(*) >1;

    2. 写SQl语句根据名字(NAME)相同按年龄(AGE)分组得到不同年龄的人的平均工资,并写出结果:

    select avg(salary) from staff group by name, age;

    3. 查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:
     
    select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A

    4. 查询表A中存在ID重复三次以上的记录,完整的查询语句如下:
     
    select * from(select count(ID) as count from table group by ID)T where T.count>3

    5. 查询各个职位员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位:

    select job, count(*) from emp where sal > (select avg(sal) from emp) group by job;

    6. 查询每个部门的最高工资:

    select deptno, max(sal) maxsal from emp group by deptno order by deptno;

    ------------------------------------ 未完待续 --------------------------------------



  • 相关阅读:
    矩阵树定理 / 生成树计数
    NC20811 蓝魔法师 (树形DP/树上01背包)
    Xor-MST学习/ 2020牛客暑假多校(五)B.Graph
    HDU-6820 Tree (2020杭电多校(五) 1007)
    Flipping Coins (概率DP)
    宝石装箱 容斥+dp
    Rabbit的工作(1) (dP)
    Codeforces-1350 E.Orac and Game of Life
    HDU-6563 Strength (贪心)
    HDU-6558 The Moon (期望DP)
  • 原文地址:https://www.cnblogs.com/Xbingbing/p/8563010.html
Copyright © 2011-2022 走看看