这个还能优化吗?

沈笑菲 2014-10-14 10:26:00
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个类别)还能优化不?
...全文
80 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
沈笑菲 2014-10-14
  • 打赏
  • 举报
回复
引用 1 楼 alimake 的回复:
用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
真真真真是厉害
xiaodongni 2014-10-14
  • 打赏
  • 举报
回复
用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

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧