Home > Database > Mysql Tutorial > GroupbyALL

GroupbyALL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:03:29
Original
2036 people have browsed it

Group by ALL 。和不加ALL差别就是加了ALL后包含所有组和结果集,甚至包含那些其中任何行都不满足 WHERE 子句指定的搜索条件的组和结果集。注意的是指定了 ALL,将对组中不满足搜索条件的汇总列返回空值。declare @t table(id int,col char(2))insert @t sel

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'
--1
select id,COUNT(1)
from @t
group by id
--2
select id,COUNT(1)
from @t
where id < 3
group by id
针对上面的数据我们可以得到显而意见的结果 :
/*
id         
----------- -----------
1           2
2           1
3           2
4           1
5           3
(5 行受影响)
id         
----------- -----------
1           2
2           1
(2 行受影响)
*/
   那么如果我们想要得到如下结果呢?
/*
id         
----------- -----------
1           2
2           1
3           0
4           0
5           0
*/
--显然大家一看就知道我的意思了吧,常规有人一定会union联合、子查询什么的吧,有没有想过其实可以简单点呀,看看下面的语法:
--3
select id,COUNT(1)
from @t
where id < 3
group by all id
    结果大家自己运行一下就有答案了。
最后后续版本的 Microsoft SQL Server 将删除该功能。请避免在新的开发工作中使用该功能,如果以后的项目里有使用的话还是着手修改当前还在使用该功能的应用程序吧。另外还得注意三点:1、CUBE 或 ROLLUP 运算符不能和ALL同时使用。2、如果在访问远程表的查询中还有 WHERE 子句,则该查询不支持 GROUP BY ALL。3、对于具有 FILESTREAM 属性的列,GROUP BY ALL将不被支持。
Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template