我想给一列数据编号,编号的规则是,每出现三个不同的名称,编号递增1(即当出现第4个,第7个,第10个...不同的名称时编号在原来的基础上加1)。格式如下:
id 名称 编号
1 A 1
2 A 1
3 A 1
5 B 1
6 B 1
7 C 1
8 D 2
9 D 2
10 E 2
id是自动的,但请考虑id号不连续的情况。请问这个怎么做?好难啊。
id 名称 编号
1 A 1
2 A 1
3 A 1
5 B 1
6 B 1
7 C 1
8 D 2
9 D 2
10 E 2
id是自动的,但请考虑id号不连续的情况。请问这个怎么做?好难啊。
这个编号是根据名称的不同编出来的,当名称为“D”时,因为“D”相对于前面的A、B、C是第四个不同的名称,所以让编号加上1,当出现第7个不同的名称时编号也加上1,依此类推。
换句话说就是每出现三个不同的名称,编号就加1。
我说的清楚了吗?
(id int identity(1,1),
名称 char(1))
insert test
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'b' union all
select 'c' union all
select 'd' union all
select 'd' union all
select 'e' union all
select 'f' union all
select 'g' union all
select 'h'
select * from test
go
alter table test add 编号 int
go
declare cu cursor local
for select id,名称 from test
declare @id int,@名称 char(1),@i int,@tem char(1),@编号 int
open cu
fetch cu into @id,@名称
select @tem=@名称,@编号=1,@i=1
while @@fetch_status=0
begin
if @i<3
begin
update test set 编号=@编号 where id=@id
if @名称<>@tem
select @i=@i+1,@tem=@名称
end
else begin
select @i=1,@编号=@编号+1,@tem=@名称
update test set 编号=@编号 where id=@id
end
fetch cu into @id,@名称
end
close cu
deallocate cu
select * from test
drop table test
----------- ----
1 a
2 a
3 a
4 b
5 b
6 c
7 d
8 d
9 e
10 f
11 g
12 h
------------------------------
id 名称 编号
----------- ---- -----------
1 a 1
2 a 1
3 a 1
4 b 1
5 b 1
6 c 1
7 d 2
8 d 2
9 e 2
10 f 2
11 g 3
12 h 3
declare @test table(id int ,name char(1),number int)
insert @test
select 1,'a',1 union all
select 2,'a',1 union all
select 3,'a',1 union all
select 5,'b',1 union all
select 6,'b',1 union all
select 7,'c',1 union all
select 8,'d',1 union all
select 9,'d',1 union all
select 10,'e',1 union all
select 11,'f',1 union all
select 12,'g',1 union all
select 13,'h',1 select * from @test--------------------------------
id name number
1 a 1
2 a 1
3 a 1
5 b 1
6 b 1
7 c 1
8 d 1
9 d 1
10 e 1
11 f 1
12 g 1
13 h 1--解决方法
declare @name varchar(1),@a int,@b int
set @a=4
update @test
set
@B = @a/3,
@a = case when @name<>name then @a+1 else @a end,
number = @B,
@name = name
select * from @test
--------------------------------------
id name number
1 a 1
2 a 1
3 a 1
5 b 1
6 b 1
7 c 1
8 d 2
9 d 2
10 e 2
11 f 2
12 g 3
13 h 3
[RecNoB] int default(1),
[名称] varchar(50))insert bb ([名称]) select distinct [名称] from 原表 order by [名称]update a set a.编号=b.RecNoB+b.RecNo/3 from 原表 as a, bb as b where a.名称=b.名称
万分感谢!
-------------------------------------------------
我不是取余数,因为RecNo是int的,所以会自动取整,加上它是自增的,编号会递增
create table test
(id int identity(1,1),
名称 char(1))
insert test
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'd' union all
select 'd' union all
select 'e' union all
select 'f' union all
select 'g' union all
select 'h'
go
alter table test add 编号 int
go
declare cu cursor local
for select id,名称 from test
declare @id int,@名称 char(1),@i int,@tem char(1),@编号 int
open cu
fetch cu into @id,@名称
select @tem=@名称,@编号=1,@i=1
while @@fetch_status=0
begin
if @名称<>@tem
select @i=@i+1,@tem=@名称
if @i>3
begin
select @i=1,@编号=@编号+1
end
update test set 编号=@编号 where id=@id
fetch cu into @id,@名称
end
close cu
deallocate cu
select * from test
/*
id 名稱 編號
1 a 1
2 a 1
3 a 1
4 b 1
5 b 1
6 c 1
7 c 1
8 d 2
9 d 2
10 e 2
11 f 2
12 g 3
13 h 3
*/
drop table test
go--方法二
create table #test
(id int identity(1,1),
名稱 char(1))
go
insert #test
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'd' union all
select 'd' union all
select 'e' union all
select 'f' union all
select 'g' union all
select 'h'
go
alter table #test add 編號 int
go
declare @名稱 char(1),@cnt int,@編號 int
select @名稱='',@cnt=0,@編號=0
update #test set @cnt=case when @名稱<>名稱 then @cnt+1 else @cnt end,@編號=case when @名稱<>名稱 and @cnt%3=1 then @編號+1 else @編號 end,@名稱=名稱,編號=@編號select * from #test
/*
id 名稱 編號
1 a 1
2 a 1
3 a 1
4 b 1
5 b 1
6 c 1
7 c 1
8 d 2
9 d 2
10 e 2
11 f 2
12 g 3
13 h 3
*/
drop table #test
--方法三
create table #test
(id int identity(1,1),
名稱 char(1))
go
insert #test
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'd' union all
select 'd' union all
select 'e' union all
select 'f' union all
select 'g' union all
select 'h'
go
alter table #test add 編號 int
go
declare @name varchar(1),@a int,@b int
set @a=2
set @name=''
update #test
set
@a = case when @name<>名稱 then @a+1 else @a end,
@B = @a/3,
編號 = @B,
@name = 名稱select * from #test
/*
id 名稱 編號
1 a 1
2 a 1
3 a 1
4 b 1
5 b 1
6 c 1
7 c 1
8 d 2
9 d 2
10 e 2
11 f 2
12 g 3
13 h 3
*/
drop table #test
--方法四
create table #test
(id int identity(1,1),
名稱 char(1))
go
insert #test
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'd' union all
select 'd' union all
select 'e' union all
select 'f' union all
select 'g' union all
select 'h'
go
alter table #test add 編號 intcreate table #test1 (id int IDENTITY,
[名稱] char(1))insert #test1 select distinct [名稱] from #test order by 1update a set a.編號=1+(b.id-1)/3 from #test as a, #test1 as b where a.名稱=b.名稱
select * from #test
/*
id 名稱 編號
1 a 1
2 a 1
3 a 1
4 b 1
5 b 1
6 c 1
7 c 1
8 d 2
9 d 2
10 e 2
11 f 2
12 g 3
13 h 3
*/
drop table #test,#test1
2.将原数据导入:Insert 新表(Name) Select Name From 原表。这样新表中编号就符合要求了。
3.原表增加id字段。
4.Update 原表 Set id=新表.id From 原表,新表 Where 原表.Name=新表.Name