表A有两个字段分别是票号,序号,下面例子中有两个序号4,如何能重新整理序号,使其不重复,都是唯一的呢、
H01C201008010002 1
H01C201008010002 2
H01C201008010002 4
H01C201008010002 4
H01C201008010002 5
H01C201008010002 6
H01C201008010002 7
H01C201008010002 1
H01C201008010002 2
H01C201008010002 4
H01C201008010002 4
H01C201008010002 5
H01C201008010002 6
H01C201008010002 7
set @i=0
update ta set 序号=@i,@i=@i+1
drop table tb
Go
Create table tb([票号] nvarchar(16),[序号] int)
Insert tb
select N'H01C201008010002',1 union all
select N'H01C201008010002',2 union all
select N'H01C201008010002',4 union all
select N'H01C201008010002',4 union all
select N'H01C201008010002',5 union all
select N'H01C201008010002',6 union all
select N'H01C201008010002',7
Go
declare @i int
set @i=0
update tb set 序号=@i,@i=@i+1
select *
from tb
/*
票号 序号
---------------- -----------
H01C201008010002 1
H01C201008010002 2
H01C201008010002 3
H01C201008010002 4
H01C201008010002 5
H01C201008010002 6
H01C201008010002 7
*/
WITH CTE AS(
SELECT *, SN = ROW_NUMBER() OVER(PARTITION BY 票号 ORDER BY (SELECT 1))
FROM [表A]
)
UPDATE CTE SET 序号 = SN
N'H01C201008010002'
N'H01C201008010003'
N'H01C201008010005'
N'H01C201008010008'
N'H01C201008010009'
N'H01C201008010010'
while exists(select 1 from #d)
begin
select top 1 @ph=票号 from #d order by newid()
set @i=0
update sycc set 序号=@i,@i=@i+1 where 票号=@phdelete #d where 票号=@ph
if not exists(select 1 from #d)
break
else
continue
end
求比这个方法好的,学习,,,,
drop table tb
Go
Create table tb([票号] nvarchar(16),[序号] int)
Insert tb
select N'H01C201008010002',1 union all
select N'H01C201008010002',2 union all
select N'H01C201008010002',4 union all
select N'H01C201008010002',4 union all
select N'H01C201008010002',5 union all
select N'H01C201008010002',6 union all
select N'H01C201008010002',7
go
alter table tb add id int identity(1,1);
go
update tb
set 序号=ID
alter table tb drop column id
go
select * from tb ;
/*
票号 序号
---------------- -----------
H01C201008010002 1
H01C201008010002 2
H01C201008010002 3
H01C201008010002 4
H01C201008010002 5
H01C201008010002 6
H01C201008010002 7
*/
if not object_id('tb') is null
drop table tb
Go
Create table tb([票号] nvarchar(16),[序号] int)
Insert tb
select N'H01C201008010002',1 union all
select N'H01C201008010002',2 union all
select N'H01C201008010002',4 union all
select N'H01C201008010002',4 union all
select N'H01C201008010002',5 union all
select N'H01C201008010002',6 union all
select N'H01C201008010002',7 union all
select N'H01C201008010003',8 union all
select N'H01C201008010003',9 union all
select N'H01C201008010003',10 union all
select N'H01C201008010003',11
Go
declare @i int,@ph nvarchar(16)
set @i=0
set @ph=''
update tb set [序号]=case when [票号]=@ph then @i else [序号]end,
@i=case when [票号]=@ph then @i+1 else 1 end,
@ph=[票号]
select *
from tb
/*
票号 序号
---------------- -----------
H01C201008010002 1
H01C201008010002 2
H01C201008010002 3
H01C201008010002 4
H01C201008010002 5
H01C201008010002 6
H01C201008010002 7
H01C201008010003 1
H01C201008010003 2
H01C201008010003 3
H01C201008010003 4*/
alter table 你的表名 add id int identity(1,1);
go
update 你的表名
set 序号=ID
alter table 你的表名 drop column id
go
from tableName
如果是去掉重复的数据的话,就用distinct 或group by 分组