将嵌套阵列转换为雪花
我有一个表列,上面有雪花数据库中的嵌套数组。我想以雪花SQL中所示的方式将嵌套数组转换为列。
表名称:Sensor_data
Rx列是数据类型变体的。如下所示,嵌套阵列不会总是为3。在某些情况下,有20,000个嵌套阵列,而其他情况则没有。
| ID | RX |
|----|-----------------------------|
| 1 |[[0, 15], [1, 50], [2, 34.2]]|
| 2 |[[0, 20], [1, 25]] |
| 3 |[[0, 100], [1, 42], [2, 76]] |
我想从上表中实现这样的东西:
| ID |Col0 | Col1| Col2|
|----|-----|-----|-----|
| 1 | 15 | 50 | 34.2|
| 2 | 20 | 25 | NULL|
| 3 | 100 | 42 | 76 |
I have a table column with nested arrays in a Snowflake database. I want to convert the nested array into columns in the manner shown below in Snowflake SQL.
Table Name: SENSOR_DATA
The RX column is of data type VARIANT. The nested arrays will not always be 3 as shown below. There are cases where there are 20,000 nested arrays, and other cases where there are none.
| ID | RX |
|----|-----------------------------|
| 1 |[[0, 15], [1, 50], [2, 34.2]]|
| 2 |[[0, 20], [1, 25]] |
| 3 |[[0, 100], [1, 42], [2, 76]] |
I want to achieve something like this from the table above:
| ID |Col0 | Col1| Col2|
|----|-----|-----|-----|
| 1 | 15 | 50 | 34.2|
| 2 | 20 | 25 | NULL|
| 3 | 100 | 42 | 76 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们重新创建该表:
然后获取您拥有的不同列键:
给定我们希望这是动态的,下一步只是使用静态SQL检查它,它给了我们正确的答案:
立即写入动态执行此操作:
这给了我们:
,当运行给我们预期的结果时。
因此,现在我们想运行:
给予:
基于手册的这些部分的代码:
与loops一起工作
雪花扫描/结果表.html“ rel =“ nofollow noreferrer”>与结果集一起使用
和上述顺序:
giss:
,所以是的,但这是最大的操作:
因此,如果我们返回原始数据
并更改代码以不具有最大和组,
我们会看到:
以便我们可以看到这些值被插入了,现在由ID进行了,现在是ID的组,将要滚动这三个ID,因此对于Col0,我们具有
15,null,Null
max采用最高的值,而15较高,而null则是null的。这是正在进行的过程。Lets recreate that table:
Then get the distinct column keys that you have:
Given we want this to be dynamic, the next step is just to check with static SQL it gives us the correct answer:
write now to do this dynamically:
which gives us:
which when run gives us the expected results.
so now we want to run that:
gives:
based of code from these sections of the manual:
Working with loops
Working with Resultsets
With extra "tricky data" and the mentioned ORDER BY:
gives:
So yes, but that's the MAX doing:
so if we go back in to original data
and alter the code to not have the MAX and the GROUP BY
we see:
so we can see those values getting unrolled, now the GROUP BY id, is going to roll those three id's up, thus for col0 we have
15, null, null
MAX takes the higest value, and 15 is higher that null so that is what is keep. This is the process this is working on.使用[]访问数组元素:
Using [] to access array elements:
您的要求不完全是什么,但这很接近
Not exactly what you asked for but this is close