ITPub博客

首页 > 数据库 > > IT日志之SqlServer数据库:查询语句2

IT日志之SqlServer数据库:查询语句2

作者:AI中国 时间:2023-02-22 16:33:01 0 删除 编辑
  • 数据分组

分组一般和聚合函数一起使用。对每一个组汇总统计。

    --请从学生表中查询出每个班级的班级ID和班级人数


    select tClassId, 班级人数=COUNT(*) from TblStudent group by tClassId



      --统计出班级中男同学和女同学的人数分别是多少


      select tClassId,tSGender,人数=count(tSGender) from TblStudent group by tClassId,tSGender



        --使用了groupby之后查询的结果只能包含group by的字段,如果想要有其他字段则要用聚合函数


        select sum(tSAge) as 年龄,tClassId,tSGender,人数=count(tSGender) from TblStudent group by tClassId,tSGender



        • having

        对分组之后的数据筛选

          --请从学生表中查询出每个班级的班级ID和班级人数,并筛选出人数超过1的班级


          select tClassId, 班级人数=COUNT(*) from tblstudent group by tClassId having count(*)>1



          where后面不能接聚合函数

          执行顺序:1.from  2.where条件 3.group by 4.having 5.select> distinct > top

           6.order by

          本文来源https://www.modb.pro/db/99212

          来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31560284/viewspace-3124304/ ,如需转载,请注明出处,否则将追究法律责任。

          上一篇: GreenPlum踩坑
          下一篇: R:BASE介绍
          请登录后发表评论 登录
          全部评论

          注册时间:2019-12-23

          • 博文量
            212
          • 访问量
            2471