日期:2014-05-18 浏览次数:20799 次
--建立测试环境 set nocount on create table test(idcard varchar(20),address varchar(2),subject varchar(2)) insert into test select '13233','02','22' insert into test select '45455','02','22' insert into test select '33555','02','22' insert into test select '13356','02','22' insert into test select '94432','02','22' insert into test select '22444','01','21' insert into test select '12342','01','21' insert into test select '10321','01','21' go --测试 declare @table table(idcard varchar(20),randRow uniqueidentifier) insert into @table select idcard,newid() from test select idcard,address,subject, address+subject+right('00000'+cast(rownum as varchar(10)),5) as [Key] from( select a.*, (select count(*)+1 from @table t inner join test on t.idcard=test.idcard where randRow>b.randRow and address=a.address and subject=a.subject) as rownum from test a inner join @table b on a.idcard=b.idcard )c order by address,subject,idcard --删除测试环境 drop table test set nocount off /* idcard address subject Key -------------------- ------- ------- -------------- 10321 01 21 012100003 12342 01 21 012100002 22444 01 21 012100001 13233 02 22 022200004 13356 02 22 022200005 33555 02 22 022200001 45455 02 22 022200002 94432 02 22 022200003 */