select top 10 *
from ta 
order by case when chk = 1 then 0 else 1 end ,NEWID( )

解决方案 »

  1.   


    select top 10 * 
    from ta 
    order by case when chk = 1 then 0 else 1 end ,NEWID( )楼上正解
      

  2.   

    select top 10 * 
    from ta 
    order by case when chk = 1 then 0 else 1 end 
      

  3.   

    LS的就可以了,为何要加newid()??
    declare @tb table(id int,class int,type varchar(50))
    insert into @tb select 1,1,'a'
    insert into @tb select 2,1,'b'
    insert into @tb select 3,2,'a'
    insert into @tb select 4,1,'d'
    insert into @tb select 5,2,'c'
    insert into @tb select 1,1,'a'
    insert into @tb select 2,1,'b'
    insert into @tb select 3,2,'a'
    insert into @tb select 4,1,'d'
    insert into @tb select 5,2,'c'
    select top 10 * from @tb order by case when class=1 then 0 else 1 end2 1 b
    1 1 a
    4 1 d
    2 1 b
    1 1 a
    4 1 d
    5 2 c
    3 2 a
    5 2 c
    3 2 a
      

  4.   

    不加NEWID()每次取的结果是不会变化的,我需要的是CHK=1的必须先防在最上面,下面的是不断变化的。
    不过我用了这个语句为什么每次取出来的结果数目却是不对的呢??SELECT * FROM #tabletmp a
    WHERE id IN
              (SELECT top 6 id
             FROM #tabletmp
             WHERE sclassid = a.sclassid
             ORDER BY (case when p0_chk=1 then 0 else 1 end),newid())
      

  5.   

    --这样行不?
    declare @tb table(id int,class int,type varchar(50))
    insert into @tb select 1,1,'a'
    insert into @tb select 2,1,'b'
    insert into @tb select 3,2,'a'
    insert into @tb select 4,1,'d'
    insert into @tb select 5,2,'c'
    insert into @tb select 1,1,'a'
    insert into @tb select 2,1,'b'
    insert into @tb select 3,2,'a'
    insert into @tb select 4,1,'d'
    insert into @tb select 3,2,'c'
    insert into @tb select 6,4,'c'
    insert into @tb select 8,5,'c'
    insert into @tb select 33,6,'c'
    insert into @tb select 333,8,'c'select top 10 * from (
        select * from @tb where class = 1
        union all 
        select * from @tb where class <> 1 ) T 
       order by case when class = 1 then 0 else 1 end,newid()
      

  6.   

    我的情况是直接用在主语句中的select top 10 * from ta order by case when chk = 1 then 0 else 1 end ,NEWID( )查询是对的,但是用在子语句中查出就有问题,比如SELECT * FROM #tabletmp a WHERE id IN (SELECT top 6 id FROM #tabletmp WHERE sclassid = a.sclassid ORDER BY (case when p0_chk=1 then 0 else 1 end),newid()) 这样的话查出来的每个ID的数目应该是6条,但是我有时候查的是对的,有时候要么排序不对,要么数目不对,奇怪!!!