简体   繁体   English

雪花 JSON FLATTEN with ORDER BY

[英]Snowflake JSON FLATTEN with ORDER BY

I have a working query that flattens a nested JSON object into rows of data.我有一个工作查询,将嵌套的 JSON object 展平为数据行。 What I would like to do, however, is preserve the original order of one array of objects which is nested several layers in.然而,我想做的是保留一个对象数组的原始顺序,该对象数组嵌套在多个层中。

I have tried to use ROW_NUMBER with an ORDER BY NULL and an ORDER BY (SELECT NULL) and neither seem to preserve the order.我尝试将ROW_NUMBERORDER BY NULLORDER BY (SELECT NULL)一起使用,但似乎都没有保留顺序。

Any ideas on how to accomplish that?关于如何实现这一目标的任何想法? Examples below.下面的例子。 I chose to mask the real data, but the important parts of the structure are the same.我选择屏蔽真实数据,但结构的重要部分是相同的。 The data in JSON format comes through with no rank-identifying information, but I used numbers as examples here to show the strange results. JSON 格式的数据没有排名识别信息,但我在这里使用数字作为示例来显示奇怪的结果。

Original structure (masked):原始结构(屏蔽):

{
    "topNode: {
        "childNode": {
            "list": [
                {
                    "title": "example title 1",
                },
                {
                    "title": "example title 2",
                },
                {
                    "title": "example title 3",
                },
                {
                    "title": "example title 4",
                },
                {
                    "title": "example title 5",
                }
            ]
        }
    }
}

Example query (masked):示例查询(屏蔽):

SELECT 
    A.VALUE:"title"::VARCHAR AS "TITLE",
    ROW_NUMBER() OVER(ORDER BY NULL) AS RANK
FROM 
    DB.SCHEMA.TABLE as A,
    lateral flatten(input=>A.JSON:topNode.childNode.list) "list_flatten"

Example output:示例 output:

TITLE                     RANK
"example title 3"         1
"example title 5"         2
"example title 2"         3
"example title 1"         4
"example title 4"         5

It is possible with INDEX , which returns index of element in array: INDEX是可能的,它返回数组中元素的索引:

SELECT A.VALUE:"title"::VARCHAR AS "TITLE",
       "list_flatten".index AS "RANK"
FROM DB.SCHEMA.TABLE as A,
lateral flatten(input=>A.JSON:topNode.childNode.list) "list_flatten"

I have a working query that flattens a nested JSON object into rows of data.我有一个工作查询将嵌套的 JSON object 展平为数据行。 What I would like to do, however, is preserve the original order of one array of objects which is nested several layers in.但是,我想做的是保留嵌套了几层的一组对象的原始顺序。

I have tried to use ROW_NUMBER with an ORDER BY NULL and an ORDER BY (SELECT NULL) and neither seem to preserve the order.我尝试将ROW_NUMBERORDER BY NULLORDER BY (SELECT NULL)一起使用,但似乎都没有保留订单。

Any ideas on how to accomplish that?关于如何做到这一点的任何想法? Examples below.下面的例子。 I chose to mask the real data, but the important parts of the structure are the same.我选择屏蔽真实数据,但结构的重要部分是相同的。 The data in JSON format comes through with no rank-identifying information, but I used numbers as examples here to show the strange results. JSON 格式的数据没有排名识别信息,但我在这里使用数字作为示例来显示奇怪的结果。

Original structure (masked):原始结构(屏蔽):

{
    "topNode: {
        "childNode": {
            "list": [
                {
                    "title": "example title 1",
                },
                {
                    "title": "example title 2",
                },
                {
                    "title": "example title 3",
                },
                {
                    "title": "example title 4",
                },
                {
                    "title": "example title 5",
                }
            ]
        }
    }
}

Example query (masked):示例查询(屏蔽):

SELECT 
    A.VALUE:"title"::VARCHAR AS "TITLE",
    ROW_NUMBER() OVER(ORDER BY NULL) AS RANK
FROM 
    DB.SCHEMA.TABLE as A,
    lateral flatten(input=>A.JSON:topNode.childNode.list) "list_flatten"

Example output:示例 output:

TITLE                     RANK
"example title 3"         1
"example title 5"         2
"example title 2"         3
"example title 1"         4
"example title 4"         5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM