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
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
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)
(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
)
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