来源:
TANKLISTUS TANKLISTCANADA TANKLISTINDIA
T111||T222 T444||T222 T555
T111||T222 T444||T111 T555
T111||T666 T444||T222 T555
代码:
WITH src AS
(
SELECT 'T111||T222' TankListUS, 'T444||T222' TankListCanada, 'T555' TankListIndia FROM DUAL UNION ALL
SELECT 'T111||T222' TankListUS, 'T444||T111' TankListCanada, 'T555' TankListIndia FROM DUAL UNION ALL
SELECT 'T111||T666' TankListUS, 'T444||T222' TankListCanada, 'T555' TankListIndia FROM DUAL
)
, step1 AS
(
SELECT SUBSTR(TankListUS, 1, 4) AS us1
, SUBSTR(TankListUS, 7) AS us2
, SUBSTR(TankListCanada, 1, 4) AS ca1
, SUBSTR(TankListCanada, 7) AS ca2
, TankListIndia AS in1
, ROWNUM AS r_id
FROM src
)
, step2 AS
(
SELECT us1 AS r_value, r_id FROM step1 UNION
SELECT us2, r_id FROM step1 UNION
SELECT ca1, r_id FROM step1 UNION
SELECT ca2, r_id FROM step1 UNION
SELECT in1, r_id FROM step1
)
,step3 AS
(
SELECT r_value
, LEAD(r_value, 1) OVER (PARTITION BY r_id ORDER BY r_value) AS lead1
, LEAD(r_value, 2) OVER (PARTITION BY r_id ORDER BY r_value) AS lead2
, LEAD(r_value, 3) OVER (PARTITION BY r_id ORDER BY r_value) AS lead3
, LEAD(r_value, 4) OVER (PARTITION BY r_id ORDER BY r_value) AS lead4
, ROW_NUMBER() OVER (PARTITION BY r_id ORDER BY r_value) AS r_num
FROM step2
)
,step4 AS
(
SELECT r_value
|| NVL2(lead1, '||' || lead1, lead1)
|| NVL2(lead2, '||' || lead2, lead2)
|| NVL2(lead3, '||' || lead3, lead3)
|| NVL2(lead4, '||' || lead4, lead4) AS the_result
FROM step3
WHERE r_num = 1
)
-- OR:
SELECT DISTINCT the_result AS the_result_with_DISTINCT
FROM step4
结果:
T111||T222||T444||T555||T666
T111||T222||T444||T555