2017-02-09 46 views
-1

重複的結果我已經有通過查詢SQL中產生兩列如下表:如何刪除SQL

Lookup Value Result 
1    2 
2    1 
4    3 
3    4 

正如你可以看到它包含重複的結果。我只想讓它顯示第一行和第三行。有誰知道如何在SQL中做到這一點?

謝謝

+0

和你有什麼試過嗎? –

+0

可能的重複[如何刪除重複的行?](http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows) –

+8

有沒有重複那裏... – jarlh

回答

2

有幾種方法。下面是一個使用union all

select t.* 
from t 
where col1 < col2 
union all 
select t1.* 
from t1 
where col1 > col2 and 
     not exists (select 1 from t t2 where t1.col1 = t2.col2 and t1.col2 = t2.col1); 

如果你總是知道存在兩對(作爲示例數據),你可以使用:

select t.* 
from t 
where col1 < col2; 
1
SELECT DISTINCT 
     CASE WHEN Lookup Value < Result 
      THEN Lookup Value 
      ELSE Result 
     END as first, 
     CASE WHEN Lookup Value < Result 
      THEN Result 
      ELSE Lookup Value 
     END as second 
FROM YourTable 
+0

胡安..這可能會產生不在表格中的行,當沒有對稱重複時。例如:(6,4)可能會出現爲(4,6) –

+1

@vkp,您是完全正確的。但歐普說,沒有特別的理由,他希望'(1,2)'超過'(2,1)',而'(4,3)'超過'(3,4)''。所以我的猜測是元組順序無關緊要,就像多米諾骨牌。 –

0
Create Table T (
[Lookup Value] int, 
Result int 
) 

Insert into T values (1,2),(2,1),(4,3),(3,4) 

Select distinct T.[Lookup Value], T.Result 
From T 
where T.[Lookup Value]<=T.Result