34,838
社区成员




create table tb(group_id varchar(20),user_iid int)
insert into tb(group_id,user_iid)
select 'sadfasfasdas',14477 union all
select 'sadfasfasdas',14477 union all
select 'sadfasfasdas',14477 union all
select 'sadfasfasdas',14478 union all
select 'sadfasfasdas',14478 union all
select 'sadfasfasdas',14478 union all
select 'sadfasfasdas',14478 union all
select 'sadfasfasdas',14479 union all
select 'sadfasfasdas',14479 union all
select 'sadfasfasdas',14479 union all
select 'sadfasfasdas',14477 union all
select 'sadfasfasdas',14477
with cte as
(select group_id,user_iid,iid=row_number() over (order by getdate()) from tb)
select a.group_id,a.user_iid from cte a where not exists(
select 1 from cte b where a.user_iid=b.user_iid and a.iid<b.iid
)
group_id user_iid
-------------------- -----------
sadfasfasdas 14478
sadfasfasdas 14479
sadfasfasdas 14477
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)
select * from tb a
where a.group_id=(select top 1 group_id from tb b where a.user_id=b.user_id)