SQL Server Pivot进入列

时间:2016-11-22 06:48:41

标签: sql-server pivot

我已经查看了所提出的各种SQL Server Pivot问题,但我无法为我的特定示例实现各种解决方案。我想知道是否有人可以为我提供正确的SQL语法,以允许我将行转换为列。

我有下表:

Tutor_Event_ID  Tutor_FAN       Tutor_Individual_ID     Tutor_Title         Tutor_First_Name        Tutor_Last_Name     
59220           56004633        46415                   Mr                  Michael                 Brian                           
59220           67693357        128376                  Mr                  John                    Newtons 

...我希望将信息转置为如下所示:

Tutor_Event_ID  First_Tutor_FAN         First_Tutor_Individual_ID       First_Tutor_Title           First_Tutor_First_Name      First_Tutor_Last_Name       Second_Tutor_FAN        Second_Tutor_Individual_ID      Second_Tutor_Title          Second_Tutor_First_Name     Second_Tutor_Last_Name      
59220           56004633                46415                           Mr                          Michael                     Brian                       67693357                128376                          Mr                          John                        Newtons

这里的挑战是我需要转换超过1个字段,并且我不知道这些列将保留的所有可能值,但实质上它是每个事件有一行,其中列出了Tutor(或Tutors)信息在同一排。 任何人都可以建议一个SQL代码来做这个吗?

非常感谢, 婆婆纳

2 个答案:

答案 0 :(得分:0)

您可以参考以下示例,希望对您有所帮助

        IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t
        CREATE TABLE #t(Tutor_Event_ID INT ,Tutor_FAN VARCHAR(100),Tutor_Individual_ID INT,Tutor_Title VARCHAR(100),Tutor_First_Name VARCHAR(100),Tutor_Last_Name VARCHAR(100))
        INSERT INTO #t(Tutor_Event_ID,Tutor_FAN,Tutor_Individual_ID,Tutor_Title,Tutor_First_Name,Tutor_Last_Name)
        SELECT 59220,'56004633',46415,'Mr','Michael','Brian' UNION ALL
        SELECT 59220,'67693357',128376,'Mr','John','Newtons' 
        DECLARE @sql NVARCHAR(max),@col NVARCHAR(max)

        SELECT @col=ISNULL(@col+',[','[')+c.data_title+']'
        FROM (SELECT *,CONVERT(VARCHAR,ROW_NUMBER()OVER(PARTITION BY Tutor_Event_ID ORDER BY Tutor_FAN)) AS rn FROM #t) AS t
        CROSS APPLY(VALUES(t.rn+'_Tutor_FAN',CONVERT(VARCHAR,Tutor_FAN)),(t.rn+'_Tutor_Individual_ID',CONVERT(VARCHAR,Tutor_Individual_ID)),(t.rn+'_Tutor_Title',Tutor_Title),(t.rn+'_Tutor_First_Name',Tutor_First_Name),(t.rn+'_Tutor_Last_Name',Tutor_Last_Name)) c(data_title,data_value)

        SET @sql='
        SELECT * FROM (
           SELECT t.Tutor_Event_ID,c.data_title,c.data_value
           FROM (SELECT *,CONVERT(VARCHAR,ROW_NUMBER()OVER(PARTITION BY Tutor_Event_ID ORDER BY Tutor_FAN)) AS rn FROM #t) AS t
           CROSS APPLY(VALUES(t.rn+''_Tutor_FAN'',CONVERT(VARCHAR,Tutor_FAN)),(t.rn+''_Tutor_Individual_ID'',CONVERT(VARCHAR,Tutor_Individual_ID)),(t.rn+''_Tutor_Title'',Tutor_Title),(t.rn+''_Tutor_First_Name'',Tutor_First_Name),(t.rn+''_Tutor_Last_Name'',Tutor_Last_Name)) c(data_title,data_value)
        ) n PIVOT(MAX(data_value) FOR data_title IN ('+@col+')) p
        '
        EXEC(@sql)
        IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t

以下是结果:

Tutor_Event_ID  1_Tutor_FAN 1_Tutor_Individual_ID   1_Tutor_Title   1_Tutor_First_Name  1_Tutor_Last_Name   2_Tutor_FAN 2_Tutor_Individual_ID   2_Tutor_Title   2_Tutor_First_Name  2_Tutor_Last_Name
59220   56004633    46415   Mr  Michael Brian   67693357    128376  Mr  John    Newtons

答案 1 :(得分:-1)

我创建了一个受支持的表(Numbers),它将具有类似的列 num和word(1,first) enter image description here