删除重复“对比”的字段。

zhuruijay 2008-03-09 02:52:44
test 表
一个字段 name
记录
a
b
c
d

查询不重复对比的次数
a b
a c
a d
b c
b d
c d
应该怎么去掉ab ba这样的重复对比?
...全文
67 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Limpire 2008-03-09
  • 打赏
  • 举报
回复
--> 测试数据: @T
declare @T table (name varchar(1))
insert into @T
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'

select * from @T a cross join @T b where a.name<b.name order by 1

/*
a b
a c
a d
b c
b d
c d
*/
zbc1009 2008-03-09
  • 打赏
  • 举报
回复

create table test(a char(1))
go
insert into test values('a')
go
insert into test values('b')
go
insert into test values('c')
go
insert into test values('d')
go

select distinct
(case when t1.a<=t2.a then t1.a else t2.a end) as col1,
(case when t1.a>t2.a then t1.a else t2.a end) as col2
from test t1 cross join test t2
where t1.a<>t2.a
dawugui 2008-03-09
  • 打赏
  • 举报
回复
create table tb(name varchar(10))
insert into tb values('a')
insert into tb values('b')
insert into tb values('c')
insert into tb values('d')
go

select m.name , n.name from tb m , tb n where m.name < n.name order by m.name

drop table tb

/*
name name
---------- ----------
a b
a c
a d
b c
b d
c d
(所影响的行数为 6 行)
*/
kelph 2008-03-09
  • 打赏
  • 举报
回复
a    a    
---- ----
a b
a c
b c
a d
b d
c d

(所影响的行数为 6 行)
kelph 2008-03-09
  • 打赏
  • 举报
回复
create table a(a char(1))
insert a select 'a' union all select 'b' union all select 'c' union all select 'd'
select * from a,a t where a.a<t.a

22,207

社区成员

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

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