T-SQL -- 将逗号分隔的列转换为多列

Posted

技术标签:

【中文标题】T-SQL -- 将逗号分隔的列转换为多列【英文标题】:T-SQL -- convert comma-delimited column into multiple columns 【发布时间】:2010-11-16 23:32:33 【问题描述】:

从下表中,如何将Values 列转换为多个列,并填充当前用逗号分隔的各个值?转换前:

Name  Values 
----  ------
John  val,val2,val3 
Peter val5,val7,val9,val14 
Lesli val8,val34,val36,val65,val71,val 
Amy   val3,val5,val99

转换的结果应该是这样的:

Name  Col1  Col2  Col3  Col4  Col5  Col6 
----  ----  ----  ----  ----  ----  ----
John  val   val2  val3 
Peter val5  val7  val9  val14 
Lesli val8  val34 val36 val65 val71 val 
Amy   val3  val5  val99

【问题讨论】:

请稍微格式化一下您的表格,以便我们更清楚您的要求。 刚刚上传的图片无法格式化表格。 该图像不允许您搜索您的问题。将表格样本缩进 4 个空格以保留格式... 【参考方案1】:

首先,您使用的是什么数据库产品和版本?如果您使用的是 SQL Server 2005 及更高版本,您可以编写一个 Split 用户定义函数,如下所示:

CREATE FUNCTION [dbo].[Split]
(   
    @DelimitedList nvarchar(max)
    , @Delimiter varchar(2) = ','
)
RETURNS TABLE 
AS
RETURN 
    (
    With CorrectedList As
        (
        Select Case When Left(@DelimitedList, DataLength(@Delimiter)) <> @Delimiter Then @Delimiter Else '' End
            + @DelimitedList
            + Case When Right(@DelimitedList, DataLength(@Delimiter)) <> @Delimiter Then @Delimiter Else '' End
            As List
            , DataLength(@Delimiter) As DelimiterLen
        )
        , Numbers As 
        (
        Select TOP (Coalesce(Len(@DelimitedList),1)) Row_Number() Over ( Order By c1.object_id ) As Value
        From sys.objects As c1
            Cross Join sys.columns As c2
        )
    Select CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen As Position
        , Substring (
                    CL.List
                    , CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen     
                    , CharIndex(@Delimiter, CL.list, N.Value + 1)                           
                        - ( CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen ) 
                    ) As Value
    From CorrectedList As CL
        Cross Join Numbers As N
    Where N.Value < Len(CL.List)
        And Substring(CL.List, N.Value, CL.DelimiterLen) = @Delimiter
    )

然后您可以使用类似于以下内容的方式拆分所需的值:

Select Name, Values
From Table1 As T1
Where Exists    (
                Select 1
                From Table2 As T2
                    Cross Apply dbo.Split (T1.Values, ',') As T1Values
                    Cross Apply dbo.Split (T2.Values, ',') As T2Values
                Where T2.Values.Value = T1Values.Value
                    And T1.Name = T2.Name
                )

【讨论】:

【参考方案2】:

这是一个使用递归 cte 生成“table of numbers”(由Itzik Ben-Gan 提供)的解决方案,它是useful,用于解决包括字符串拆分和 PIVOT 在内的所有问题。 SQL Server 2005 及更高版本。包含完整的表创建、插入和选择脚本。

CREATE TABLE dbo.Table1 
(
    Name        VARCHAR(30),
    [Values]    VARCHAR(128)
)
GO

INSERT INTO dbo.Table1 VALUES ('John', 'val,val2,val3')
INSERT INTO dbo.Table1 VALUES ('Peter', 'val5,val7,val9,val14')
INSERT INTO dbo.Table1 VALUES ('Lesli', 'val8,val34,val36,val65,val71,val')
INSERT INTO dbo.Table1 VALUES ('Amy', 'val3,val5,val99')
GO

SELECT * FROM dbo.Table1;
GO

WITH
L0 AS(SELECT 1 AS c UNION ALL SELECT 1),
L1 AS(SELECT 1 AS c FROM L0 AS A, L0 AS B),
L2 AS(SELECT 1 AS c FROM L1 AS A, L1 AS B),
L3 AS(SELECT 1 AS c FROM L2 AS A, L2 AS B),
Numbers AS(SELECT ROW_NUMBER() OVER(ORDER BY c) AS n FROM L3)
SELECT Name, [1] AS Column1, [2] AS Column2, [3] AS Column3, [4] AS Column4, [5] AS Column5, [6] AS Column6, [7] AS Column7
FROM
(SELECT Name,
        ROW_NUMBER() OVER (PARTITION BY Name ORDER BY nums.n) AS PositionInList,
        LTRIM(RTRIM(SUBSTRING(valueTable.[Values], nums.n, charindex(N',', valueTable.[Values] + N',', nums.n) - nums.n))) AS [Value]
 FROM   Numbers AS nums INNER JOIN dbo.Table1 AS valueTable ON nums.n <= CONVERT(int, LEN(valueTable.[Values])) AND SUBSTRING(N',' + valueTable.[Values], n, 1) = N',') AS SourceTable
PIVOT
(
MAX([VALUE]) FOR PositionInList IN ([1], [2], [3], [4], [5], [6], [7])
) AS Table2
GO

--DROP TABLE dbo.Table1 

哪个转换这个输出

Name   Values
John   val,val2,val3
Peter  val5,val7,val9,val14
Lesli  val8,val34,val36,val65,val71,val
Amy    val3,val5,val99

Name  Column1 Column2 Column3 Column4 Column5 Column6 Column7
Amy   val3    val5    val99   NULL    NULL    NULL    NULL
John  val     val2    val3    NULL    NULL    NULL    NULL
Lesli val8    val34   val36   val65   val71   val     NULL
Peter val5    val7    val9    val14   NULL    NULL    NULL

【讨论】:

我应该补充一点,这样做的缺点是您无法根据逗号分隔字符串中的最大值数动态调整列数。为此,您需要考虑动态 SQL 并建立列数以等于任何一个分隔字符串中的最大值数。

以上是关于T-SQL -- 将逗号分隔的列转换为多列的主要内容,如果未能解决你的问题,请参考以下文章

使用 SQL Server 将逗号分隔的文本转换为多列结果

SQL Server 逗号分隔列到多列

如何用引号和逗号连接多列单元格数据

Pandas 用逗号将列拆分为多列

将逗号分割的列,变成多列

EXCEL中如何把一列数据变为多列?