重复ID的记录,只显示其中1条


--重复ID的记录,只显示其中1条

--生成原始表
select * into #tempTable from (
select '1' as id ,'a' as name
union all
select '1' as id ,'b' as name
union all
select '2' as id ,'c' as name
union all
select '2' as id ,'d' as name ) a

--查询原始表
select * from #tempTable

--增加序号列
select ROW_NUMBER() OVER (ORDER BY id) AS xh ,* into #tempTableXh from #tempTable

--最终查询
select * from #tempTableXh where xh in (select MIN(xh) from #tempTableXh group by id)

--删除临时表
drop table #tempTable
drop table #tempTableXh

原文地址:https://www.cnblogs.com/zouhao/p/5113246.html