2012-03-01 61 views
-2
one table a= {el1, el2} 
where el1 is 1..10, el2 is {5..15}. 

内表中的记录都像[el1, el2] ,还有一些记录[el2, el1],即它们是相同的,只是在不同的collumns。找到唯一的记录表

获取独特的el1,el2元素的最佳方式是什么? (记录1,2与2,1相同)

回答

1

我确定有一个更优雅的解决方案,但现在我无法想到它。第一部分查找行,如果您颠倒了列,则无法找到匹配项。所述第二查找行,其中如果反向的列可以找到一个匹配 - 和处理的是[EL1,EL2]对具有在每列

select t1.el1, t1.el2 
from @tbl t1 
where not exists (select * from @tbl t2 where t2.el1 = t1.el2 and t2.el2 = t1.el1) 

union 

select t1.el1, t1.el2 
from @tbl t1 
where exists (select * from @tbl t2 where t2.el1 = t1.el2 and t2.el2 = t1.el1 and t2.el1 <= t1.el1) 
+0

我想出了一个有点不同的解决方案,做了一个工会,即表1(一,b)table2(a,b)从t1中选择a,b,其中a 2012-03-01 18:57:19

+0

那么为什么不发布它来帮助别人在未来? – kaj 2012-03-01 18:58:58

0

相同的值要清理的数据(擦地板。 ..):

SELECT el1, el2 
    FROM YourTable 
UNION 
SELECT el2 AS el1, el1 AS el2 
    FROM YourTable; 

以防止数据损坏再次发生(...修复泄漏):

​​