- 爱易网页
 
                        - 
                            MSSQL教程
 
                        - 按条件限量随机提取记录有关问题 
 
                         
                    
                    
                    日期:2014-05-18  浏览次数:20743 次 
                    
                        
                         按条件限量随机提取记录问题
DECLARE   @test1   table(c1   varchar(10),c2   varchar(10)) 
 INSERT   INTO   @test1   (c1,c2) 
                               SELECT    '1 ', 'a ' 
 UNION   ALL   SELECT    '2 ', 'a ' 
 UNION   ALL   SELECT    '3 ', 'a ' 
 UNION   ALL   SELECT    '4 ', 'b ' 
 UNION   ALL   SELECT    '5 ', 'b ' 
 UNION   ALL   SELECT    '6 ', 'b ' 
 UNION   ALL   SELECT    '7 ', 'b ' 
 UNION   ALL   SELECT    '8 ', 'c ' 
 UNION   ALL   SELECT    '9 ', 'c ' 
 UNION   ALL   SELECT    '10 ', 'd ' 
 UNION   ALL   SELECT    '11 ', 'd ' 
 UNION   ALL   SELECT    '12 ', 'd ' 
 UNION   ALL   SELECT    '13 ', 'e ' 
 UNION   ALL   SELECT    '14 ', 'e ' 
 UNION   ALL   SELECT    '15 ', 'e ' 
  
 DECLARE   @test2   table   (c2   varchar(10),cnt   int) 
 INSERT   INTO   @test2   (c2,MaxCnt) 
                               SELECT    'a ',2 
 UNION   ALL   SELECT    'c ',1 
 UNION   ALL   SELECT    'd ',1 
  
 问题是在@test1中随机提取N条记录,记录不重复。 
 所取记录的c2次数不大于@test2中c2所对应的MaxCnt。 
 @test2中没对应的c2表示没有数量限制。 
 请问有没有方法能用一条语句高效实现?
------解决方案--------------------
取n条随机记录 
 select top n from @test1 order by newid()
------解决方案--------------------
比如楼主的数据,N=10 
  
 create table #(id varchar(5)) 
 insert into # select 3 
 union all select 3 
 union all select 8 
 union all select 8 
  
 drop table #count 
 create table #count(ctid varchar(5)) 
 insert into #count select  '+ ' 
 union all select  '- ' 
 union all select  '* ' 
 union all select  '/ ' 
  
 select identity(int,1,1) id, '( '+aid+b.ctid+ '( '+bid+c.ctid+ '( '+cid+d.ctid+did+ '))) '[mula] into #wei 
 from 
 (select DISTINCT a.id [aid],b.id [bid],c.id [cid],d.id+ '.0 ' [did] from # a,# b,# c,# d 
 where len(REPLACE(a.id+b.id+c.id+d.id, '3 ', ' '))=3  
 and len(REPLACE(a.id+b.id+c.id+d.id, '8 ', ' '))=3  
 and len(REPLACE(a.id+b.id+c.id+d.id, '8 ', ' '))=2)a 
 ,#count b,#count c,#count d 
  
 create table #show(id int identity(1,1),result dec(18,3)) 
  
 declare @int int,@var varchar(50) 
 set @int=1 
 while @int <=(select max(id)from #wei) 
 begin 
 	select @var=mula from #wei where id=@int 
 	insert into #show exec( 'select  '+@var) 
 	if(@@ERROR=8134) 
 		insert into #show select null 
 	set @int=@int+1 
 end 
  
 select * from #wei a join #show b on b.id=a.id 
 where b.result=24 
  
 drop table #count 
 drop table # 
 drop table #wei 
 drop table #show 
  
  
 DECLARE @test1 table(c1 varchar(10),c2 varchar(10)) 
 INSERT INTO @test1 (c1,c2) 
           SELECT  '1 ', 'a ' 
 UNION ALL SELECT  '2 ', 'a ' 
 UNION ALL SELECT  '3 ', 'a ' 
 UNION ALL SELECT  '4 ', 'b ' 
 UNION ALL SELECT  '5 ', 'b ' 
 UNION ALL SELECT  '6 ', 'b '