将列数据转换为行,SQL Server 2008

时间:2014-11-07 17:41:26

标签: sql-server

请帮帮我......我目前的数据如下:

studentname          course_name              room_name    period   day_name
----------------------------------------------------------------------------
smith, john    Advisory B House       AR90           A         B
smith, john    AP Calculus AB         A03            6         B
smith, john    AP ECE Calculus BC     A03            2         B
smith, john    Maths                  A03            5         B
smith, john    Computer Science       C26            8         B
smith, john    Homeroom/LunchS1       C15            4         B
smith, john    Homeroom/LunchS2       C15            4         B
Avon, Lisa     Maths                  C26            3         B
Avon, Lisa     Copmuter Science       C15            8         B
Avon, Lisa     ACP  English 10        B09            A         B
Avon, Lisa     H Journalism           B09            2         B
Avon, Lisa     H Journalism           B09            3         B
Avon, Lisa     Homeroom/Lunch 11 S1   B09            7         B
Avon, Lisa     Homeroom/Lunch 11 S2   B09            7         B
Avon, Lisa     Science Fiction        B09            3         B

我想将行转换为以下列:

studentname period1   period2   period3     period4  period5         period6         period7
smith, john Advisory  Calculus  APCalculs   Maths    ComputerScience Homeroom/S1    Homeroom/LunchS2
           AR90        A03         A03      A03      C26              C15           C15
           A            6           2       5         8               4              4
           B            B           B       B         B               B              B

我非常感谢我对这个问题的任何帮助。非常感谢你。

-HT

1 个答案:

答案 0 :(得分:0)

使用Cross ApplyPivot即可获得结果。首先使用cross apply,您可以将多个列转换为多个行。然后使用Pivot你可以得到结果。

CREATE TABLE #pivot
  (studentname VARCHAR(50),course_name VARCHAR(50),
     room_name   VARCHAR(50),period      VARCHAR(50),
     day_name    VARCHAR(50))

INSERT INTO #pivot
VALUES      ('smith,john','Advisory B House','AR90','A','B'),
            ('smith,john','AP Calculus AB','A03','6','B'),
            ('smith,john','AP ECE Calculus BC','A03','2','B'),
            ('smith,john','Maths','A03','5','B'),
            ('smith,john','Computer Science','C26','8','B'),
            ('smith,john','Homeroom/LunchS1','C15','4','B'),
            ('smith,john','Homeroom/LunchS2','C15','4','B'),
            ('Avon, Lisa','Maths','C26','3','B'),
            ('Avon, Lisa','Computer Science','C15','8','B'),
            ('Avon, Lisa','ACP  English 10','B09','A','B'),
            ('Avon, Lisa','H Journalism','B09','2','B'),
            ('Avon, Lisa','H Journalism','B09','3','B'),
            ('Avon, Lisa','Homeroom/Lunch 11 S1','B09','7','B'),
            ('Avon, Lisa','Homeroom/Lunch 11 S2','B09','7','B'),
            ('Avon, Lisa','Science Fiction','B09','3','B') 


DECLARE @cols   VARCHAR(max)='',@aggcol VARCHAR(max)='',@sql    NVARCHAR(max)

select @cols += Rn from (
SELECT Distinct  ',' + 'period'+ CONVERT(VARCHAR(25), Row_number() OVER (PARTITION by studentname 
ORDER BY course_name)) Rn
FROM   #pivot) bb

SELECT @cols = RIGHT(@cols, Len(@cols) - 1)
select @aggcol += Rn from (
SELECT Distinct ',Max(' + 'period'+ CONVERT(VARCHAR(25), Row_number() OVER (PARTITION by studentname ORDER BY course_name))
                  + ') period'+ CONVERT(VARCHAR(25), Row_number() OVER (PARTITION by studentname ORDER BY course_name)) rn
FROM   #pivot) bb

SELECT @aggcol = RIGHT(@aggcol, Len(@aggcol) - 1) 


set @sql =
'SELECT studentname,
       '+@aggcol+'
FROM   (SELECT studentname,room_name,cname,data,periods
FROM  (SELECT ''period''+ CONVERT(VARCHAR(25), Row_number() OVER (ORDER BY course_name)) periods,
              *
       FROM   #pivot) a
      CROSS apply (VALUES (''course_name'',course_name),
                          (''room_name'',room_name),
                          (''period'',period),
                          (''day_name'',day_name)) cp (cname, data) )a
       PIVOT (Max(data)
             FOR periods IN('+@cols+')) piv
GROUP  BY studentname,
          cname '

--print @sql
EXEC SP_EXECUTESQL @sql 

<强>输出

studentname period1             period2         period3             period4  period5            period6             period7
----------- ----------------    --------------  ------------------  -------  ----------------   ----------------    ---------------
smith,john  Advisory B House    AP Calculus AB  AP ECE Calculus BC  Maths    Computer Science   Homeroom/LunchS1    Homeroom/LunchS2
smith,john  B                   B               B                   B        B                  B                   B
smith,john  A                   6               2                   5        8                  4                   4
smith,john  AR90                A03             A03                 A03      C26                C15                 C15