如何在sql中将行转换为列

时间:2016-08-29 08:39:21

标签: sql sql-server pivot

我有一张桌子,

table 1

并需要将其转换为

table 2

我尝试了枢轴,但无法弄清楚请帮助我。

2 个答案:

答案 0 :(得分:0)

Pivot无法使用多个列开箱即用有几种方法可以解决此问题:

将此表格变量用于所有测试,请说明您的样本数据始终复制并且可以粘贴。最好的是MCVE(最小完整可验证示例),您可以在此处设置一些类似我的代码。

DECLARE @tbl TABLE(ID INT, Code INT,EmployeeName VARCHAR(100),ExamName VARCHAR(100),Board VARCHAR(100),Result VARCHAR(100));
INSERT INTO @tbl VALUES
 (11537,12984,'TheName','SSC','b04','1st')
,(11537,12984,'TheName','HSC','b04','2nd')
,(11537,12984,'TheName','BA(H)','u33','2nd');

这是首先将数据连接到一个列的代码。这允许PIVOT

SELECT p.*
FROM
(
    SELECT tbl.ID
        ,tbl.Code
        ,tbl.EmployeeName
        ,'Exam_' + CAST(ROW_NUMBER() OVER(PARTITION BY tbl.ID ORDER BY tbl.Code) AS VARCHAR(100)) AS ColName
        ,ExamName + ' (' + Board + '): ' + Result AS Concatenated
    FROM @tbl AS tbl
) AS t
PIVOT
(
    MIN(Concatenated) FOR ColName IN(Exam_1,Exam_2,Exam_3 /*add as many as you need*/)
) AS p

结果:

11537   12984   TheName SSC (b04): 1st  HSC (b04): 2nd  BA(H) (u33): 2nd

下一个代码完全相同,但是创建XML而不是纯文本,这允许之后分离您的数据:

SELECT p.ID
      ,p.Code
      ,p.EmployeeName
      ,E1
      ,E1.value('(/exam/@ExamName)[1]','varchar(100)') AS ExamName1
      ,E1.value('(/exam/@Board)[1]','varchar(100)') AS Board1
      ,E1.value('(/exam/@Result)[1]','varchar(100)') AS Result1
      ,E2
      ,E2.value('(/exam/@ExamName)[1]','varchar(100)') AS ExamName2
      ,E2.value('(/exam/@Board)[1]','varchar(100)') AS Board2
      ,E2.value('(/exam/@Result)[1]','varchar(100)') AS Result2
      ,E3
      ,E3.value('(/exam/@ExamName)[1]','varchar(100)') AS ExamName3
      ,E3.value('(/exam/@Board)[1]','varchar(100)') AS Board3
      ,E3.value('(/exam/@Result)[1]','varchar(100)') AS Result3
FROM
(
    SELECT tbl.ID
        ,tbl.Code
        ,tbl.EmployeeName
        ,'Exam_' + CAST(ROW_NUMBER() OVER(PARTITION BY tbl.ID ORDER BY tbl.Code) AS VARCHAR(100)) AS ColName
        ,(SELECT ExamName AS [@ExamName],Board AS [@Board],Result AS [@Result] FOR XML PATH('exam')) AS AsXML
    FROM @tbl AS tbl
) AS t
PIVOT
(
    MIN(AsXML) FOR ColName IN(Exam_1,Exam_2,Exam_3 /*add as many as you need*/)
) AS p
OUTER APPLY
(
    SELECT CAST(p.Exam_1 AS XML) AS E1
          ,CAST(p.Exam_2 AS XML) AS E2
          ,CAST(p.Exam_3 AS XML) AS E3
) AS CastedToXml

结果:

11537   12984   TheName <exam ExamName="SSC" Board="b04" Result="1st" />    SSC b04 1st <exam ExamName="HSC" Board="b04" Result="2nd" />    HSC b04 2nd <exam ExamName="BA(H)" Board="u33" Result="2nd" />  BA(H)   u33 2nd

这是老式的支点,这通常比正常支点更好:

;WITH Numberd AS
(
    SELECT *
          ,ROW_NUMBER() OVER(PARTITION BY tbl.ID ORDER BY tbl.Code) AS Number
    FROM @tbl AS tbl
)
SELECT ID,Code,EmployeeName
      ,MAX(CASE WHEN Number=1 THEN ExamName END) AS ExamName1
      ,MAX(CASE WHEN Number=1 THEN Board END) AS Board1
      ,MAX(CASE WHEN Number=1 THEN Result END) AS Result1
      ,MAX(CASE WHEN Number=2 THEN ExamName END) AS ExamName2
      ,MAX(CASE WHEN Number=2 THEN Board END) AS Board2
      ,MAX(CASE WHEN Number=2 THEN Result END) AS Result2
      ,MAX(CASE WHEN Number=3 THEN ExamName END) AS ExamName3
      ,MAX(CASE WHEN Number=3 THEN Board END) AS Board3
      ,MAX(CASE WHEN Number=3 THEN Result END) AS Result3
FROM Numberd
GROUP BY ID,Code,EmployeeName

最后一个选项是动态SQL ...

答案 1 :(得分:0)

让你大吃一惊,但我的目的是用这个来解决的。

EXCEPTION: Error in ./AppComponent class AppComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: No provider for Http!
ORIGINAL STACKTRACE:
Error: DI Exception
at NoProviderError.BaseException [as constructor] (/data/data/org.nativescript.groceries/files/app/tns_modules/@angular/core/src/facade/exceptions.js:27:23)
at NoProviderError.AbstractProviderError [as constructor] (/data/data/org.nativescript.groceries/files/app/tns_modules/@angular/core/src/di/reflective_exceptions.js:43:16)
at new NoProviderError (/data/data/org.nativescript.groceries/files/app/tns_modules/@angular/core/src/di/reflective_exceptions.js:80:16)
at ReflectiveInjector_._throwOrNull (/data/data/org.nativescript.groceries/files/app/tns_modules/@angular/core/src/di/reflective_injector.js:786:19)
at ReflectiveInjector_._getByKeyDefault (/data/data/org.nativescript.groceries/files/app/tns_modules/@angular/core/src/di/reflective_injector.js:814:25)
at ReflectiveInjector_._getByKey (/data/data/org.nativescript.groce

然后

{{1}}

以上查询为我服务,但会尝试@Shnugo Sample,谢谢大家。