求去重复值SQL

felix3118 2010-10-25 10:49:12
现有数据
c2 c1 uid uid2
3 4 2 1
NULL 3 3 1
3 4 1 2
2 3 3 2
NULL 3 1 3
2 3 2 3

如何写SQL语句实现过滤UID,UID2相同的数据
最后结果为:

c2 c1 uid uid2
3 4 1 2
NULL 3 1 3
2 3 2 3
...全文
61 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
水族杰纶 2010-10-25
  • 打赏
  • 举报
回复
if not object_id('tb') is null
drop table tb
Go
Create table tb([c2] int,[c1] int,[uid] int,[uid2] int)
Insert tb
select 3,4,2,1 union all
select null,3,3,1 union all
select 3,4,1,2 union all
select 2,3,3,2 union all
select null,3,1,3 union all
select 2,3,2,3
Go
select c2,
c1,
min([Uid])[Uid],
max([Uid])[Uid2]
from(
Select [c2],[c1],[Uid] from tb
union
Select [c2],[c1],[Uid2] from tb
)t
group by c2,c1
/*
c2 c1 Uid Uid2
----------- ----------- ----------- -----------
NULL 3 1 3
2 3 2 3
3 4 1 2
*/
「已注销」 2010-10-25
  • 打赏
  • 举报
回复
怎么过滤的?没看懂。
dawugui 2010-10-25
  • 打赏
  • 举报
回复
create table tb(c2 int,c1 int,uid int,uid2 int)
insert into tb values(3 ,4 ,2 ,1)
insert into tb values(NULL ,3 ,3 ,1)
insert into tb values(3 ,4 ,1 ,2)
insert into tb values(2 ,3 ,3 ,2)
insert into tb values(NULL ,3 ,1 ,3)
insert into tb values(2 ,3 ,2 ,3)

go

select m.* from tb m, tb n where m.uid = n.uid2 and m.uid2 = n.uid and m.uid < m.uid2

drop table tb

/*
c2 c1 uid uid2
----------- ----------- ----------- -----------
3 4 1 2
NULL 3 1 3
2 3 2 3

(所影响的行数为 3 行)
*/
dawugui 2010-10-25
  • 打赏
  • 举报
回复
select m.* from tb m, tb n where m.uid = n.uid2 and m.uid2 = n.uid and m.uid < n.uid2

22,207

社区成员

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

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