34,837
社区成员




select id,(select count(*) from tb as S2 where S2.id<=S1.id)as
rownum from tb as S1 order by id
id rownum
--------------
1 1
3 2
5 3
8 4
10 5
11 6
if object_id('tb') is not null
drop table tb
create table tb
(
id int primary key
)
insert into tb
select 1 union all
select 3 union all
select 5 union all
select 8 union all
select 10 union all
select 11
select id,row_number() over(order by id) as rownum from tb
(6 行受影响)
id rownum
--------------
1 1
3 2
5 3
8 4
10 5
11 6
if object_id('test') is not null
drop table test
create table test
(
id int primary key
)
insert into test
select 1 union all
select 3 union all
select 5 union all
select 8 union all
select 10 union all
select 11
select *,IDENTity(int,1,1) as xh into ## from test
select * from ##
(6 行受影响)
id xh
----------- -----------
1 1
3 2
5 3
8 4
10 5
11 6
select * from (select *,row_number() over (order by a) '行数' from #a) as b
where b='2009-07-01'
-------------结果---------------
a b c 行数
2 2009-07-01 00:00:00.000 3 4
row_number()
Create table #a ( a int,b datetime,c int)
insert into #a
select 1 , '2009-06-01' , 1 union all
select 2 , '2009-06-01' , 2 union all---------------------要过滤掉的数据
select 2 , '2009-06-02' , 3 union all
select 2 , '2009-07-01' , 3 union all
select 3 , '2009-06-01' , 2
select *,row_number() over (order by a) '行数' from #a
if object_id('test') is not null
drop table test
create table test
(
id int primary key
)
insert into test
select 1 union all
select 3 union all
select 5 union all
select 8 union all
select 10 union all
select 11
select *,(select count(1) from test where id <= t.id) from test t
/**
id
----------- -----------
1 1
3 2
5 3
8 4
10 5
11 6
(所影响的行数为 6 行)
**/