使用marcothesane與樞軸/ unpivot的運營理念。如果更多輸入列應該重複數據刪除,則更容易維護。這維護了源數據(列對)的順序 - 而marcothesane的解決方案可能會根據輸入數據對列對進行重新排序。它也比marcothesane慢一點。它只能在11R1以上運行。
WITH
input(c1,c2,c3,c4,c5,c6) AS (
SELECT 1, 2,1, 2,1, 3 from dual
UNION ALL SELECT 1, 2,1, 3,1, 4 from dual
UNION ALL SELECT 1,NULL ,1,NULL ,1,NULL from dual
)
,
-- need rowid
input_with_rowid AS (
SELECT ROW_NUMBER() OVER (order by 1) AS row_id, input.* FROM input
),
unpivoted_pairs as
(
select row_id, tuple_idx, val1, val2, row_number() over (partition by row_id, val1, val2 order by tuple_idx) as keep_first
from input_with_rowid
UnPivot include nulls(
(val1, val2) --measure
for tuple_idx in ((c1,c2) as 1,
(c3,c4) as 2,
(c5,c6) as 3)
)
)
select row_id,
t1_val1 as c1,
t1_val2 as c2,
t2_val1 as c3,
t2_val2 as c4,
t3_val1 as c5,
t3_val2 as c6
from (
select row_id,
val1, val2, row_number() over (partition by row_id order by tuple_idx) as tuple_order
from unpivoted_pairs
where keep_first = 1
)
pivot (sum(val1) as val1, sum(val2) as val2
for tuple_order in ('1' as t1, '2' as t2, '3' as t3)
)
你的問題不是很精確。你究竟需要什麼?你的例子可以用很多方式來解釋,你的描述類型自相矛盾。 「刪除一行中存在的重複值」與「兩列的組合應該在一行中唯一」不同。另外,哪些組合應該是唯一的? – m00hk00h
更新了問題! – Biswabid
是否所有的列都來自同一個表?如何查詢你的查詢 –