将配置单元查询转换为雪花

w41d8nur  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(425)

我们想将下面的查询更改为使用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

提一下 "\"" 已替换为 $$"$$ .
但我没有找到合适的替代品 sort_array 功能。
有人能帮忙吗?

gt0wga4j

gt0wga4j1#

您是否尝试过使用array\u agg而不是collect\u list?
https://docs.snowflake.com/en/sql-reference/functions/array_agg.html
类似于(因此它也对数组进行排序):

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

r1zhe5dt2#

listag聚合函数返回用分隔符连接的字符串,类似于concat\u ws。可与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) )

相关问题