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
distinct 一下呗,如果记录条数是1就说明10条数据都一样,否则就不一样了!
当然用count() group by 也是可行的。
一条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
(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
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