Snowflake SQL如何打开阵列

发布于 2025-02-10 19:16:47 字数 587 浏览 0 评论 0原文

在Snowflake数据库中,我有一张桌子上有一个数组列。 大多数pf时数阵列中只有1个值,但最多可达100。 我正在尝试打开将为每个单元格提供不同原始的数组。

这是表中一行的一个示例:

"currencies_added":[{"Gems": 24000},{"Gems": 1250}]

尝试使用“ Flatten”函数,但是每次都会出现错误:

单行子查询返回一行以上。

例如:

select * 
from  FISH_OF_FORTUNE_DEV.DWH.FACT_DAILY_REVENUE, 
 table(flatten ( 
     select currencies_added 
     from FISH_OF_FORTUNE_DEV.DWH.FACT_DAILY_REVENUE
  ) )f ;

请参阅图像: 数据库中的行

In Snowflake Database, I've got a table where I got an array column.
most pf the times there is only 1 value in the array, but can be up to 100.
I'm trying to open the array that will give each cell a different raw.

Here is an example of a single row in the table:

"currencies_added":[{"Gems": 24000},{"Gems": 1250}]

Tried using "flatten" function, but each time I get an error:

Single-row subquery returns more than one row.

For example:

select * 
from  FISH_OF_FORTUNE_DEV.DWH.FACT_DAILY_REVENUE, 
 table(flatten ( 
     select currencies_added 
     from FISH_OF_FORTUNE_DEV.DWH.FACT_DAILY_REVENUE
  ) )f ;

see image:
Rows in database

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2025-02-17 19:16:47

假设这些数组存储在变体列或数组中,并且已被解析为JSON,则需要使用横向变平。该代码使用CTE使用您提供的相同提供的,而不是表:

WITH x AS (SELECT parse_json('[{"Gems": 24000},{"Gems": 1250}]') as var)
SELECT x.var, y.value, y.value:Gems::number
FROM x,
lateral flatten(input=>var) y;

我查询中的输出:

  1. 原始
  2. 在变平之后的
  3. 值之后,铸造为gems的数字

Assuming these arrays are stored in a variant columns or arrays and have been parsed as JSON, you'd want to use a lateral flatten. This code uses a CTE to use your same provided, rather than a table:

WITH x AS (SELECT parse_json('[{"Gems": 24000},{"Gems": 1250}]') as var)
SELECT x.var, y.value, y.value:Gems::number
FROM x,
lateral flatten(input=>var) y;

Output in my query:

  1. original value
  2. value after flatten
  3. value after flatten for Gems cast to a number
滴情不沾 2025-02-17 19:16:47

添加到@mike,使用Flatten的更多示例。

使用以下数据集的原始形式(无扁平) -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select parse_json(colname) from cte;

| PARSE_JSON(colname)     |
|-------------------------|
| {                       |
|   "currencies_added": [ |
|     {                   |
|       "Gems": 24000     |
|     },                  |
|     {                   |
|       "Gems": 1250      |
|     }                   |
|   ]                     |
| }                       |

仅扁平列 -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select value from cte, 
lateral flatten(input=>parse_json(colname));

+-------------------+
| VALUE             |
|-------------------|
| [                 |
|   {               |
|     "Gems": 24000 |
|   },              |
|   {               |
|     "Gems": 1250  |
|   }               |
| ]                 |
+-------------------+
1 Row(s) produced.

扁平以获取数组(将产生两个行,对应于两个数组元素) -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select value from cte, 
lateral flatten(input=>parse_json(colname):"currencies_added");

| VALUE           |
|-----------------|
| {               |
|   "Gems": 24000 |
| }               |
| {               |
|   "Gems": 1250  |
| }               |

平坦的数组元素 -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select lf2.key, lf2.value from cte, 
lateral flatten(input=>parse_json(colname):"currencies_added") lf1, 
lateral flatten(input=>lf1.value) lf2;
键值或您可以
Gems24000
Gems1250

,您可以,您可以使用递归子句 -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select lf1.key, lf1.value from cte,
lateral flatten(input=>parse_json(colname):"currencies_added", recursive=>TRUE) lf1 
where key is NOT NULL;
钥匙
GEMS24000
GEMS1250

使用AS AS仅数组 -

with cte(colname) as (
select * from values
('[{"Gems": 24000},{"Gems": 1250}]')
)
select lf1.key, lf1.value from cte,
lateral flatten(input=>parse_json(colname), recursive=>TRUE) lf1
where key is NOT NULL;
钥匙
GEMS24000
GEMS1250

Adding to @Mike, few more examples of using FLATTEN.

Using below data-set in original form (no flatten) -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select parse_json(colname) from cte;

| PARSE_JSON(colname)     |
|-------------------------|
| {                       |
|   "currencies_added": [ |
|     {                   |
|       "Gems": 24000     |
|     },                  |
|     {                   |
|       "Gems": 1250      |
|     }                   |
|   ]                     |
| }                       |

Flatten only the column -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select value from cte, 
lateral flatten(input=>parse_json(colname));

+-------------------+
| VALUE             |
|-------------------|
| [                 |
|   {               |
|     "Gems": 24000 |
|   },              |
|   {               |
|     "Gems": 1250  |
|   }               |
| ]                 |
+-------------------+
1 Row(s) produced.

Flatten to get the array (will produce two rows, corresponding to two array elements) -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select value from cte, 
lateral flatten(input=>parse_json(colname):"currencies_added");

| VALUE           |
|-----------------|
| {               |
|   "Gems": 24000 |
| }               |
| {               |
|   "Gems": 1250  |
| }               |

Flatten array elements -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select lf2.key, lf2.value from cte, 
lateral flatten(input=>parse_json(colname):"currencies_added") lf1, 
lateral flatten(input=>lf1.value) lf2;
KEYVALUE
Gems24000
Gems1250

OR, you can use recursive clause -

with cte(colname) as (
select * from values
('{"currencies_added":[{"Gems": 24000},{"Gems": 1250}]}')
)
select lf1.key, lf1.value from cte,
lateral flatten(input=>parse_json(colname):"currencies_added", recursive=>TRUE) lf1 
where key is NOT NULL;
KEYVALUE
Gems24000
Gems1250

Using as array only -

with cte(colname) as (
select * from values
('[{"Gems": 24000},{"Gems": 1250}]')
)
select lf1.key, lf1.value from cte,
lateral flatten(input=>parse_json(colname), recursive=>TRUE) lf1
where key is NOT NULL;
KEYVALUE
Gems24000
Gems1250
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文