现在的位置: 首页 > 综合 > 正文

获取每个分类下的N条记录

2018年07月06日 ⁄ 综合 ⁄ 共 187字 ⁄ 字号 评论关闭
 --获取每个分类下的N条记录 
select *
from(
  select *,rn=row_number() over (partition by 类别 order by 日期 desc)
  from @tb
)t
where rn <= N
--或者
select 
  distinct b.*
from
  @tb a
cross apply
  (select top N * from @tb where 类别=a.类别 order by  newid())b

 

抱歉!评论已关闭.