来自变体的横向扁平雪花 table
Lateral Flatten Snowflake from a Variant table
我有一个名为 raw_json
的变体 table,它包含多个 json 文件,这些文件对于 ID 是唯一的,但在每个 json 中包含相似的数据点。它们位于 jsontext 列中。这是上下文的 SS。我正在尝试将 raw_json
table 的每一行展平为正常的 table 视图。两个数组索引需要对齐才能分配正确的值。
这是 raw_json
table 中的两行以及 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"
}
],
}
}
变成这样的table(我需要这个)
+--------+----------+-------------+-----------+----------+-------------------+
| 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 数组展平为 table 并将正确的 ID 分配给 table。到目前为止,这是我的代码:在不散开 table 的情况下添加 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;
产生这个 table (我有这个)
+--------+-----------+----------+-------------------+
| 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 |
+--------+-----------+----------+-------------------+
所以大部分这个答案是将数据放入 CTE 的序言,但是 "iff" 两个数组的顺序是一致的,您可以只使用展平的索引来访问原始数组其他类型:
WITH raw_json AS (
select PARSE_json(column1) AS jsontext FROM VALUES
('{
"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"
}
],
}
}')
)
select
jsontext:ID::varchar as ID,
en.value:FirstName::varchar as FirstName,
en.value:LastName::varchar as LastName,
en.value:pxObjClass::varchar as pxObjClass,
jsontext:content.EEList[en.index].EEListID::text as EEListID,
jsontext:content.EEList[en.index].EEProductID::text as EEProductID
from raw_json,
lateral flatten (input => jsontext:content:EENotesList, outer => false) en;
这导致:
ID FIRSTNAME LASTNAME PXOBJCLASS EELISTID EEPRODUCTID
PO-103 John Smith XX-XXSales-Work-XX PO-103-1 XXX1976
PO-103 Bob Joe XX-XXSales-Work-XX PO-103-2 XXX1977
PO-103 Mike Smith XX-XXSales-Work-XX PO-103-3 XXX1978
PO-104 Sarah Butler XX-XXSales-Work-XX PO-104-1 XXX1979
PO-104 Jessica Adams XX-XXSales-Work-XX PO-104-2 XXX1980
我有一个名为 raw_json
的变体 table,它包含多个 json 文件,这些文件对于 ID 是唯一的,但在每个 json 中包含相似的数据点。它们位于 jsontext 列中。这是上下文的 SS。我正在尝试将 raw_json
table 的每一行展平为正常的 table 视图。两个数组索引需要对齐才能分配正确的值。
这是 raw_json
table 中的两行以及 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"
}
],
}
}
变成这样的table(我需要这个)
+--------+----------+-------------+-----------+----------+-------------------+
| 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 数组展平为 table 并将正确的 ID 分配给 table。到目前为止,这是我的代码:在不散开 table 的情况下添加 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;
产生这个 table (我有这个)
+--------+-----------+----------+-------------------+
| 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 |
+--------+-----------+----------+-------------------+
所以大部分这个答案是将数据放入 CTE 的序言,但是 "iff" 两个数组的顺序是一致的,您可以只使用展平的索引来访问原始数组其他类型:
WITH raw_json AS (
select PARSE_json(column1) AS jsontext FROM VALUES
('{
"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"
}
],
}
}')
)
select
jsontext:ID::varchar as ID,
en.value:FirstName::varchar as FirstName,
en.value:LastName::varchar as LastName,
en.value:pxObjClass::varchar as pxObjClass,
jsontext:content.EEList[en.index].EEListID::text as EEListID,
jsontext:content.EEList[en.index].EEProductID::text as EEProductID
from raw_json,
lateral flatten (input => jsontext:content:EENotesList, outer => false) en;
这导致:
ID FIRSTNAME LASTNAME PXOBJCLASS EELISTID EEPRODUCTID
PO-103 John Smith XX-XXSales-Work-XX PO-103-1 XXX1976
PO-103 Bob Joe XX-XXSales-Work-XX PO-103-2 XXX1977
PO-103 Mike Smith XX-XXSales-Work-XX PO-103-3 XXX1978
PO-104 Sarah Butler XX-XXSales-Work-XX PO-104-1 XXX1979
PO-104 Jessica Adams XX-XXSales-Work-XX PO-104-2 XXX1980