当前位置: 代码迷 >> Sql Server >> 这个还能优化吗?解决方法
  详细解决方案

这个还能优化吗?解决方法

热度:61   发布时间:2016-04-24 09:54:10.0
这个还能优化吗?
select top 5 Id,[Type] from A  where [Type] =0 
union all 
select top 5 Id,[Type] from A  where [Type] =1


需求是根据表中的类别(0-3),每个类别都取5条,上面这样的写法(只是模拟先取2个类别)还能优化不?

------解决思路----------------------
用ROW_NUMBER ,你的代码少了排序列啊。不然TOP 5 就是随机取的了
select * from 
(select * ,row_number()over(partition by type  order by colname) asn from A) as t
where t.n<=5 
  相关解决方案