sql从行到列
请帮我解决以下问题。要根据PeriodNum column的值将行数据转换为列,最大列数将达到period8。。。表中有多名教师……下面是两名教师的样本 数据样本:sql从行到列,sql,sql-server-2008,Sql,Sql Server 2008,请帮我解决以下问题。要根据PeriodNum column的值将行数据转换为列,最大列数将达到period8。。。表中有多名教师……下面是两名教师的样本 数据样本: id teachername course_name periodnum courseid ------------------------------------------------------------------ 14088 Smith, John 1; Physical E
id teachername course_name periodnum courseid
------------------------------------------------------------------
14088 Smith, John 1; Physical Education 9 GYM ABCD 1 10064
14088 Smith, John 2; Physical Education 9 GYM ABCD 2 10064
14088 Smith, John 3; Physical Education 11 GYM BD 3 10065
14088 Smith, John 5; Physical Education 11 GYM AC 5 10065
14088 Smith, John 6; Physical Education 11 GYM ABCD 6 10065
14088 Smith, John 7; Health 9 P373 ABCD 7 10059
14088 Smith, John 8; Physical Education 11 GYM AC 8 10065
14088 Smith, John 8; Health 10 GYM BD 8 10066
15411 Yong, Jerry 1; Science 6 ABCD 1 10078
15411 Yong, Jerry 2; Science 9 ABCD 2 10078
15411 Yong, Jerry 3; Science 11 BD 3 10078
15411 Yong, Jerry 5; Science 11 AC 5 10078
15411 Yong, Jerry 6; Science 11 ABCD 6 10078
15411 Yong, Jerry 7; Maths P373 ABCD 7 10080
15411 Yong, Jerry 8; Biology 11 AC 8 10041
我希望得到如下所示的预期结果:
id teachername Period1 Period2 Period3 Period4 Period5 Period6 Period7 Period8
14088 Smith, John 1; Physical Education 9 GYM ABCD 2; Physical Education 9 GYM ABCD 3; Physical Education 11 GYM BD NULL 5; Physical Education 11 GYM AC 6; Physical Education 11 GYM ABCD 7; Health 9 P373 ABCD 8; Physical Education 11 GYM AC + 8; Health 10 GYM BD
15411 Yong, Jerry 1; Science 6 ABCD 2; Science 9 ABCD 3; Science 11 BD 5; Science 11 AC 6; Science 11 ABCD 7; Maths P373 ABCD 8; Biology 11 AC
非常感谢
我尝试了以下查询
SELECT DISTINCT
t.[id],
t.[teachername]
,t1.course_name AS period1,t2.course_name AS period2,t3.course_name AS period3, t4.course_name AS period4, t5.course_name AS period5, t6.course_name AS period6,
t7.course_name AS period7,t8.course_name AS period8 -- into ccs_test
FROM @teacher t
LEFT OUTER JOIN CompanyGrouped t1 ON t.[id]=t1.[id] AND t1.ColumnNumber=1 and t.periodnum=1
LEFT OUTER JOIN CompanyGrouped t2 ON t.[id]=t2.[id] AND t2.ColumnNumber=2 and t.periodnum=2
LEFT OUTER JOIN CompanyGrouped t3 ON t.[id]=t3.[id] AND t3.ColumnNumber=3 and t.periodnum=3
LEFT OUTER JOIN CompanyGrouped t4 ON t.[id]=t4.[id] AND t4.ColumnNumber=4 and t.periodnum=4
LEFT OUTER JOIN CompanyGrouped t5 ON t.[id]=t5.[id] AND t5.ColumnNumber=5 and t.periodnum=5
LEFT OUTER JOIN CompanyGrouped t6 ON t.[id]=t6.[id] AND t6.ColumnNumber=6 and t.periodnum=6
LEFT OUTER JOIN CompanyGrouped t7 ON t.[id]=t7.[id] AND t7.ColumnNumber=7 and t.periodnum=7
LEFT OUTER JOIN CompanyGrouped t8 ON t.[id]=t8.[id] AND t7.ColumnNumber=8 and t.periodnum=8
问候,,
HT我将数据放入SQL Server中的一个表中,并能够以您所寻找的结果为中心
SELECT ID
, TeacherName
,[1] as Period1
,[2] as Period2
,[3] as Period3
,[4] as Period4
,[5] as Period5
,[6] as Period6
,[7] as Period7
,[8] as Period8
FROM
(SELECT ID, TeacherName, peridonum, course_name
FROM teacher) src
PIVOT
(MAX(course_name) FOR peridonum in ([1],[2],[3],[4],[5],[6],[7],[8])
) pvt
请格式化您的代码,使其实际可读,并提供您已经尝试过的查询。您使用的数据库是什么,因为某些数据库上的函数可能会重复此操作,感谢@AHiggins为我指出解决方案对我有效的线程。感谢@Vinnie我的问题现在已解决。。。再次非常感谢。