表结构,id,value我想知道最后十条记录的值是否全都一样,用一条sql语句能写出来吗?

解决方案 »

  1.   

    把最后10条记录分组 count 一下不就知道了吗
      

  2.   

    group by 一下看count是否为1
      

  3.   

    就两个字段 简单的用GROUP BY 就OK啊
      

  4.   

    create table #sd
    (id int identity(1,1)not null,
     value int)
     insert into #sd(value) select 1
     union all select 1
     union all select 2
     union all select 1
     union all select 1
     union all select 2
    go
    select value,COUNT(value) from(
    select top(10)* from #sd  order by id desc)a group by value
      

  5.   

    distinct 一下呗,如果记录条数是1就说明10条数据都一样,否则就不一样了!
      

  6.   

    当然用count()   group by 也是可行的。
      

  7.   

    一条sql语句完成,select 
    case when count(distinct value)=1 then '最后十条记录的值全都一样.'
    else '最后十条记录的值不全一样.' end as 'result'
    from
    (select row_number() over(order by id desc) rn,
    id,value from tab) t
    where t.rn between 1 and 10