简体   繁体   English

将 Hive 查询转换为 Snowflake

[英]Convert Hive query to Snowflake

we want to change our the following query to work with Snowflake.我们想更改以下查询以使用 Snowflake。

INSERT OVERWRITE INTO {events_scoring}.profile_to_json_with_classified_campaigns_results
SELECT to_json.ksname,
       to_json.cust_profile_id,
       concat("{{",concat_ws('$',sort_array(collect_list(concat($$"$$,cast(to_json.portfolio_type AS STRING),$$":"$$, cast(to_json.counter AS STRING),$$"$$)))),'}}') AS json_of_campaign_counters_according_to_portfolio_types
FROM
  (SELECT final.ksname,
          final.cust_profile_id,
          final.portfolio_type_final AS portfolio_type,
          count(*) AS counter
   FROM {events_scoring}.campaign_to_portfolio_type FINAL
   GROUP BY final.ksname,
            final.cust_profile_id,
            final.portfolio_type_final
            ORDER BY portfolio_type) to_json
GROUP BY to_json.ksname,
         to_json.cust_profile_id

mention that "\\"" already replaced with $$"$$ .提到"\\""已经替换为$$"$$

But I didn't find a suitable replacement to sort_array function.但是我没有找到sort_array函数的合适替代品。

Can anyone help?任何人都可以帮忙吗?

Have you tried to use ARRAY_AGG instead of collect_list?您是否尝试过使用 ARRAY_AGG 而不是 collect_list?

https://docs.snowflake.com/en/sql-reference/functions/array_agg.html https://docs.snowflake.com/en/sql-reference/functions/array_agg.html

Something like (so it also sorts the array):类似的东西(所以它也对数组进行排序):

select array_agg(X) within group (order by X asc) from your_table;

listagg aggregation function returns string concatenated with delimiter, similar to the concat_ws. listagg聚合函数返回用分隔符连接的字符串,类似于 concat_ws。 Can be used with group by or with over().可以与 group by 或 over() 一起使用。

select listagg(concat($$"$$,cast(to_json.portfolio_type AS STRING),$$":"$$, cast(to_json.counter AS STRING),$$"$$), "$") within group (ORDER BY cast(to_json.portfolio_type AS STRING), cast(to_json.counter AS STRING) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM