查询雪花中的json数组

问题描述 投票:0回答:1

我在名为 IAEBREAKDOWNS 的列中有以下 Json 结构

{
    "breakdownScope1and2": [
        {
            "breakdown": [
                {
                    "name": "DQS1",
                    "percent": 27,
                    "total": 1000
                },
                {
                    "name": "DQS2",
                    "percent": 27,
                    "total": 1000
                }
            ],
            "breakdownBy": "DQS",
            "viewBy": "Account",
            "weightedAverage": 1.22
        },
        {
            "breakdown": [
                {
                    "name": "2024",
                    "percent": 27,
                    "total": 1000
                },
                {
                    "name": "2023",
                    "percent": 27,
                    "total": 1000
                }
            ],
            "breakdownBy": "Year of Emission",
            "viewBy": "Account",
            "weightedAverage": null
        },
        {
            "breakdown": [
                {
                    "name": "2024",
                    "percent": 27,
                    "total": 1000
                },
                {
                    "name": "2023",
                    "percent": 27,
                    "total": 1000
                }
            ],
            "breakdownBy": "Year of Revenue",
            "viewBy": "Account",
            "weightedAverage": null
        }
    ]
}

我想选择细分,其中细分= DQS 和 viewBy =“Account”,表为 ACCOUNTS_DATA

我有以下查询,但没有结果出了什么问题

SELECT IAEBREAKDOWNS:breakdownScope1and2.breakdown 
FROM ACCOUNTS_DATA 
WHERE variationid = '063de708-a90a-42e2-849d-89d6d56cc00e' 
AND IAEBREAKDOWNS IS NOT NULL 
AND 
IAEBREAKDOWNS:breakdownScope1and2.breakdownBy = 'DQS' 
AND IAEBREAKDOWNS:breakdownScope1and2.viewBy = 'Account'
snowflake-cloud-data-platform
1个回答
0
投票

所以让我们从 CTE 开始保存数据:

with data(iaebreakdowns, variationid) as (
select parse_json('{
    "breakdownScope1and2": [
        {
            "breakdown": [
                {
                    "name": "DQS1",
                    "percent": 27,
                    "total": 1000
                },
                {
                    "name": "DQS2",
                    "percent": 27,
                    "total": 1000
                }
            ],
            "breakdownBy": "DQS",
            "viewBy": "Account",
            "weightedAverage": 1.22
        },
        {
            "breakdown": [
                {
                    "name": "2024",
                    "percent": 27,
                    "total": 1000
                },
                {
                    "name": "2023",
                    "percent": 27,
                    "total": 1000
                }
            ],
            "breakdownBy": "Year of Emission",
            "viewBy": "Account",
            "weightedAverage": null
        },
        {
            "breakdown": [
                {
                    "name": "2024",
                    "percent": 27,
                    "total": 1000
                },
                {
                    "name": "2023",
                    "percent": 27,
                    "total": 1000
                }
            ],
            "breakdownBy": "Year of Revenue",
            "viewBy": "Account",
            "weightedAverage": null
        }
    ]
}'), '063de708-a90a-42e2-849d-89d6d56cc00e' 
)

然后显示数据需要展平才能访问

breakdownScope1and2

的数组项
SELECT f.value:breakdown as breakdown
    ,f.value:breakdownBy
    ,f.value:viewBy
FROM data as d
,TABLE(FLATTEN(input=>d.iaebreakdowns:breakdownScope1and2)) as f
WHERE d.variationid = '063de708-a90a-42e2-849d-89d6d56cc00e' 

enter image description here

现在使用正确的过滤器根据要求选择结果:

SELECT f.value:breakdown as breakdown
FROM data as d
,TABLE(FLATTEN(input=>d.iaebreakdowns:breakdownScope1and2)) as f
WHERE d.variationid = '063de708-a90a-42e2-849d-89d6d56cc00e' 
    AND f.value:breakdownBy = 'DQS' 
    AND f.value:viewBy = 'Account'

不需要 null 检查,因为如果 json 为 null,它将在 FLATTEN 中折叠,因为默认行为类似于 INNER JOIN。

© www.soinside.com 2019 - 2024. All rights reserved.