Group by ALL

来源:互联网 发布:地图软件下载 编辑:程序博客网 时间:2024/05/22 18:34
Group by ALL 。和不加ALL差别就是加了ALL后包含所有组和结果集,甚至包含那些其中任何行都不满足 WHERE 子句指定的搜索条件的组和结果集。注意的是指定了 ALL,将对组中不满足搜索条件的汇总列返回空值。declare @t table(id int,col char(2))insert @t select 1,'a'insert @t select 1,'a'insert @t select 2,'a'insert @t select 3,'a'insert @t select 3,'a'insert @t select 4,'a'insert @t select 5,'a'insert @t select 5,'a'insert @t select 5,'a'--1select id,COUNT(1)from @tgroup by id--2select id,COUNT(1)from @twhere id < 3group by id针对上面的数据我们可以得到显而意见的结果 :/*id         ----------- -----------1           22           13           24           15           3(5 行受影响)id         ----------- -----------1           22           1(2 行受影响)*/   那么如果我们想要得到如下结果呢?/*id         ----------- -----------1           22           13           04           05           0*/--显然大家一看就知道我的意思了吧,常规有人一定会union联合、子查询什么的吧,有没有想过其实可以简单点呀,看看下面的语法:--3select id,COUNT(1)from @twhere id < 3group by all id    结果大家自己运行一下就有答案了。最后后续版本的 Microsoft SQL Server 将删除该功能。请避免在新的开发工作中使用该功能,如果以后的项目里有使用的话还是着手修改当前还在使用该功能的应用程序吧。另外还得注意三点:1、CUBE 或 ROLLUP 运算符不能和ALL同时使用。2、如果在访问远程表的查询中还有 WHERE 子句,则该查询不支持 GROUP BY ALL。3、对于具有 FILESTREAM 属性的列,GROUP BY ALL将不被支持。

0 0