select * from(
select 1 a,2 b,3 c union 
select 2,3,4 union
select 3,4,5) m
order by newid()

解决方案 »

  1.   

    --try
    select * from 
             ((select top 3 examid from tblexam 
                     where examtype = 16 and examsubject in(1,53,56,57,58,54,59,60,61,55,62,63,64)
    and examid not in (35) ) a
    union 
    (select examid from tblexam where examid in (37)  group by examid having avg(examdiff) = 1) b) m
    order by newid() 
      

  2.   

    set nocount on
    go
    declare @tb table (id int identity,diff int default 20)
    insert @tb(diff)
    select 120 union
    select 10 union
    select 25 union
    select 37 union
    select 58 union
    select 47 union
    select 20 union
    select 246 union
    select 256 union
    select 12declare @tmp table(id int,diff int)
    declare @avgValue int,@loop int
    select @avgValue=10,@loop=1
    insert @tmp select * from @tb where id in(1,2,3)while 1>0
    begin
         insert @tmp select top 2 * from @tb where id not in(1,2,3) order by newid()
         if (select avg(diff) from @tmp)=@avgValue
            break
         else
            delete from @tmp where id not in(1,2,3)     set @loop=@loop+1
         if @loop>=100
            break
    end if (select count(1) from @tmp)<5
    print '程序循环'+convert(varchar,@loop)+'次,未找到符合条件的记录'
    else
    select * from @tmp
      

  3.   

    set nocount on
    go
    declare @tb table (id int identity,diff int default 20)
    insert @tb(diff)
    select 120 union
    select 10 union
    select 25 union
    select 37 union
    select 58 union
    select 47 union
    select 20 union
    select 246 union
    select 256 union
    select 12declare @tmp table(id int,diff int)
    declare @avgValue int,@loop int
    select @avgValue=10,@loop=1
    insert @tmp select * from @tb where id in(1,2,3)while 1>0
    begin
         insert @tmp select top 2 * from @tb where id not in(1,2,3) order by newid()
         if (select avg(diff) from @tmp)=@avgValue
            break
         else
            delete from @tmp where id not in(1,2,3)     set @loop=@loop+1
         if @loop>=100
            break
    end if (select count(1) from @tmp)<5
    print '程序循环'+convert(varchar,@loop)+'次,未找到符合条件的记录'
    else
    select * from @tmp
      

  4.   

    select id into #t from tblexam where id in(1,2,3)insert #t select id from tblexam where id not in(1,2,3) group by id  having avg(examdiff) = 1  order by newid()select * from tblexam where id in(select id from #t)
      

  5.   

    如果根本就缺少id=1,2,3的某个id,或者 不存在这样的记录" 这五条记录的avg(diff)是一个给定的值 "你会怎么处理?
      

  6.   

    select * from 
             ((select top 3 examid from tblexam 
                     where examtype = 16 and examsubject in(1,53,56,57,58,54,59,60,61,55,62,63,64)
    and examid not in (35) ) a
    union 
    (select examid from tblexam where examid in (37)  group by examid having avg(examdiff) = 1) b) m
    order by newid() 服务器: 消息 156,级别 15,状态 1,行 5
    在关键字 'union' 附近有语法错误。
    服务器: 消息 170,级别 15,状态 1,行 6
    第 6 行: 'b' 附近有语法错误。