Reputation: 125
I have the data in Table A like this as i/p
Col A | Col B | Col C
PG_1100000357_1100000356 | 1100000357 | 1100000356
PG_1100000356_1100000357 | 1100000356 | 1100000357
PG_10909099_12990909 | 10909099 | 12990909
PG_8989898_79797987 | 8989898 | 79797987
PG_8989898_79797987 | 8989898 | 79797987
I need to write a query to recieve the o/p as -
1) Remove the exact duplicates from the i/p when it matches with another record. (examples 4th & 5th record)
2) We need to consider concatenation of COl B
, COl C
to concatenation of Col c
, Col B
and remove that duplicate also. (1st and 2nd record)
note :- COl A
is arrived by CONTACT(PG_,Col B,'_',Col c) and dont worry about that
Col A | Col B | Col C
PG_1100000357_1100000356 | 1100000357 | 1100000356
PG_10909099_12990909 | 10909099 | 12990909
PG_8989898_79797987 | 8989898 | 79797987
Could you please help me? Thanks very much in advance.
Upvotes: 0
Views: 610
Reputation: 65433
It's not right to hold the same data in the multiple columns. The values of the Col_B
and Col_C
are already exist in Col_A
, you just need to split them, and then apply group by with least
and greatest
functions as @akk0rd87 suggested and considering the previous tag oracle
:
with Table_A(Col_A) as
(
select 'PG_1100000357_1100000356' from dual union all
select 'PG_1100000356_1100000357' from dual union all
select 'PG_10909099_12990909' from dual union all
select 'PG_8989898_79797987' from dual union all
select 'PG_8989898_79797987' from dual
), t as
(
select regexp_substr(Col_A, '[^_]+', 1, 1) col_one,
regexp_substr(Col_A, '[^_]+', 1, 2) col_two,
regexp_substr(Col_A, '[^_]+', 1, 3) col_three
from Table_A
)
select max(concat(concat(col_one||'-',least(col_two,col_three)||'-'),
greatest(col_two,col_three)))
as Col_A,
least(col_two,col_three) as Col_B, greatest(col_two,col_three) as Col_C
from t
group by least(col_two,col_three), greatest(col_two,col_three);
Upvotes: 1
Reputation: 1891
The below SQL query returns result as you expected
;WITH CTE AS( SELECT ColA, ColB, ColC,
ROW_NUMBER() OVER (PARTITION BY CASE WHEN ColB > ColC THEN ColB ELSE ColC END ORDER BY ColB) RN
FROM TableA )
SELECT ColA, ColB, ColC FROM CTE WHERE RN =1
Upvotes: 0
Reputation: 1288
--Oracle SQL: row_number().
--Least and Greatest functions will work regardless Col_B and Col_C have number or varchar2 data type
with s (Col_A, Col_B, Col_C) as (
select 'PG_1100000357_1100000356', 1100000357, 1100000356 from dual union all
select 'PG_1100000356_1100000357', 1100000356, 1100000357 from dual union all
select 'PG_10909099_12990909' , 10909099 , 12990909 from dual union all
select 'PG_8989898_79797987' , 8989898 , 79797987 from dual union all
select 'PG_8989898_79797987' , 8989898 , 79797987 from dual)
select Col_A, Col_B, Col_C
from
(select s.*,
row_number () over (partition by least(Col_B, Col_C), greatest(Col_B, Col_C) order by Col_B desc) rn
from s
)
where rn = 1;
COL_A COL_B COL_C
------------------------ ---------- ----------
PG_8989898_79797987 8989898 79797987
PG_10909099_12990909 10909099 12990909
PG_1100000357_1100000356 1100000357 1100000356
Upvotes: 3