是这样的表,item member score 1 李明 90 2 李明 90 3 刘备 85 4 刘备 85 5 刘备 85 可以这样来查询 select min(item) Item,member,score from 表名 group by member,score但是如果表的列很多时这样就太麻烦了,不知道有没有更好的方法
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 行) */
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 行) */
感觉还是你的好啊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 min(item) Item,member,score from #tb group by member,scorewith cte as ( select *,ROW_NUMBER() OVER(PARTITION BY member order by item asc) as RN from #tb ) select * from cte where RN=1
1 李明 90
2 李明 90
3 刘备 85
4 刘备 85
5 刘备 85
可以这样来查询
select min(item) Item,member,score from 表名 group by member,score但是如果表的列很多时这样就太麻烦了,不知道有没有更好的方法
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 行)
*/
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 行)
*/
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 毫秒。
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,scorewith cte as
(
select *,ROW_NUMBER() OVER(PARTITION BY member order by item asc) as RN
from #tb
)
select * from cte where RN=1