这个表查询时如何去除重复行,内容一样但序号不同

lixm007 2011-07-14 08:13:27
...全文
125 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixm007 2011-12-01
  • 打赏
  • 举报
回复
很久没来了,都忘记我的贴了,谢谢大家
liangyong1107 2011-07-14
  • 打赏
  • 举报
回复


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


wxxloveu 2011-07-14
  • 打赏
  • 举报
回复
感觉还是你的好啊

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 毫秒。
claro 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lixm007 的回复:]
可以这样来查询
select min(item) Item,member,score from 表名 group by member,score
但是如果表的列很多时这样就太麻烦了,不知道有没有更好的方法
[/Quote]有!
但你的方式就很好。
cd731107 2011-07-14
  • 打赏
  • 举报
回复
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 行)
*/
cd731107 2011-07-14
  • 打赏
  • 举报
回复
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 行)
*/
lixm007 2011-07-14
  • 打赏
  • 举报
回复
是这样的表,

item member score
1 李明 90
2 李明 90
3 刘备 85
4 刘备 85
5 刘备 85


可以这样来查询
select min(item) Item,member,score from 表名 group by member,score

但是如果表的列很多时这样就太麻烦了,不知道有没有更好的方法
AcHerat 元老 2011-07-14
  • 打赏
  • 举报
回复
木图木真相!

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧