惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
V
Vulnerabilities – Threatpost
T
Tenable Blog
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Visual Studio Blog
WordPress大学
WordPress大学
Latest news
Latest news
K
Kaspersky official blog
T
Tailwind CSS Blog
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
C
Cisco Blogs
博客园 - 聂微东
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
小众软件
小众软件
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
Security Latest
Security Latest
Y
Y Combinator Blog
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
月光博客
月光博客
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
S
Security Affairs
P
Proofpoint News Feed
D
DataBreaches.Net
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG

博客园 - music000

分享:使用JQuery进行跨域请求 Flash Chart 谨防 url 传递参数未编码(转码)产生的陷阱 OSI七层网络模型与TCP/IP四层网络模型 一些数据库理论知识 数据查询的另类需求 A Preview of HTML 5 CSS Sprites 15 Rules for Faster-Loading Web Sites 关于"多级目录(分类)"的一些想法 ----- 实现方法 金额转换:阿拉伯数字转中文(SQL存储过程) 金额转换:阿拉伯数字转中文(javascript) 这两天不爽——公车上被误认为色狼、游泳撞破上嘴唇 如何获取字段中分隔符的个数?(sql语句) 关于GridView导出Excel的一些问题(采用Ajax出现的的问题及解决方法) - music000 - 博客园 固定表头 Bubble in JavaScript DOM Reg-日期 安装 WebDesigner 之后,ASPNET 帐户没有对 IIS 的访问权。
Sqlserver中Compute By子句用法分析
music000 · 2008-01-29 · via 博客园 - music000

使用Compute by子句和行统计函数(count,sum,max,avg,min等),可以统计排序中结果完全相同的列,统计值作为查询结果以附加行的形式显示,

语法:Compute avg|count|max|min|sum by 表达式

1、举一个例子

比如有一个数据表:表名为person,三个字段分别为 name,age,country

select name,country,age from person where ?? order by a compute sum(age),max(age),min(age) by country

这个语句在查询时将数据按照country分组,然后分别显示每组的详细信息和统计信息。结果可能如下:

name                  country                    age

张三                  中国                        16
李四                  中国                        21
王五                  中国                        24

sum      max      min

61            24        16

=================================

name                  country                        age

泰森                     美国                         20
布什                     美国                         24
盖茨                     美国                         25

sum      max      min

69           25        20

=============================================

name                  country                        age

妓子                  日本                           12
完犊子                日本                           14
猪生逆子              日本                           15

sum      max      min

41          15        12

我们很容易看出她其实是在一个查询结果中包含了三个子查询(根据country不同而分的组),每个子查询又包含两个子查询(一个是详细信息,一个是统计信息)

2、compute by 子句的规则:

(1)不能将distinct与行统计函数一起使用

(2)compute ??? by 子句中 ???出的列必须出现在选择列表中

(3)不能在含有compute by 子句的语句中使用select into 子句,因为包括compute 子句的语句会产生不规则的行。

(4)如果使用了compute by子句,则必须使用order by 子句, 而且compute by子句中的列必须包含在order by 子句中,并且对列的前后顺序和起始项都要一致(说白了compute by子句中的列必须是order by子句中列表的全部,或者前边的连续几个)。

(5)如果compute 省略了 by ,则order by 也可以省略

(6)如果compute by 子句包含多列时,会将一个组(第一个列分的组)分成若干个子组(利用后面的列),并对每层子组进行统计。

(7)使用多个compute by子句时,会分别按不同的组统计出结果。详细信息还是按照正常的第一个分组方式显示。

(8)compute by 子句中可以使用多个统计函数,他们互不影响

(9)compute by 子句中可以不包含by ,而只用compute  此时不对前面信息分组,而只对全部信息进行统计。