将逗号分隔的列数据拆分为其他列

问题描述 投票:53回答:3

我在列中有逗号分隔的数据:

Column 
------- 
a,b,c,d 

我想将逗号分隔数据拆分为多个列以获得此输出:

Column1  Column2 Column3 Column4 
-------  ------- ------- -------
a        b       c       d 

怎么能实现这一目标?

postgresql split delimiter postgresql-8.4
3个回答
61
投票

如果CSV中的字段数是常数,那么您可以执行以下操作:

select a[1], a[2], a[3], a[4]
from (
    select regexp_split_to_array('a,b,c,d', ',')
) as dt(a)

例如:

=> select a[1], a[2], a[3], a[4] from (select regexp_split_to_array('a,b,c,d', ',')) as dt(a);
 a | a | a | a 
---+---+---+---
 a | b | c | d
(1 row)

如果CSV中的字段数不是常数,那么您可以使用以下内容获得最大字段数:

select max(array_length(regexp_split_to_array(csv, ','), 1))
from your_table

然后为您的查询构建适当的a[1], a[2], ..., a[M]列列表。因此,如果上面给出的最大值为6,那​​么你可以使用:

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

如果需要,可以将这两个查询组合成一个函数。

例如,提供此数据(在最后一行中为NULL):

=> select * from csvs;
     csv     
-------------
 1,2,3
 1,2,3,4
 1,2,3,4,5,6

(4 rows)

=> select max(array_length(regexp_split_to_array(csv, ','), 1)) from csvs;
 max 
-----
   6
(1 row)

=> select a[1], a[2], a[3], a[4], a[5], a[6] from (select regexp_split_to_array(csv, ',') from csvs) as dt(a);
 a | a | a | a | a | a 
---+---+---+---+---+---
 1 | 2 | 3 |   |   | 
 1 | 2 | 3 | 4 |   | 
 1 | 2 | 3 | 4 | 5 | 6
   |   |   |   |   | 
(4 rows)

由于您的分隔符是一个简单的固定字符串,您也可以使用string_to_array而不是regexp_split_to_array

select ...
from (
    select string_to_array(csv, ',')
    from csvs
) as dt(a);

感谢Michael提供有关此功能的提醒。

您真的应该重新设计数据库架构,以便尽可能避免使用CSV列。您应该使用数组列或单独的表。


89
投票

split_part()一步到位地做你想做的事:

SELECT split_part(col, ',', 1) AS col1
     , split_part(col, ',', 2) AS col2
     , split_part(col, ',', 3) AS col3
     , split_part(col, ',', 4) AS col4
FROM   tbl;

添加与col中的项目一样多的行(可能的最大值)。超出数据项的列将为空字符串('')。


1
投票

您可以使用拆分功能。

    SELECT 
    (select top 1 item from dbo.Split(FullName,',') where id=1 ) Column1,
    (select top 1 item from dbo.Split(FullName,',') where id=2 ) Column2,
    (select top 1 item from dbo.Split(FullName,',') where id=3 ) Column3,
    (select top 1 item from dbo.Split(FullName,',') where id=4 ) Column4,
    FROM MyTbl
© www.soinside.com 2019 - 2024. All rights reserved.