SQL Server 基础 04 函数与分组查询数据

                 

                                                       函数与分组查询数据

 

系统函数分

聚合函数、数据类型转换函数、日期函数、数学函数 . . .    

 

1、 聚合函数

           主要是对一组值进行计算,然后返回一个值。 聚合函数包括 sum(求和)、avg(求平均值)、min()、max()、count(求数量)

sum函数语法  :  sum(列明)

select sum(sno) as 序号和 from stu_info 

avg

select avg(sno) as 序号平均值 from stu_info

. . .

count 用来一组值的个数,统计

select count(sno) as 个数 from stu_info 

 

2、类型转换函数

           将日期和数字转换成字符串格式, ...   用到 convert() 、cast()    

           convert 适合用于日期的转换,cast适合用于一般数据类型的转换。。。

   convert 的语法格式 :  convert ( datatype [ (length) , expression , [style] ] )

                                 datatype 如果要转换成 char、archar 、binary 、arbinary 数据类型,还需要设置数据类型长度  

                                 expression : 表达式,进行数据类型转换的值或者列名

                                 style         : 用于日期格式的设置

                                 

1 -- 类型转换
2  --语法convert(datatype[(length),expression,[style]])
3  select convert(char,getdate())
4  select sname,convert(char,date,103) as 出生日期 from stu_info

 

 cast : 语法格式 :  cast (expression as datatype [ (length) ] ) 

 select sname,cast(date as char(10)) as 生日 from stu_info

 

 

3、日期函数  

          getdate、dateadd、datediff、datename、datepart、

          

4、数学函数

5、字符函数

6、其它几个系统函数

               文本和图形函数、配置函数、游标函数、元数据函数、安全函数、常用的系统函数--(isnull)

 

 

 

                                                       分组查询

 

数据分组是指将数据表按照某种值分为很多组。例如,性别里分男女,会得到两组。

数据分组使用 group by , 如果把满足某种条件的的分组查询出来,还需要 having 子句配合

     /**/前面说需要distinct关键字 去除重复值,会影响效率,为此以后使用 group by 。

 

1、按列分组

        1 -- 将表内容按列分组 2

select depart from stu_info group by depart      

/***/   如果使用*通配符  就会出错,应当列出所有表的字段名,按 group by 后面的字段分组

 

2、聚合函数与分组配合使用

 1 select sex,count(*) as 人数 from dbo.stu_info group by sex 

      /**/   由于句中含有 group by 子句 , 所以count(*) 统计的是每组的记录个数,而并非是所有记录的个数 

  

1  -- where 子句先于 group by
2  select depart,count(*) as 女生人数 from stu_info where sex=''
3  group by depart

 

3、查询数据直方图 ,  关键字 : replicate    

1 select depart,replicate('=',count(*)*5) as 人数对比图 from dbo.stu_info group by depart

  /**/   该函数的作用是以指定的次数重复字符表达式 ,这里以 5 倍为次数 , 重复了符号 ( = )

 

4、排序分组结果   order by 语句永远放在最后

1  select depart ,count(*) from dbo.stu_info group by depart order by count(*) desc

   /**/  先分组,然后按组值数量进行排序  . . .

 

5、反转查询结果      关键字 : case when

1 select depart,
2         count(case when sex='' then 1 else null end) as 男生人数
3         ,count(case when sex='' then 1 else null end) as 女生人数 
4       from dbo.stu_info group by depart  

 

 

6、使用 having 子句设置分组查询条件

1 select depart ,count(*) as 人数 from stu_info group by depart 
2        having depart in ('软件系','网页')
3        order by count(*)
4        -- 不过这里也可以使用where代替having

 

   

  /**/ having 与 where 的异同 

                                  having 子句用于筛选组,where 子句用于筛选记录

                                  having 子句可以使用聚合函数,where不能

                                  having 子句中不能出现既不被group by 子句包含,又不能聚合含的字段。而where含可以出现任意字段

                                  having 配合 group by 子句使用 , 而 where 不能配合任何子句使用

 

 

 

 

posted on 2014-05-03 21:44  知鸟  阅读(314)  评论(0编辑  收藏  举报