Summarizing Data with CUBE and ROLLUP

本文展示了如何使用SQL中的CUBE和ROLLUP命令来快速获取数据库数据的概览,通过实例分析了如何利用这些命令来汇总特定表格的数据,并提供了不同命令在输出结果上的区别。

Looking for a quick, efficient way to summarize the data stored in your database?  The SQL ROLLUP and CUBE commands offer a valuable tool for gaining some quick and dirty insight into your data.  ROLLUP and CUBE are SQL extensions and they're available in SQL Server 6.5 (and above) and Oracle 8i (and above).

The CUBE command is added to an SQL 

To provide an example, let's imagine a table that contains the number and type of pets available for sale at our chain of pet stores:

Pets

Type

Store

Number

Dog

Miami

12

Cat

Miami

18

Turtle

Tampa

4

Dog

Tampa

14

Cat

Naples

9

Dog

Naples

5

Turtle

Naples

1


As the proud owners of this Florida pet superstore, we'd like to take a quick look at various aspects of our inventory.  We could hire an SQL programmer to sit down and write a number of queries to retrieve the exact data that we're looking for.  However, our dataset isn't very large and we enjoy looking at the raw numbers.  Our hunger for data can be appeased using the CUBE command.  Here's the sample SQL:

SELECT Type, Store, SUM(Number) as Number
FROM Pets 
GROUP BY type,store 
WITH CUBE

And the results of the query:

Type

Store

Number

Cat

Miami

18

Cat

Naples

9

Cat

NULL

27

Dog

Miami

12

Dog

Naples

5

Dog

Tampa

14

Dog

NULL

31

Turtle

Naples

1

Turtle

Tampa

4

Turtle

NULL

5

NULL

NULL

63

NULL

Miami

30

NULL

Naples

15

NULL

Tampa

18

Wow!  That's a lot of data!  Notice that we are presented with a number of additional groupings that contain NULL fields that wouldn't appear in the results of a normal GROUP BY command.  These are the summarization rows added by the CUBE statement.  Analyzing the data, you'll notice that our chain has 27 cats, 31 dogs and 5 turtles spread among our three stores.  Our Miami store has the largest number of pets in stock with a whopping inventory of 30 pets.

We're not particularly interested in the total number of pets at each store -- we'd just like to know our statewide inventory of each species along with the standard GROUP BY data.  Utilizing the ROLLUP operator instead of the CUBE operator will eliminate the results that contain a NULL in the first column. 

Here's the SQL:

SELECT Type, Store, SUM(Number) as Number
FROM Pets 
GROUP BY type,store 
WITH ROLLUP

And the results:

Type

Store

Number

Cat

Miami

18

Cat

Naples

9

Cat

NULL

27

Dog

Miami

12

Dog

Naples

5

Dog

Tampa

14

Dog

NULL

31

Turtle

Naples

1

Turtle

Tampa

4

Turtle

NULL

5

NULL

NULL

63

And that's CUBE and ROLLUP in a nutshell!  Be sure to check back next week for another exciting journey into the world of databases!

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值