--测试
declare @tb table(ID int identity,deptID int,empName varchar(20))
insert @tb(deptID,empName) select * from #
select deptID,empName from @tb A where not exists(select 1 from @tb where ID<A.ID and deptID=A.deptID)
--删除测试环境
drop table #
--结果
/*
deptID empName
----------- --------------------
1 张三
2 Bill
--测试
declare @tb table(ID int identity,deptID int,empName varchar(20))
insert @tb(deptID,empName) select * from #
select ID,deptID,empName from @tb A where not exists(select 1 from @tb where ID<A.ID and deptID=A.deptID)
--删除测试环境
drop table #
--结果
/*
ID deptID empName
----------- ----------- --------------------
1 1 张三
4 2 Bill