Update e       
set idcard_key =b.pi_y+a.addressid+c.subid+d.subjectid +right(10000000+(select count(*) from gw_card where address=e.address and subjecth=e.subjecth and subject=e.subject and id <=e.id),5)from           gw_card e,gw_address a,gw_pici b,gw_subjecth c,gw_subject d   
where e.address=a.addressname and e.subjecth=c.subjecthead and d.subject=e.subject   
其中:gw_card e(证件表),gw_address(地市州)a,gw_pici(批次) b,gw_subjecth(专业)c,gw_subject(专业下的科目)d   
以上是现在生成证件号码的语句. 但是因为上面红色那段代码同一个类别下的号码却是按照ID的连续生成的. 
现在要求通过修改红色一段代码来实现对同类别下的一段号码随机分配.并且不重复.不空值.速度快. 在这里思考随机问题的时候可以不考虑前面的类别问题.
(数据> 7万条) 
昨天已经问过一个帖.许多语句或多或少可以实现.但是执行都需要6分钟.(自信解答了这个问题的人去下面地址跟贴JF) 
http://topic.csdn.net/u/20071205/13/f6bfe4a5-b3ec-4b91-abf9-5699c0f532cd.html 
最简单的模拟问题: 
就是说有a,b,c,d,e五个人,给这个5个人随机分1,2,3,4,5这5个编号.每个人分一个,不重复,不空值,有效率. 
当然这里的5个人是举例,实际情况是变化的.各个类别下的人何止千百。所以必须要求语句精湛才行. 平均每2个小时加1个贴求解此问题.下午3点要交了..急死了...望各位达人BM处理下这个问题.

解决方案 »

  1.   

    老實說,俺不是很明白需求哈,
    隨便寫一個,接人無完人鳥鳥同志的測試數據CREATE TABLE gw_card
    (
    id INT IDENTITY(1,1),
    address VARCHAR(100),
    subjecth INT,
    subject VARCHAR(10),
    id_key VARCHAR(10)
    )
    INSERT gw_card SELECT 'aa',1,'xx',NULL
    UNION ALL SELECT 'aa',1,'xx',NULL
    UNION ALL SELECT 'bb',1,'xx',NULL
    UNION ALL SELECT 'cc',1,'yy',NULL
    UNION ALL SELECT 'bb',1,'xx',NULL
    UNION ALL SELECT 'aa',1,'xx',NULL
    UNION ALL SELECT 'aa',1,'xx',NULL
    select tmp=(select count(*) from gw_card where address=a.address and subjecth=a.subjecth and subject=a.subject and id<=a.id),
              address,subjecth,subject,convert(varchar(10),'') as id_key
    into #t1 
    from gw_card Adeclare @n int
    select @n=max(tmp) from #t1set  rowcount   @n
    select id=identity(int,1,1)  into #  from syscolumns a,syscolumns b
    set  rowcount   0create table #t2 (id int identity(1,1), value int)
    insert into #t2(value)
    select id from # order by newid()update #t1
    set id_key=right(10000000000+value,10)
    from #t2
    where #t1.tmp=#t2.id
    select  address,subjecth,subject,id_key from #t1
    /*
    aa 1 xx 0000000003
    aa 1 xx 0000000001
    bb 1 xx 0000000003
    cc 1 yy 0000000003
    bb 1 xx 0000000001
    aa 1 xx 0000000002
    aa 1 xx 0000000004*/drop table #,#t1,#t2
    drop table gw_card
      

  2.   

    樓主可不可以搞好測試環境?弄點測試數據來?
    就這么一個update語句放在那,很抽象啊
      

  3.   

    简化一下:
    t1
    ---------------------
    id    address  subjecth subject   id_key
    1      11       00        02       1100020002
    2      11       00        02       1100020001
    3      11       00        02       1100020003 (后面的123随机的)
    4      11       01        01       1101010005
    4      11       01        01       1101010001
    4      11       01        01       1101010003
    4      11       01        01       1101010004
    4      11       01        01       1101010002(后面的12345随机的)其实:人无完人的例子已经很接近了。但是运行以后。小量的数据没有问题。当数据太多的时候。速度会很慢。并且会出现空值。
      

  4.   

    时间就像乳沟,挤挤还是有的:
    上面的数据有问题的。
    aa    1    xx    0000000003
    aa    1    xx    0000000001
    bb    1    xx    0000000003------------
    cc    1    yy    0000000003
    bb    1    xx    0000000001------------
    aa    1    xx    0000000002
    aa    1    xx    0000000004
    --------------------------------------------------------
    bb   后面的02不在了。
      

  5.   


    --難道是這樣,最后寫一次,不對就不寫啦
    CREATE TABLE gw_card
    (
    id INT ,
    address VARCHAR(100),
    subjecth INT,
    subject VARCHAR(10),
    id_key VARCHAR(10)
    )
    INSERT gw_card SELECT 1,'aa',1,'xx',NULL
    UNION ALL SELECT 2,'aa',1,'xx',NULL
    UNION ALL SELECT 3,'bb',1,'xx',NULL
    UNION ALL SELECT 4,'cc',1,'yy',NULL
    UNION ALL SELECT 5,'bb',1,'xx',NULL
    UNION ALL SELECT 6,'aa',1,'xx',NULL
    UNION ALL SELECT 7,'aa',1,'xx',NULLselect tmp=identity(int,1,1) ,*
    into #
    from
    (
    select top 100 percent *
    from gw_card
    order by address,subjecth,subject,newid()
    )  X
    select cnt=(select count(*) from # a where a.address=#.address and a.subjecth=#.subjecth and a.subject=#.subject and a.tmp<=#.tmp),
    *
    into #T
    from #update #T 
    set id_key=right(1000000+cnt,5)select id,address,subjecth,subject,id_key  from #T
    /*
    id      adress    subjecth   subject   id_key
    ------------------------------------------------
    2 aa 1 xx 00001
    1 aa 1 xx 00002
    6 aa 1 xx 00003
    7 aa 1 xx 00004
    5 bb 1 xx 00001
    3 bb 1 xx 00002
    4 cc 1 yy 00001*/
    drop table #,#t
    drop table gw_card