2

从下表中,如何将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

2 回答 2

3

首先,您使用的是什么数据库产品和版本?如果您使用的是 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
                )
于 2010-11-17T02:19:40.890 回答
3

这是一个使用递归 cte 生成“数字表”的解决方案(由Itzik Ben-Gan提供),这对于包括字符串拆分和 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
于 2010-11-17T03:21:27.067 回答