码迷,mamicode.com
首页 > 数据库 > 详细

T-SQL之SELECT语句的元素

时间:2019-09-26 23:00:45      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:rom   执行   group by   使用   元素   聚合   结果   tin   nbsp   

  select id,name,count(distinct name) as namecount from  table1 where  id=1  
  group  by id,name having count(distinct name) having  name=‘‘  order  by  id

如上述sql,在逻辑上按照下面的顺序进行执行:

1.from 2.where 3.group by 4.having  5.select  (5.1 over  5.2distinct  5.3top) 6.order by 

 

1.1from

  指定要查询的表名。

1.2  where

可以指定一个为此或逻辑表达式,从而过滤由from阶段返回 的行。

1.3 group by

这个阶段可以将前面查询到的数据按组进行组合。所以在select中后面使用到的属性,都要包括在group by后面,因为这个阶段是为了保证查询到的属性值作为一个组合是唯一的。

注意:所有的聚合函数都会忽略NULL值,只有count(*)除外,比如一列count1的值为10,20,NULL,10,10,60,那么count(*)=6,但是count(count1)=5.因为count1列只有5行已知值。还可以在聚合函数中指定distinct关键字。比如count( distinct count1)=4,sum(distinct count1)=90,avg( distinct count1)=90/3=30.

1.4having

having 关键字是在对行进行分组之后进行处理的,是指定对组进行过滤的谓词或逻辑表达式。

1.5 select

select用于指定需要在查询返回的结果集中包含的属性(列)。

 

T-SQL之SELECT语句的元素

标签:rom   执行   group by   使用   元素   聚合   结果   tin   nbsp   

原文地址:https://www.cnblogs.com/anjingdian/p/11594870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!