自动编号问题?

insert2003 2006-03-15 04:37:42
有如下表
tb_Test
c_Group c_No
A0001
A0001
A0001
A0001
A0001
A0002
A0002
A0002
A0002
A0002
A0003
A0003
A0003
A0003
A0003
A0003

按c_Group自动编号,就是c_Group一致的,从1开始递增编号
要得到如下结果
c_Group c_No
A0001 1
A0001 2
A0001 3
A0001 4
A0001 5
A0002 1
A0002 2
A0002 3
A0002 4
A0002 5
A0003 1
A0003 2
A0003 3
A0003 4
A0003 5
A0003 6

请问,应该如何做?
...全文
132 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
aniude 2006-03-15
  • 打赏
  • 举报
回复
Mark
-狙击手- 2006-03-15
  • 打赏
  • 举报
回复
create table tb_Test(c_Group varchar(6),c_No int)
insert tb_test(c_group)
select 'A0001' union all
select 'A0001' union all
select 'A0001' union all
select 'A0001' union all
select 'A0001' union all
select 'A0002' union all
select 'A0002' union all
select 'A0002' union all
select 'A0002' union all
select 'A0002' union all
select 'A0003' union all
select 'A0003' union all
select 'A0003' union all
select 'A0003' union all
select 'A0003' union all
select 'A0003'
go

select rowid =identity(int,1,1),* into # from tb_test
select c_Group ,(select count(1)+1 from # where c_group = a.c_group and rowid < a.rowid) as c_no
from # a

drop table tb_test,#
/*


c_Group c_no
------- -----------
A0001 1
A0001 2
A0001 3
A0001 4
A0001 5
A0002 1
A0002 2
A0002 3
A0002 4
A0002 5
A0003 1
A0003 2
A0003 3
A0003 4
A0003 5
A0003 6

*/
lsqkeke 2006-03-15
  • 打赏
  • 举报
回复
子陌好快哦 比我快不到一秒 :)
lsqkeke 2006-03-15
  • 打赏
  • 举报
回复
declare @tb_Test table(c_Group varchar(10), c_N int)
insert @tb_Test(C_Group)
select 'A0001' union all
select 'A0001' union all
select 'A0001' union all
select 'A0001' union all
select 'A0001' union all
select 'A0002' union all
select 'A0002' union all
select 'A0002' union all
select 'A0002'

declare @i int
declare @v varchar(100)
set @i=0
update @tb_Test
set
@i =case when @v=c_group then @i+1 else 1 end,
@v=c_group ,
c_n=@i
from @tb_Test

select * from @tb_Test
子陌红尘 2006-03-15
  • 打赏
  • 举报
回复
declare @t table(c_Group varchar(20),c_No int)
insert into @t select 'A0001',null
insert into @t select 'A0001',null
insert into @t select 'A0001',null
insert into @t select 'A0001',null
insert into @t select 'A0001',null
insert into @t select 'A0002',null
insert into @t select 'A0002',null
insert into @t select 'A0002',null
insert into @t select 'A0002',null
insert into @t select 'A0002',null
insert into @t select 'A0003',null
insert into @t select 'A0003',null
insert into @t select 'A0003',null
insert into @t select 'A0003',null
insert into @t select 'A0003',null
insert into @t select 'A0003',null

declare @c_Group varchar(20),@c_No int

update @t
set
@c_No=case when c_Group=@c_Group then @c_No+1 else 1 end,
@c_Group=c_Group,
c_No=@c_No

select * from @t
/*

c_Group c_No
-------------------- -----------
A0001 1
A0001 2
A0001 3
A0001 4
A0001 5
A0002 1
A0002 2
A0002 3
A0002 4
A0002 5
A0003 1
A0003 2
A0003 3
A0003 4
A0003 5
A0003 6
*/
mm2love2zz 2006-03-15
  • 打赏
  • 举报
回复
:)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧