postgresql - unnest,对于每个结果

标签 postgresql unnest

我想创建一个 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/

相关文章:

PostgreSQL - 流式实体化 View 更改

r - 检查一个变量 R 中各种 DATE 的差异

postgresql - 如何对 PostgreSQL 数据库运行 MDX 查询?

postgresql - 如果月份是十二月,则年份增加 1

postgresql - 选择所有标记有标签列表的行

c# - 在postgresql中添加到bytea blob的函数

sql - 将 UNNEST 与 JOIN 结合使用

arrays - 查找文本数组包含与输入相似的值的行

mysql - 我可以在 MySQL 中获取 json 列的唯一/不同值吗?

sql - 谷歌 BigQuery : UNNEST where each different key becomes a column