zoukankan      html  css  js  c++  java
  • SqlServer--分组查询和统计

    以下是学习笔记:

    1,分组统计

     

     2,分组后筛选统计

    select 班级=StudentClass.ClassName,人数=COUNT(*),C#最高分=Max(CSharp),DB最高分=MAX(SQLServerDB),
    AVG(CSharp) as C#平均分,AVG(SQLServerDB) as DB平均分
    from Students
    inner Join StudentClass on Students.ClassId =StudentClass.ClassId
    inner join ScoreList on ScoreList.StudentId=Students.StudentId
    group by ClassName --ClassName 按照班级的名称分组
    
    
    select 班级=StudentClass.ClassName,人数=COUNT(*),C#最高分=Max(CSharp),DB最高分=MAX(SQLServerDB),
    AVG(CSharp) as C#平均分,AVG(SQLServerDB) as DB平均分
    from Students
    inner Join StudentClass on Students.ClassId =StudentClass.ClassId
    inner join ScoreList on ScoreList.StudentId=Students.StudentId
    group by ClassName --ClassName 按照班级的名称分组
    having AVG(CSharp)>=70 and AVG(SQLServerDB)>=70 --分组统计后不能有where条件筛选,要用having
    

     

     3,重复数据问题

    --在知道那个字段重复的情况
    select StudentId from ScoreList  group by StudentId having COUNT(*)>1
    
    --查询所有重复的记录
    select * from ScoreList
    where StudentId in(select StudentId from ScoreList  group by StudentId having COUNT(*)>1)
    order by StudentId
    
    --其他方法
    select * from ScoreList
    where (select COUNT(*) from ScoreList  s where s.StudentId=ScoreList.StudentId)>1
    order by StudentId
    
    --过滤掉重复数据
    select distinct StudentId,CSharp from ScoreList
    
    select distinct StudentId,CSharp,SQLServerDB from ScoreList
    

      

     

  • 相关阅读:
    elasticsearch api
    kaili camera
    mysql create db utf8 character
    npm run-script
    d-link kvm 关闭声音
    setInterval js
    jpa datasource config
    mvn添加本地jar
    Sublime Text 2 中文包
    初遇ping++
  • 原文地址:https://www.cnblogs.com/baozi789654/p/13910735.html
Copyright © 2011-2022 走看看