gpt4 book ai didi

postgresql - unnest,对于每个结果

转载 作者:行者123 更新时间:2023-11-29 13:15:35 27 4
gpt4 key购买 nike

我想创建一个 View ,并对 unnest 函数的每个结果进行一些数据处理。

在我的专栏 column2 中,我有:

  1. “12345”
  2. “123456”
  3. “12345,123456”或更多号码

我想做一些unnest(col2, ',') 并为每个结果做这样的事情:

if length(col2) = 5 then treatment_1
else if length(col2) = 6 then treatment_2

表中示例:

col1      col2        
-------------------
D1 12345, 123456
D3 12345
D4 123456

View 中的预期结果(对 col2 中的每一行进行处理):

col1      col2        
-------------------
D1 12345
D1 123456
D3 12345
D4 123456

最佳答案

您可以使用 regexp_split_to_table将字符串拆分为多行:

select  t1.col1
, case
when length(split.col2) > 5 then right(split.col2, 3)
else replace(split.col2, '123', '***')
end as col2
from Table1 t1
cross join
regexp_split_to_table(t1.col2, '\s*,\s*') split(col2)

Working example at SQL Fiddle.

关于postgresql - unnest,对于每个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49537227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com