SQL Over

与over函数结合的几个函数

复制代码
create table #tab(A varchar(8), B varchar(8))
insert into #tab
select 'A1', 'B1' union all
select 'A1', 'B2' union all
select 'A1', 'B3' union all
select 'A2', 'B4' union all
select 'A2', 'B5' union all
select 'A2', 'B6' union all
select 'A3', 'B7' union all
select 'A3', 'B8' union all
select 'A3', 'B9'
复制代码

 

row_number() over() :

select row_number()over(order by A) as id ,* from #tab;

 

partition by:

按A分组,按A排序

select row_number()over(order by A) as id,
*,
row_number()over(partition by A order by A)Num
from #tab;

按A分组,组内按B倒序

select row_number()over(order by A) as id,
*,
row_number()over(partition by A order by B desc)Num
from #tab;

rank()over()

http://www.cnblogs.com/mycoding/archive/2010/05/29/1747065.html

select *,rank()over(order by A)from #tab;

select *,rank()over(partition by A order by A) from #tab;

select *,rank()over(partition by A order by B) from #tab;

也可以按多列分组,具体的看链接。

dense_rank()over()

select *,dense_rank()over(order by A)from #tab;

sum() over():

复制代码
create table #tab(A varchar(8), B varchar(8),C int)
insert into #tab
select 'A1', 'B1' ,10 union all
select 'A1', 'B2' ,10union all
select 'A1', 'B3' ,10 union all
select 'A2', 'B4' ,20 union all
select 'A2', 'B5' ,20union all
select 'A2', 'B6' ,20union all
select 'A3', 'B7' ,30union all
select 'A3', 'B8',30 union all
select 'A3', 'B9',30
复制代码
select *,sum(C)over(partition by A)from #tab;

select *,sum(C)over()from #tab;

http://hi.baidu.com/s__wind/item/a20107fa4eb6631ca6298858

http://www.cnblogs.com/85538649/archive/2011/08/13/2137370.html

http://www.cnblogs.com/lanzi/archive/2010/10/26/1861338.html

posted @   hongdada  阅读(393)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示