Home > Database > Mysql Tutorial > SQLServer之Compute/ComputeBy实现数据汇总

SQLServer之Compute/ComputeBy实现数据汇总

WBOY
Release: 2016-06-07 15:14:51
Original
1956 people have browsed it

创建一个表Material用于测试 CREATE TABLE material( pid INT PRIMARY KEY IDENTITY(1,1), name VARCHAR(100), color VARCHAR(50), num INT ); 插入些许测试: INSERT INTO material(name,color,num) SELECT 'ball','red',12 UNION ALL SELECT 'ball','red',

创建一个表Material用于测试

CREATE TABLE material(
pid INT PRIMARY KEY IDENTITY(1,1),
name VARCHAR(100),
color VARCHAR(50),
num INT
);

插入些许测试值:

INSERT INTO material(name,color,num)
SELECT 'ball','red',12
UNION ALL
SELECT 'ball','red',24
UNION ALL
SELECT 'cat','yellow',13
UNION ALL
SELECT 'ball','yellow',13
UNION ALL
SELECT 'dog','yellow',13
UNION ALL
SELECT 'cat','black',4
UNION ALL
SELECT 'cat','white',5

(1)Compute


SELECT NAME,color,num
FROM material
ORDER BY NAME,color
COMPUTE SUM(num),MAX(num)

结果:




(2)Compute By

SELECT NAME,color,num
FROM material
ORDER BY NAME,color
COMPUTE SUM(num),MAX(num) BY NAME,color

结果:


总结:

GROUP BY 生成单个结果集。每个组都有一个只包含分组依据列和显示该组子聚合的聚合函数的行。选择列表只能包含分组依据列和聚合函数。


COMPUTE 生成多个结果集。一类结果集包含每个组的明细行,其中包含选择列表中的表达式。另一类结果集包含组的子聚合,或 SELECT 语句的总聚合。选择列表可包含除分组依据列或聚合函数之外的其它表达式。聚合函数在 COMPUTE 子句中指定,而不是在选择列表中。

说明 在 COMPUTE 或 COMPUTE BY 子句中,不能包含 ntext、text 或 image 数据类型。


Related labels:
source:php.cn
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