支持greenplum string_agg 转换成hivesql

Posted

技术标签:

【中文标题】支持greenplum string_agg 转换成hivesql【英文标题】:greenplum string_agg conversion into hivesql supported 【发布时间】:2021-09-24 03:26:05 【问题描述】:

我们正在将 greenplum sql 查询迁移到 hivesql,请在下面找到可用的语句 string_agg。我们如何迁移,请帮助我们。下面是迁移 hive 所需的示例 greenplum 代码。

select string_agg(Display_String, ';' order by data_day )

select string_agg(Display_String, ';' order by data_day )
from
(
select data_day, 
sum(revenue)/1000000.00 as revenue,
data_day||' '||trim(to_char(sum(revenue),'9,999,999,999')) as Display_String 
from(
select case when data_date = current_date  then 'D:'
when data_date = current_date - 1 then ' D-01:'
when data_date = current_date - 2 then ' D-02:'
when data_date = current_date - 7 then ' D-07:'
when data_date = current_date - 28 then ' D-28:'
end data_day, revenue/1000000.00 revenue
from test.testable
where data_date between current_date - 28 and current_date and hour <=(Select hour from ( select row_number() over(order by hour desc) iRowsID, hour from test.testable where data_date = current_date and  type = 'UVC')tbl1
where irowsid = 2) and type in( 'UVC')
order by 1 desc) a
group by 1)aa;

【问题讨论】:

【参考方案1】:

在蜂巢中没有这样的东西。但是你可以使用collect listpartition by/Order by 来计算它。

select concat_ws(';', max(concat_str)) 
FROM (
SELECT collect_list(Display_String) over (order by data_day ) concat_str 
FROM 
(your above SQL) s ) concat_qry)r

解释-

    收集列表连接字符串,并在执行此操作时按日期列上的订单数据排序。 最外层的 MAX() 将获取连接字符串的最大数据。

请注意这是一个非常缓慢的操作。在实施之前也要测试性能。

这里有一个示例 SQL 和结果可以帮助您。

select
  id, concat_ws(';', max(concat_str)) 
from
  ( select
      s.id, collect_list(s.c) over (partition by s.id  order by s.c  ) concat_str 
    from
      ( select 1 id,'ax' c union 
      select 1,'b' 
      union select 2,'f' 
      union select 2,'g' 
      union all select 1,'b' 
      union all select 1,'b'  )s
  )  gs  
group by  id 

【讨论】:

我关注但查询不支持配置单元中的子查询 select concat_ws(';', max(concat_str)) FROM (SELECT collect_list(Display_String) over (order by data_day) concat_str FROM (select data_day, sum(revenue)/1000000.00 作为收入,data_day||' '||trim(regexp_replace(sum(revenue), '(?!^)(\\d\\d \\d(?=(\\d\\d\\d)*\\b))', ',$1')) as Display_String from(select case when data_date = current_date then 'D:' when data_date = date_sub (current_date,1) 然后 'D-01:' 当 data_date = date_sub(current_date,2) 然后 'D-02:' 当 data_date = date_sub(current_date,7) 然后 'D-07:' 当 data_date = date_sub(current_date ,28) 然后 'D-28:' 结束 data_day, 收入/1000000.00 收入来自 test.tablename 其中 data_date 在 date_sub(current_date,28) 和 current_date 和小时之间 trim(to_char(sum(revenue),'9,999,999,999')) as Display_String --- 改为 -->trim(regexp_replace(sum(revenue), '(?!^)(\\ d\\d\\d(?=(\\d\\d\\d)*\\b))', ',$1'))

以上是关于支持greenplum string_agg 转换成hivesql的主要内容,如果未能解决你的问题,请参考以下文章

GreenPlum之按月份或季度实现行转列

将 T-SQL string_agg 转换为 LINQ C#

计算从 Greenplum 到 hive 迁移的整数计算能力

MS SQL Server的STRING_SPLIT和STRING_AGG函数

GreenPlum SQL 语句迁移到 HiveSQL

在 Talend 中将所有 MYSQL 组件转换为 Greenplum 组件