如何在presto中将行转换为Map?

zfciruhq  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(509)

考虑sql语句:

SELECT key, value
FROM (
  SELECT key, value, row_number() OVER (PARTITION BY key ORDER BY length(value) DESC) AS rn
  FROM my_table
)
WHERE rn <= 5;

这将产生:

key value
A   1
A   2
B   10

如何使其成为:

key values
A   [1;2;3;4;5]
B   [10;20;30;40;50]

sql引擎是presto。有什么想法吗?

oipij1gg

oipij1gg1#

map_agg 解决方案是:

SELECT key, map_agg(key, value)
FROM (
  SELECT key, value, row_number() OVER (PARTITION BY key ORDER BY length(value) DESC) AS rn
  FROM my_table
)
WHERE rn <= 5 group by key;

非常感谢,@shantanu kher最快的回复!

相关问题