簡體   English   中英

雪花從表數據創建 json 結構

[英]snowflake creating json structure from table data

我有一個視圖,它以以下格式返回數據,除了我希望將行值加載到 JSON 結構中的新列之外,我還希望將其 UPSERTED 轉換到表中。 JSON 結構需要基於 ID 和 NAME 列形成,並且 rest 需要在 JSON 內部進行數組。

示例源數據在此處輸入圖像描述

我希望將值加載到與上述類似的目標表中,還需要通過對 ID 和 NAME 列進行分組來形成 JSON 結構化值,並加載到 JSON 列中,該列是變體類型。

樣本目標表

在此處輸入圖像描述

需要為 JSON1、JSON2 和 JSON 3 填充示例 JSON 列值如下。 非常感謝任何有助於實現預期結果的幫助。 謝謝

JSON1

[
    {
        "col1": "1",
        "col2": "TEST001",
        "war_detail":[
                        {   "War_id": "WR001",
                            "War_start_date": "1/1/1970",
                            "War_end_date": "12/12/9999"
                        }
                    ],
        "Con_details": [
                            {   "Cont_id": "CON001",
                                "Con_start_date": "1/1/1970",
                                "Con_end_date": "12/12/9999"
                            },
                            {   "Cont_id": "CON002",
                                "Con_start_date": "1/1/2000",
                                "Con_end_date": "12/12/9999"
                            }
                        ]
    },
    {
        "col1": "9",
        "col2": "TEST001",
        "war_detail":[
                        {   "War_id": "WR001",
                            "War_start_date": "1/1/1970",
                            "War_end_date": "12/12/9999"
                        }
                    ],
        "Con_details": [
                            {   "Cont_id": "CON123",
                                "Con_start_date": "1/1/2010",
                                "Con_end_date": "12/12/9999"
                            }
                        ]
    },
    
    
]

---------------------------------------
JSON2

[
    JSON:{
            "col1": "2",
            "col2": "TEST002",
            "war_detail":[
                            {   "War_id": "WR987",
                                "War_start_date": "1/1/1970",
                                "War_end_date": "12/12/9999"
                            },
                            {   "War_id": "WR123",
                                "War_start_date": "1/1/1990",
                                "War_end_date": "12/12/9999"
                            }
                        ],
            "Con_details": [
                                {   "Cont_id": "CON003",
                                    "Con_start_date": "1/1/2020",
                                    "Con_end_date": "12/12/9999"
                                }
                            ]
        }
]
---------------------------------------
JSON3

[
    JSON:{
            "col1": "2",
            "col2": "TEST002",
            "war_detail":[
                            {   "War_id": "WR678",
                                "War_start_date": "1/1/2001",
                                "War_end_date": "12/12/2023"
                            },
                            {   "War_id": "WR004",
                                "War_start_date": "1/1/2010",
                                "War_end_date": "12/12/2030"
                            }
                        ],
            "Con_details": []
        }
]

因此,首先,圖片很有幫助,但實際上,其中包含值的文本塊使得將值復制到 SQL 比手動輸入要好得多。

但是對數據使用 CTE:

WITH data AS (
    SELECT * FROM VALUES
        (1, 'test001', 'WR001', '1970-01-01', '9999-12-12', 'CON001', '1970-01-01', '9999-12/12'),
        (1, 'test001', 'WR001', '1970-01-01', '9999-12-12', 'CON002', '1970-01-01', '9999-12/12'),
        (9, 'test001', 'WR001', '1970-01-01', '9999-12-12', 'CON123', '1970-01-01', '9999-12/12'),
        (2, 'test002', 'WR987', '2020-01-01', '9999-12-12', 'CON003', '1970-01-01', '9999-12/12'),
        (2, 'test002', 'WR123', '1990-01-01', '9999-12-12', 'CON003', '1970-01-01', '9999-12/12'),
        (3, 'test003', 'WR678', '2001-01-01', '2023-12-12', null, null, null),
        (3, 'test003', 'WR004', '2010-01-01', '2030-12-12', null, null, null)
        v(id, name, war_id, war_start_date, war_end_date, cont_id, con_start_date, con_end_date)
)

假設你的JSON1JSON2JSON3是有效的NAME然后

我們可以嵌套一些OBJECT_CONSTRUCTARRAY_AGG來按預期構建數據。

SELECT 
    array_agg(war_block) WITHIN GROUP (ORDER BY war_block:col1) as json_block
FROM (
    SELECT name,
        object_construct('col1', id, 'col2', name, 'war_detail', a_war, 'con_details', a_con) as war_block
    FROM (
        SELECT id
            ,name
            ,array_agg(distinct war) WITHIN GROUP (ORDER BY war:war_start_date) AS a_war
            ,array_agg(distinct con) WITHIN GROUP (ORDER BY con:con_start_date) AS a_con
        FROM (
            SELECT id
                ,name
                ,object_construct('war_id', war_id, 'war_start_date', war_start_date, 'war_end_date', war_end_date) as war
                ,object_construct('cont_id', cont_id, 'con_start_date', con_start_date, 'con_end_date', con_end_date) as con
            FROM data
        )
        GROUP BY id, name
    )
)
GROUP BY name
ORDER BY name;

這使:

JSON_BLOCK
[    {      "col1": 1,      "col2": "test001",      "con_details": [        {          "con_end_date": "9999-12/12",          "con_start_date": "1970-01-01",          "cont_id": "CON001"        },        {          "con_end_date": "9999-12/12",          "con_start_date": "1970-01-01",          "cont_id": "CON002"        }      ],      "war_detail": [        {          "war_end_date": "9999-12-12",          "war_id": "WR001",          "war_start_date": "1970-01-01"        }      ]    },    {      "col1": 9,      "col2": "test001",      "con_details": [        {          "con_end_date": "9999-12/12",          "con_start_date": "1970-01-01",          "cont_id": "CON123"        }      ],      "war_detail": [        {          "war_end_date": "9999-12-12",          "war_id": "WR001",          "war_start_date": "1970-01-01"        }      ]    }  ]
[    {      "col1": 2,      "col2": "test002",      "con_details": [        {          "con_end_date": "9999-12/12",          "con_start_date": "1970-01-01",          "cont_id": "CON003"        }      ],      "war_detail": [        {          "war_end_date": "9999-12-12",          "war_id": "WR123",          "war_start_date": "1990-01-01"        },        {          "war_end_date": "9999-12-12",          "war_id": "WR987",          "war_start_date": "2020-01-01"        }      ]    }  ]
[    {      "col1": 3,      "col2": "test003",      "con_details": [        {}      ],      "war_detail": [        {          "war_end_date": "2023-12-12",          "war_id": "WR678",          "war_start_date": "2001-01-01"        },        {          "war_end_date": "2030-12-12",          "war_id": "WR004",          "war_start_date": "2010-01-01"        }      ]    }  ]

要注意屬性的順序不一樣,但它們沒有順序,但 arrays 是按照我假設您的意圖排序的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM