34,587
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (key1 varchar(1),key2 varchar(1),content1 varchar(3),content2 varchar(3),content3 varchar(3))
insert into #T
select 'A','B','...','...','...' union all
select 'C','B','...','...','...' union all
select 'A','B','...','...','...' union all
select 'A','B','...','...','...' union all
select 'D','E','...','...','...' union all
select 'C','B','...','...','...'
--> 2005
select id=row_number()over(partition by key1 order by key1), * from #T
--> 2000
if object_id('tempdb.dbo.#') is not null drop table #
select id=identity(int,1,1),* into # from #T order by key1
select id1=(select count(1) from # where key1=t.key1 and id<=t.id),key1,key2,content1,content2,content3 from # as t
/*
id1 key1 key2 content1 content2 content3
----------- ---- ---- -------- -------- --------
1 A B ... ... ...
2 A B ... ... ...
3 A B ... ... ...
1 C B ... ... ...
2 C B ... ... ...
1 D E ... ... ...
*/
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (key1 varchar(1),key2 varchar(1),content1 varchar(3),content2 varchar(3),content3 varchar(3))
insert into #T
select 'A','B','...','...','...' union all
select 'C','B','...','...','...' union all
select 'A','B','...','...','...' union all
select 'A','B','...','...','...' union all
select 'D','E','...','...','...' union all
select 'C','B','...','...','...'
select id=row_number()over(partition by key1 order by key1), * from #T
/*
id key1 key2 content1 content2 content3
-------------------- ---- ---- -------- -------- --------
1 A B ... ... ...
2 A B ... ... ...
3 A B ... ... ...
1 C B ... ... ...
2 C B ... ... ...
1 D E ... ... ...
*/