distinct 和 TOP N 怎么一起用

来源:百度知道 编辑:UC知道 时间:2024/05/31 09:20:40
select distinct(top 2 cusid) from customer

我想查询customer表里 cusid 前2位 并且消除重复 distinct 和 top n 怎么才能一起用呢
请问最后面为什么要加T呢?

select distinct * from (select top 2 cusid from customer) T
-------
意思是把(select top 2 cusid from customer)这个结果集取个别名叫T。
于是简化了就是select distinct * from T

select distinct top 2 cusid from customer
这样就可以了distinct 与 top 同时使用的时候 distinct 要放top前面

distinct是去除重复项
TOP 是取前N项记录