求一条SQL语句 求唯一性
如上结果
我想得到user_id 不重复的一条记录
比如 14477 就取一条 哪一条没关系 但是group_id 也要出现
distinct 后就得不到group_id了
14477 P_201204231150031112
14478 P_201206151035405245
14479 P_201208072017098564
这种结果
------解决方案--------------------select * from tb a
where a.group_id=(select top 1 group_id from tb b where a.user_id=b.user_id)
------解决方案--------------------
select * from tb a
where a.group_id=(select top 1 group_id from tb b where a.user_id=b.user_id order by group_id)
------解决方案--------------------with cte as
(select group_id,user_id,iid=row_number() over (order by getdate()) from 你的表)
select a.group_id,a.user_id from cte a where not exists(
select 1 from cte b where a.user_id=b.user_id and a.iid<b.iid
)
------解决方案--------------------select group_id,user_id from (select *,row=row_number()over(partition by user_id order by getdate()))t where t.row=1
------解决方案--------------------select group_id,user_id from (select *,row=row_number()over(partition by user_id order by getdate()) from 表)t where t.row=1