如何用一條Sql語句搜索重復的記錄 和 如何用一條Sql語句查找出不重復的記錄

huole 2007-10-17 08:51:53
如何用一條Sql語句篩選出重復的記錄

和 如何用一條Sql語句篩選出不重復的記錄
...全文
286 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
huole 2007-10-17
  • 打赏
  • 举报
回复
3q 3q
dawugui 2007-10-17
  • 打赏
  • 举报
回复
第二问题的更多写法:

c1 c2 type
第1行 a 9 5
第2行 b 8 5
第3行 c 7 6
第4行 d 6 6
第5行 e 5 7


--@Test
declare @test table(c1 varchar(1),c2 int,type int)
insert @test
select 'a',9,5 union all
select 'b',8,5 union all
select 'c',7,6 union all
select 'd',6,6 union all
select 'e',5,7

--按记录顺序取第一条
select * from @test a where c1=(select top 1 c1 from @Test where type=a.type)

--取最小
select * from @test a where c1=(select min(c1) from @Test where type=a.type)

--取最大
select * from @test a where c1=(select max(c1) from @Test where type=a.type)

--随机取
select * from @test a where c1=(select top 1 c1 from @Test where type=a.type order by newid())


dawugui 2007-10-17
  • 打赏
  • 举报
回复
和 如何用一條Sql語句篩選出不重復的記錄 用min也行.

select a.* from tb a,
(select id , min(name) name from tb group by id) b
where a.id = b.id and a.name = b.name

select a.* from tb a where name = (select min(name) from tb where id = a.id)
dawugui 2007-10-17
  • 打赏
  • 举报
回复
如何用一條Sql語句篩選出重復的記錄

select * from tb where id in (select id from tb group by id having count(*) > 1)

和 如何用一條Sql語句篩選出不重復的記錄

select a.* from tb a,
(select id , max(name) name from tb group by id) b
where a.id = b.id and a.name = b.name

select a.* from tb a where name = (select max(name) from tb where id = a.id)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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