2012-03-01 58 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; 

以防止數據損壞再次發生(...修復泄漏):

​​