![](/img/trans.png)
[英]athena array_agg sort by multiple values results in incorrect order
[英]Preserve order of array elements after unnest and array_agg in AWS Athena / Presto
我有一個 Athena 查詢,它取消嵌套一個數組,將元素與另一個表連接起來,然后使用array_agg
將它們收集回一個數組中。 我想保留元素的原始順序,但 Athena 不支持 Presto 功能向array_agg
添加ORDER BY
子句。 如何保留元素的順序?
查詢類似於此示例:
SELECT x, array_agg(b) bs
FROM table1 -- table1 columns are x, ys
CROSS JOIN UNNEST(ys) AS t(y)
JOIN table2 ON y=a -- table2 columns are a, b
GROUP BY x
bs
中ys
的順序?有同樣的問題,我的解決方法是聚合值以將序數作為鍵進行映射。 然后將映射的值轉換為數組(按鍵排序):
SELECT t1.x,
transform(sequence(1,cardinality(map_agg(n,y)),(x)-> map_agg(n,y)[x])
FROM table1 t1 CROSS JOIN
UNNEST(t1.ys) WITH ORDINALITY AS t(y, n) JOIN
table2 t2
ON t1.y = t2.a
GROUP BY t1.x;
Presto 支持with ordinality
unnest()
:
SELECT t1.x, array_agg(t2.b ORDER BY n) bs
FROM table1 t1 CROSS JOIN
UNNEST(t1.ys) WITH ORDINALITY AS t(y, n) JOIN
table2 t2
ON t1.y = t2.a
GROUP BY t1.x;
請注意,您不需要注釋來指定列的來源。 只需使用適當的表別名質量列引用。 這也使查詢明確且易於理解。
一種在沒有ORDER BY
情況下保留ORDER BY
:
SELECT
x
-- prefix each "b" with a 19-digit sortable string
-- aggregate into array, sort, then remove the prefix
, TRANSFORM(
ARRAY_SORT(
array_agg(
SUBSTR(LPAD(CAST(yi AS VARCHAR), 19, '0'), -19)
|| b))
, e1 -> SUBSTR(e1, 1 + 19)
) AS bs
FROM table1 -- table1 columns are x, ys
CROSS JOIN UNNEST(ys) WITH ORDINALITY t (y, yi)
JOIN table2 ON y = a -- table2 columns are a, b
GROUP BY x
該方法假設:
b
是字符數據(或易於轉換)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.