select top 1 * from tablename order by newid()

解决方案 »

  1.   

    select
        a.*
    from
        表 a
    where
        a.lastcol = (select top 1 lastcol from 表 
                     where firstcol=a.firstcol and ... and ninthcol=a.ninthcol)
      

  2.   

    select top 1 * from tablename where 最后一个字段 in (
    select 最后一个字段 from tablename group by 最后一个字段 having count(*)=1)
      

  3.   

    --你把前9个字段分组,然后再取Top 1
    --例子
    --测试环境declare @t table(name varchar(10),code varchar(10),a varchar(10),b varchar(10),value int)
    insert into @t select 'a','a','a','a',1
    union all select 'a','a','a','a',2
    union all select 'b','b','b','b',3
    union all select 'b','b','b','b',4
    union all select 'c','c','c','c',5
    union all select 'c','c','c','c',6
    union all select 'd','d','d','d',7
    union all select 'd','d','d','d',8
    union all select 'e','e','e','e',9
    union all select 'e','e','e','e',10
    --查询语句
    select name,code,a,b,value=(select top 1 value from @t where name=a.name and code=a.code and a=a.a and b=a.b  ) from @t a
    group by name,code,a,b
    --结果
    name       code       a          b          value       
    ---------- ---------- ---------- ---------- ----------- 
    a          a          a          a          1
    b          b          b          b          3
    c          c          c          c          5
    d          d          d          d          7
    e          e          e          e          9(所影响的行数为 5 行)
      

  4.   

    select top 1 * from 表名 order by 最后一个字段