Sql 如何在蜂箱中进行字符串浓缩

Sql 如何在蜂箱中进行字符串浓缩,sql,arrays,hive,concatenation,hiveql,Sql,Arrays,Hive,Concatenation,Hiveql,我有两张表,让我们假设表1如下 表2如下: 我期望的结果是 我在hive中尝试了concat_ws方法,但结果不符合预期,而且当我使用concat_ws方法时,我只能应用于1个字段。请告诉我如何克服或是否有任何可用的解决方案。使用collect\u set获取每个(name,function)的零件数组,然后使用concat\u ws以逗号作为分隔符连接数组: select t.name1, t1.function, t2.parts, t2.body, t1.scope from (se

我有两张表,让我们假设表1如下

表2如下:

我期望的结果是


我在hive中尝试了concat_ws方法,但结果不符合预期,而且当我使用concat_ws方法时,我只能应用于1个字段。请告诉我如何克服或是否有任何可用的解决方案。

使用
collect\u set
获取每个(
name,function
)的零件数组,然后使用
concat\u ws
以逗号作为分隔符连接数组:

select t.name1, t1.function, t2.parts, t2.body, t1.scope
from
(select name1, function,
        concat_ws(',',collect_set(scope)) as scope
   from table1
  group by name1, function
) t1
 inner join
(select name2, function,
        concat_ws(',',collect_set(parts)) as parts,
        concat_ws(',',collect_set(Body)) as Body
   from table2 t2 
  group by name2, function
)t2
 on t1.name1=t2.name2 and t1.function=t2.function 
还有一种方法-如果没有连接,使用UNION ALL+聚合,这可能会更有效:

select name, function,
       concat_ws(',',collect_set(parts)) as parts,
       concat_ws(',',collect_set(Body)) as Body,
       concat_ws(',',collect_set(scope)) as scope
from
(
select name1 as name, function, null as parts, null as Body, scope from table1
UNION ALL
select name2 as name, function, parts, Body, null as scope from table2
)s
group by name, function

使用
collect\u set
获取每个部分的数组(
name,function
),然后使用
concat\u ws
以逗号作为分隔符连接数组:

select t.name1, t1.function, t2.parts, t2.body, t1.scope
from
(select name1, function,
        concat_ws(',',collect_set(scope)) as scope
   from table1
  group by name1, function
) t1
 inner join
(select name2, function,
        concat_ws(',',collect_set(parts)) as parts,
        concat_ws(',',collect_set(Body)) as Body
   from table2 t2 
  group by name2, function
)t2
 on t1.name1=t2.name2 and t1.function=t2.function 
还有一种方法-如果没有连接,使用UNION ALL+聚合,这可能会更有效:

select name, function,
       concat_ws(',',collect_set(parts)) as parts,
       concat_ws(',',collect_set(Body)) as Body,
       concat_ws(',',collect_set(scope)) as scope
from
(
select name1 as name, function, null as parts, null as Body, scope from table1
UNION ALL
select name2 as name, function, parts, Body, null as scope from table2
)s
group by name, function

我需要将表1中的范围添加到最终结果。我需要将表1中的范围添加到最终结果。