34,591
社区成员
发帖
与我相关
我的任务
分享
create table #tb(item int,member varchar(10),score int)
insert #tb
select 1 ,'李明', 90 union all
select 2 ,'李明', 90 union all
select 3 ,'刘备', 85 union all
select 4 ,'刘备', 85 union all
select 5 ,'刘备', 85
--select min(item) Item,member,score from #tb group by member,score
with cte as
(
select *,ROW_NUMBER() OVER(PARTITION BY member order by item asc) as RN
from #tb
)
select * from cte where RN=1
select distinct a.item,a.member,a.score from #tb as a
where not exists (select 1 from #tb as b where a.member=b.member and b.score= a.score and b.item<a.item)
--扫描计数 2,逻辑读取 6 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
--
--SQL Server 执行时间:
-- CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
SELECT MIN(item) as item,member,score from #tb group by member,score
--扫描计数 1,逻辑读取 1 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
--
--SQL Server 执行时间:
-- CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
create table #tb(item int,member varchar(10),score int)
insert #tb
select 1 ,'李明', 90 union all
select 2 ,'李明', 90 union all
select 3 ,'刘备', 85 union all
select 4 ,'刘备', 85 union all
select 5 ,'刘备', 85
--可以这样来查询,上面弄反了,呵呵
select Item,member,score from #tb a
where not exists (select 1 from #tb b where a.member=b.member
and a.score=b.score
and b.Item<a.Item)
/*
Item member score
----------- ---------- -----------
1 李明 90
3 刘备 85
(所影响的行数为 2 行)
*/
create table #tb(item int,member varchar(10),score int)
insert #tb
select 1 ,'李明', 90 union all
select 2 ,'李明', 90 union all
select 3 ,'刘备', 85 union all
select 4 ,'刘备', 85 union all
select 5 ,'刘备', 85
可以这样来查询
select Item,member,score from #tb a
where not exists (select 1 from #tb b where a.member=b.member
and a.score=b.score
and b.Item>a.Item)
/*
Item member score
----------- ---------- -----------
2 李明 90
5 刘备 85
(所影响的行数为 2 行)
*/