SQL: 取消嵌套数组在 aws athena 中保持相同的行数

SQL: Unnest array keeping the same number of rows in aws athena

来自以下查询的基础

SELECT internal_transaction_id, tags FROM "bankstatements"."statements_transactions_sample_data"

我得到以下 table

+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| internal_transaction_id     | tags                                                                                                                                                                                                                                                                                                                                                                   |
+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2173059                     | [{category=null, creditdebit=credit, lendertype=null, pending=null, pre_authorisation=null, thirdparty=null}, {category=null, creditdebit=null, lendertype=null, pending=null, pre_authorisation=null, thirdparty=Internal Transfer Credit}, {category=Internal Transfer, creditdebit=null, lendertype=null, pending=null, pre_authorisation=null, thirdparty=null}]
+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2173061                     | [{category=null, creditdebit=credit, lendertype=null, pending=null, pre_authorisation=null, thirdparty=null}, {category=null, creditdebit=null, lendertype=null, pending=null, pre_authorisation=null, thirdparty=UBER}, {category=External Transfer, creditdebit=null, lendertype=null, pending=null, pre_authorisation=null, thirdparty=null}]
+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

我想取消嵌套“标签”列以保持相同的行数。目前我的查询

SELECT 
internal_transaction_id, t.category, t.creditdebit, t.lendertype, t.pending, t.pre_authorisation, t.thirdparty
FROM 
"bankstatements"."statements_transactions_sample_data"
CROSS JOIN UNNEST(tags) AS tag (t)  

有一个结果:

+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| internal_transaction_id     | category            | creditdebit   | lendertype    | pending   | pre_authorisation     | thirdparty                |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173059                     |                     | credit        |               |           |                       |                           |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173059                     |                     |               |               |           |                       | Internal Transfer Credit  |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173059                     | Internal Transfer   |               |               |           |                       |                           |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173061                     |                     | credit        |               |           |                       |                           |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173061                     |                     |               |               |           |                       | UBER                      |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173061                     | External Transfer   |               |               |           |                       |                           |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+

我想知道如何取消嵌套标签,使其只有 2 行,如下所示:

+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| internal_transaction_id     | category            | creditdebit   | lendertype    | pending   | pre_authorisation     | thirdparty                |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173059                     | Internal Transfer   | credit        |               |           |                       | Internal Transfer Credit  |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+
| 2173061                     | External Transfer   |               |               |           |                       | UBER                      |
+-----------------------------+---------------------+---------------+---------------+-----------+-----------------------+---------------------------+

JSON 中的 tags 标签如下所示:

[
    {
        "thirdParty": "Other Credits"
    },
    {
        "category": "All Other Credits"
    },
    {
        "creditDebit": "credit"
    }
]

当我定义创建时:

tags: array<
    struct<
        category: string,
        creditdebit: string,
        lendertype: string,
        pending: string,
        pre_authorisation: string,
        thirdparty: string                                            
    >
>

使用最小值或最大值聚合:

SELECT 
internal_transaction_id, max(t.category) as category, max(t.creditdebit) as creditdebit, max(t.lendertype) as lendertype, max(t.pending) as pending, max(t.pre_authorisation) as pre_authorisation, max(t.thirdparty) as thirdparty
FROM 
"bankstatements"."statements_transactions_sample_data"
CROSS JOIN UNNEST(tags) AS tag (t) 
GROUP BY internal_transaction_id