我有一个名为 的变raw_json
体表,其中包含多个 json 文件,这些文件对于 ID 是唯一的,但在每个 json 中包含相似的数据点。它们位于 jsontext 列中。这是上下文的SS。我正在尝试将表格的每一行展平raw_json
为普通的表格视图。两个数组索引需要对齐以分配正确的值。
这是表中的两行raw_json
以及 json 的结构。
{
"ID": "PO-103",
"content": {
"EEList": [
{
"EEListID": "PO-103-1",
"EEProductID": "XXX1976",
},
{
"EEListID": "PO-103-2",
"EEProductID": "XXX1977",
},
{
"EEListID": "PO-103-3",
"EEProductID": "XXX1978",
}
],
"EENotesList": [
{
"FirstName": "John",
"LastName": "Smith",
"pxObjClass": "XX-XXSales-Work-XX"
},
{
"FirstName": "Bob",
"LastName": "Joe",
"pxObjClass": "XX-XXSales-Work-XX"
},
{
"FirstName": "Mike",
"LastName": "Smith",
"pxObjClass": "XX-XXSales-Work-XX"
}
],
}
}
{
"ID": "PO-104",
"content": {
"EEList": [
{
"EEListID": "PO-104-1",
"EEProductID": "XXX1979",
},
{
"EEListID": "PO-104-2",
"EEProductID": "XXX1980",
},
{
"EEListID": "PO-104-3",
"EEProductID": "XXX1981",
}
],
"EENotesList": [
{
"FirstName": "Sarah",
"LastName": "Butler",
"pxObjClass": "XX-XXSales-Work-XX"
},
{
"FirstName": "Jessica",
"LastName": "Adams",
"pxObjClass": "XX-XXSales-Work-XX"
}
],
}
}
变成这样的表(我需要这个)
+--------+----------+-------------+-----------+----------+-------------------+
| ID | EEListID | EEProductID | FirstName | LastName | pxObjClass |
+--------+----------+-------------+-----------+----------+-------------------+
| PO-103 | PO-103-1 | XXX1976 | John | Smith | X-XXSales-Work-XX |
| PO-103 | PO-103-2 | XXX1977 | Bob | Joe | X-XXSales-Work-XX |
| PO-103 | PO-103-3 | XXX1978 | Mike | Smith | X-XXSales-Work-XX |
| PO-104 | PO-104-1 | XXX1979 | Sarah | Butler | X-XXSales-Work-XX |
| PO-104 | PO-104-2 | XXX1980 | Jessica | Adams | X-XXSales-Work-XX |
+--------+----------+-------------+-----------+----------+-------------------+
我已经能够将 EENoteList 数组展平为一个表并将正确的 ID 分配给该表。到目前为止,这是我的代码:在不展开表格的情况下添加 EEList 值是我出错的地方。
select
jsontext:ID::varchar as ID,
en.value:FirstName::varchar as FirstName,
en.value:LastName::varchar as LastName,
en.value:pxObjClass::varchar as pxObjClass
-- concat(ID, EEProductID, FirstName, LastName)
from raw_json,
lateral flatten (input => jsontext:content:EENotesList, outer => false) en;
产生这张桌子(我有这个)
+--------+-----------+----------+-------------------+
| ID | FirstName | LastName | pxObjClass |
+--------+-----------+----------+-------------------+
| PO-103 | John | Smith | X-XXSales-Work-XX |
| PO-103 | Bob | Joe | X-XXSales-Work-XX |
| PO-103 | Mike | Smith | X-XXSales-Work-XX |
| PO-104 | Sarah | Butler | X-XXSales-Work-XX |
| PO-104 | Jessica | Adams | X-XXSales-Work-XX |
| PO-104 | Terrence | Williams | X-XXSales-Work-XX |
+--------+-----------+----------+-------------------+