与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'

 SQL Over

row_number() over() :

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

 SQL Over

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;

SQL Over

按A分组,组内按B倒序

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

SQL Over

rank()over()

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

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

SQL Over

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

SQL Over

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

SQL Over

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

dense_rank()over()

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

SQL Over

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;

SQL Over

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

SQL Over

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-06-03
  • 2021-09-30
猜你喜欢
  • 2021-11-11
  • 2021-12-31
  • 2021-05-22
  • 2021-11-17
  • 2021-10-10
  • 2022-02-23
  • 2021-08-22
相关资源
相似解决方案