我已将复杂架构简化为以下示例
学生
类
和联结表
StudentsClasses
我的目标是列出每个学生按周期顺序列出的所选课程。我有以下选择
SELECT Name,Period1, Period2, Period3,
Period4, Period5, Period6
FROM (
SELECT _Students.Name AS [NAME],_Classes.Period AS PIVOT_CODE, _Classes.name as [Class]
FROM _Classes
INNER JOIN _StudentsClasses ON _Classes.ClassID=_StudentsClasses.ClassID
INNER JOIN _Students ON _StudentsClasses.StudentID=_Students.StudentID
)
AS data
PIVOT
( min([Class]) FOR [PIVOT_CODE] IN
(Period1, Period2, Period3,
Period4, Period5, Period6)
) AS pvt
结果是
Name Period1 Period2 Period3 Period4 Period5 Period6
------ --------- --------- --------- --------- --------- ----------
Amy NULL NULL NULL NULL NULL History
Beth NULL Speech NULL Physics NULL History
Bill Algebra Speech NULL Physics NULL NULL
Scott Algebra NULL NULL Physics NULL NULL
Steve NULL Speech NULL NULL NULL History
我需要帮助的地方是我需要将所有非空值移到左列,这样就没有空白了。可以重命名列名称,例如
Name Choice1 Choice2 Choice3 Choice4 Choice5 Choice6
------ --------- --------- --------- --------- --------- ----------
Amy History
Beth Speech Physics History
Bill Algebra Speech Physics
Scott Algebra Physics
Steve Speech History
我可以通过选择一个临时表到一个临时表,然后用光标迭代每一行/列来实现这一点,但我想避免这种情况。非常感谢任何建议。
答案 0 :(得分:5)
假设SQL Server 2005(至少),使用ROW_NUMBER()
订购选项:
SELECT Name, Choice1, Choice2, Choice3, Choice4, Choice5, Choice6
FROM (
SELECT
S.Name AS [NAME],
'Choice' + CAST(ROW_NUMBER() OVER(PARTITION BY S.Name ORDER BY S.Name, C.Period) AS VARCHAR) AS PIVOT_CODE,
C.Name as [Class]
FROM Classes C
JOIN StudentsClasses SC ON C.ClassID = SC.ClassID
JOIN Students S ON SC.StudentID = S.StudentID
)
AS data
PIVOT
( min([Class]) FOR [PIVOT_CODE] IN
(Choice1, Choice2, Choice3, Choice4, Choice5, Choice6)
) AS pvt