如何透视文本列?

时间:2018-02-02 15:34:50

标签: sql tsql

我们的BA为我们提供了Excel表格,其中包含我在SQL表中加载的两列column1column2column1包含不同的值EmployeeID, FirstName, LastName, Addresscolumn2包含受尊重的值。

如何将Column1个不同的值转换为列,以便我的表格会以EmployeeID, FirstName, LastName, and Address为列?

File

Column1     || Column2
EmployeeID  || 00251
FirstName   || Joe
LastName    || Reed
Address     || 101 5th Ave
EmployeeID  || 00145
FirstName   || Jen
LastName    || Smith
Address     || 1001 Henderson RD

我正在使用以下查询,但它发出错误。

  SELECT pt.EmployeeID , pt.FirstName, pt.LastName, pt.[Address]
  FROM 
    (
        SELECT *
        FROM FlatFile.UserList

    ) AS temp
        PIVOT
        (
            MAX([COLUMN2])
            FOR [COLUMNE1] IN ('EmployeeID','FirstName','LastName:','[Address]')

        ) as pt

错误:

Error Message:
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'EmployeeID'.

1 个答案:

答案 0 :(得分:2)

将拼写错误$attributes修复为COLUMNE1,并从字段列表中删除字符串引号?

COLUMN1

SQL Server: Examples of PIVOTing String data