博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

【原创】SQL Server 中distinct和top结合使用的方法

Posted on 2011-07-27 09:51  风之韵  阅读(1324)  评论(0)    收藏  举报

一、错误写法,错误信息:“在关键字 'distinct' 附近有语法错误。”。

select top 10 distinct EmpName, Sex, Age, DeptName from tbEmployee order by EmpName

二、正确写法,解析:distinct要放到top前面,先将数据distinct去重复,然后再取其中的前10行。

select distinct top 10 EmpName, Sex, Age, DeptName from tbEmployee order by EmpName