表 a (主键 id int,name varchar(20))select id,name from a结果:001 张三002 李四003 王五004 刘二005 张三006 李四007 张三008 刘二009 张三... ....现在要达到这个目的:当name值有重复的记录是,第一个值不变,有重复的从第二个值update为name=name+大写字母A
,有重复的第三个值update为name=name+大写字母B,有重复的第四个值update为name=name+大写字母C,以此类推只到
有重复的第N个值update为name=name+大写字母(A-Z),现在其中重复的值不会达到26个。
达到效果如下:select id,name from a结果:001 张三002 李四003 王五004 刘二005 张三A006 李四A007 张三B008 刘二A009 张三C... ....分不多,希望能得到大家的帮助谢谢!
 

解决方案 »

  1.   


    create table tb(id char(3),name varchar(10))
    insert into tb
    select '001','张三' union
    select '002','李四' union
    select '003','张三' union
    select '004','李四' union
    select '005','李四' union
    select '006','张三' ;with cte as
    (
     select ROW_NUMBER() over(PARTITION by name order by id) no,* from tb
    )
    update a set a.name=b.name+CHAR(65+b.no-2)
    from tb a ,cte b where a.id=b.id and b.no>1select * from tb/*
    id   name
    ---- ----------
    001  张三
    002  李四
    003  张三A
    004  李四A
    005  李四B
    006  张三B
      

  2.   

    drop table tbcreate table tb(id char(20),name varchar(20))select id,name from tbinsert into tb(id,name)
    select '001','张三'
    union all select '002','李四'union all select '003','王五'union all select '004','刘二'union all select '005','张三'union all select '006','李四'union all select '007','张三'union all select '008','刘二'union all select '009','张三'select *,replace(name+char(64+(select count(1) from tb where name=a.name and id<a.id)),'@','')  as name2 from tb a
    /*
    id   name    name2
    --- ------  ------
    001  张三  张三
    002  李四  李四
    003  王五  王五
    004  张二  张二
    005  张三  张三A
    006  李四  李四A
    007  张三  张三B
    008  张二  张二A
    009  张三  张三C*/
      

  3.   

    drop table tbcreate table tb(id char(20),name varchar(20))select id,name from tbinsert into tb(id,name)
    select '001','张三'
    union all select '002','李四'union all select '003','王五'union all select '004','刘二'union all select '005','张三'union all select '006','李四'union all select '007','张三'union all select '008','刘二'union all select '009','张三'select *,replace(name+char(64+(select count(1) from tb where name=a.name and id<a.id)),'@','') as name2 from tb a
    /*
    id name name2
    --- ------ ------
    001 张三 张三
    002 李四 李四
    003 王五 王五
    004 张二 张二
    005 张三 张三A
    006 李四 李四A
    007 张三 张三B
    008 张二 张二A
    009 张三 张三C*/