PARTITION BY 和 group by
sum() over (PARTITION BY ...) 是一个分析函数。 他执行的效果跟普通的sum ...group by ...不一样,它计算组中表达式的累积和,而不是简单的和。
表a,内容如下:
B C D
02 02 1
02 03 2
02 04 3
02 05 4
02 01 5
02 06 6
02 07 7
02 03 5
02 02 12
02 01 2
02 01 23
select b,c,sum(d) e from a group by b,c
得到:
B C E
02 01 30
02 02 13
02 03 7
02 04 3
02 05 4
02 06 6
02 07 7
而使用分析函数得到的结果是:
SELECT b, c, d, SUM(d) OVER(PARTITION BY b,c ORDER BY d) e FROM a
B C E
02 01 2
02 01 7
02 01 30
02 02 1
02 02 13
02 03 2
02 03 7
02 04 3
02 05 4
02 06 6
02 07 7
结果不一样,这样看还不是很清楚,我们把d的内容也显示出来就更清楚了:
SELECT b, c, d,SUM(d) OVER(PARTITION BY b,c ORDER BY d) e FROM a
B C D E
02 01 2 2 d=2,sum(d)=2
02 01 5 7 d=5,sum(d)=7
02 01 23 30 d=23,sum(d)=30
02 02 1 1 c值不同,重新累计
02 02 12 13
02 03 2 2
02 03 5 7
02 04 3 3
02 05 4 4
02 06 6 6
02 07 7 7
但 我们可以利用partition by 找到一个表中 不同分组中最大和最小的一个值
select a.* from
(
select row_number() over(partition by nId order by a.inMemberCount asc) nRow,aa,bb from TableA
) a
where nRow=1
PARTITION BY 和 group by的更多相关文章
- over partition by与group by 的区别
(本文摘自scottpei的博客) over partition by与group by 的区别 今天看到一个老兄的问题, 大概如下: 查询出部门的最低工资的userid 号 表结构: D号 ...
- SQL Server - Partition by 和 Group by对比
参考:https://www.cnblogs.com/hello-yz/p/9962356.html —————————————————— 今天大概弄懂了partition by和group by的区 ...
- sqlserver中分区函数 partition by与 group by 区别 删除关键字段重复列
partition by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition by ...
- over partition by与group by
over partition by与group by 的区别 http://www.cnblogs.com/scottpei/archive/2012/02/16/2353718.html 今天看到一 ...
- partition by和group by对比
今天大概弄懂了partition by和group by的区别联系. 1. group by是分组函数,partition by是分析函数(然后像sum()等是聚合函数): 2. 在执行顺序上, 以下 ...
- SQL:over partition by与group by 的区别
group by是对检索结果的保留行进行单纯分组,一般总爱和聚合函数一块用例如AVG(),COUNT(),max(),main()等一块用. partition by虽然也具有分组功能,但同时也具有其 ...
- sql server partition分区与group by 分组
例子:在一个StudentScore表中,有序号ID,班级ClassId,学生姓名Name,性别Sex,语文成绩ChineseScore,数学成绩MathScore,平均成绩AverageScore等 ...
- MSSQL Server中partition by与group by的区别
在使用over等开窗函数时,over里头的分组及排序的执行晚于“where,group by,order by(但此排序顺序优先级是最高的)”的执行. ①group by 列名 合并(列值相同的并作一 ...
- SQL Server: Difference between PARTITION BY and GROUP BY
https://stackoverflow.com/questions/2404565/sql-server-difference-between-partition-by-and-group-by ...
随机推荐
- MC- 挂单STOP交易
using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; using ATCenterP ...
- 动态封杀与解封IP
不论IIS6还是IIS7 都可以把需要封杀的IP加入封杀列表.但是需要注意的是我们代码写的是全部替换原先的数据.但是在IIS7下,执行的效果是原先的不替换,新加一批封杀 IP.当然IIS7下,如果新加 ...
- [FBA]SharePoint 2013自定义Providers在基于表单的身份验证(Forms-Based-Authentication)中的应用
//http://tech.ddvip.com/2014-05/1401197453210723.html 由于项目的需要,登录SharePoint Application的用户将从一个统一平台中获取 ...
- Telnet服务器和域名系统的端口号 Mac OS X
找到Telnet服务器和域名系统的端口号: lapommedeMacBook-Pro:~ lapomme$ grep telnet /etc/services telnet /udp # Telnet ...
- OC和JS的交互---JavaScriptCore
JavaScriptCore的简单介绍 JavaScriptCore是iOS7提供的原生框架,可以让Objective-C和JavaScript提供简单直接的交互. JavaScriptCore的使用 ...
- CentOS7 emacs安装
首先安装依赖库 依赖库: yum install gcc* yum install glib* yum install gtk* yum install ncurses* yum ...
- JavaScript(5)——DOM
DOM操作 为了写这一篇随笔真的是费了好多力气,虽然还是写不好.本来是从周一都开始写的,但是周二周三忙着去帮忙招新了,哈哈哈.感觉做自己喜欢的事特别好玩,虽然挺忙的.看着那些小鲜肉,感觉自己真的老了啊 ...
- Windows Access Token
security descriptor A structure and associated data that contains the security information for a sec ...
- how to add a shared lib in C?
http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html Basically, 2 steps: 1) make the ...
- crontab如何设置秒级别的定时【转载】
* * * * * date > /home/gamester88/test/nihao.txt * * * * * (sleep 10 && date >> /ho ...