【问题标题】:Split comma separated column data into additional columns which has more than 50 fields将逗号分隔的列数据拆分为具有 50 多个字段的附加列
【发布时间】:2018-10-12 11:00:20
【问题描述】:

所以我正在尝试将逗号分隔的列数据拆分为其他列。

select max(array_length(regexp_split_to_array(tags, ','), 1)) from employee 

    The value is 61. 

如何更改下面不需要输入 a[xx] 61 次的内容?

select a[1], a[2], a[3], a[4], a[5], a[6]
from (
    select regexp_split_to_array(tags, ',')
    from employee
) as dt(a)

【问题讨论】:

  • 不要将结果放在单独的列中,将它们放在单独的行中。
  • 我该怎么做?
  • 如果您需要 60 列,则需要为每一列键入表达式。没有办法解决它。
  • 您可以使用动态 SQL 来构建一个字符串,然后执行该操作,但在 ... 中简单地键入它可能更容易
  • @user10324767 。 . . regexp_split_to_table().

标签: sql postgresql split delimiter


【解决方案1】:

正如@gordon-linoff 指出的那样,您可以使用regexp_split_to_table

代码示例:

create table employee (tags varchar(50));

insert into employee values ('one,two,three'),('cow,chicken,rooster');`

select * 
from (
    select regexp_split_to_table(tags, ',')
    from employee
) as t

结果:

 regexp_split_to_table
-----------------------
 one
 two
 three
 cow
 chicken
 rooster

【讨论】:

    猜你喜欢
    • 2018-11-11
    • 2018-11-02
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多