我想创建一个 View ,并对 unnest 函数的每个结果进行一些数据处理。
在我的专栏 column2 中,我有:
- “12345”
- “123456”
- “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)
关于postgresql - unnest,对于每个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49537227/