删除重复的串联列

时间:2019-04-12 02:31:44

标签: sql hive hiveql

我在表A中将数据存储为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  

我需要编写一个查询以将o / p接收为-

1)当i / p与另一条记录匹配时,从i / p中删除确切的重复项。 (例如第4和第5条记录) 2)我们需要考虑将COl BCOl C串联到Col cCol B的串联中,并删除该重复项。 (第一和第二条记录) 注意:-COl A是由CONTACT(PG_,Col B,'_',Col c)到达的,不必担心

    Col A                      |    Col B    |    Col C
PG_1100000357_1100000356   |  1100000357 |    1100000356 
PG_10909099_12990909       |  10909099   |    12990909
PG_8989898_79797987        |  8989898    |    79797987   

能帮我吗?非常感谢。

3 个答案:

答案 0 :(得分:3)

--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

答案 1 :(得分:1)

在多列中保存相同的数据是不正确的。 Col_BCol_C的值已经存在于Col_A中,您只需要拆分它们,然后通过leastgreatest函数应用group by如@ akk0rd87建议,并考虑先前的标签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);

Demo

答案 2 :(得分:0)

下面的SQL查询返回预期的结果

 ;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