SQL Server 2008中的cube运算符

Cube 运算符

Cube 运算符与 Group By 子句一起使用,以返回结果中组或者子组的所有可能组合。 它以聚合形式呈现数据。

首先我们创建一个名为emp的表

create table Stocks

(

Apparel varchar(15),

Store varchar(15),

Number int

)

插入一些数据

insert into Stocks

select 'Shirt', 'Bindals' ,25 union all

select 'Trousers','Bindals', 30 union all

select 'Jeans', 'Labhjis' ,15 union all

select 'Shirt', 'Labhjis', 25 union all

select 'Jeans', 'Labhjis', 34

使用Cube 运算符:

现在,如果我们想快速查看各种服装库存,请输入以下代码以查看服装和商店的所有可能组合:

select Apparel,Store, SUM(Number) AS Quantity

FROM Stocks

GROUP BY Apparel, Store WITH CUBE
日期:2020-06-02 22:17:44 来源:oir作者:oir