Regexp_replace在雪花中

发布于 2025-01-23 01:59:05 字数 335 浏览 0 评论 0原文

我在雪花表中有旧的_column,并希望将值转换为新列,如下所述。

我尝试了Regexp_replace,无法编写精确的逻辑。

old_columnnew_column
1,2,4ACT,星期六,TOEFL
6,3lnat,雅思
5,1ACT,BMAT

I have the old_column in the snowflake table and would like to convert the values to the new column as mentioned below.

I tried with REGEXP_REPLACE and couldn’t able to write exact logic.

old_columnnew_column
1,2,4ACT,SAT,TOEFL
6,3LNAT,IELTS
5,1ACT,BMAT

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

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

发布评论

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

评论(1

執念 2025-01-30 01:59:05

您的new_columns不会显示随机订单。假设其订单,一个查询的一个版本可以如下(可以根据数据值进一步添加情况) -

with data_cte as
(select * from values
('1,2,4'),
('6,3'),
('5,1')
)
select distinct
listagg (value,',')
within group (order by index)
over (partition by seq) old_value,
listagg (col,',')
within group (order by value::number)
over (partition by seq) new_value
from (
select t.*,
case
when value=1 then 'ACT'
when value=2 then 'SAT'
when value=3 then 'IELTS'
when value=4 then 'TOEFL'
when value=5 then 'BMAT'
ELSE 'LNAT'
end as col
from data_cte d , table(split_to_table(d.column1,',')) t
);
+-----------+---------------+
| OLD_VALUE | NEW_VALUE     |
|-----------+---------------|
| 1,2,4     | ACT,SAT,TOEFL |
| 6,3       | IELTS,LNAT    |
| 5,1       | ACT,BMAT      |
+-----------+---------------+

Your new_columns does not shows a random order. Assuming its ordered, one version of query can be as follows (CASE can be added as needed further based on data values) -

with data_cte as
(select * from values
('1,2,4'),
('6,3'),
('5,1')
)
select distinct
listagg (value,',')
within group (order by index)
over (partition by seq) old_value,
listagg (col,',')
within group (order by value::number)
over (partition by seq) new_value
from (
select t.*,
case
when value=1 then 'ACT'
when value=2 then 'SAT'
when value=3 then 'IELTS'
when value=4 then 'TOEFL'
when value=5 then 'BMAT'
ELSE 'LNAT'
end as col
from data_cte d , table(split_to_table(d.column1,',')) t
);
+-----------+---------------+
| OLD_VALUE | NEW_VALUE     |
|-----------+---------------|
| 1,2,4     | ACT,SAT,TOEFL |
| 6,3       | IELTS,LNAT    |
| 5,1       | ACT,BMAT      |
+-----------+---------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文