Sql 将一列中的数据拆分为多列

Sql 将一列中的数据拆分为多列,sql,tsql,etl,Sql,Tsql,Etl,我有一个学生出勤数据表,我们正在迁移到另一个学生信息系统,他们希望平面文件的方式是每个学生每天一行,列出所有7个周期。现在,我们的数据被存储为每个时段每天一条记录(见附件模式),这将是格式化此数据以匹配我上面列出的内容的最佳方式。我还附上了他们想要的截图(每一行都是一列) 添加了数据的屏幕截图。 看看PIVOT和UNPIVOT 是一个例子,所以pivot不是我需要的。最后我和一位同事讨论了我的问题,他告诉我使用子查询。我就是这样解决的 select distinct student_id,s

我有一个学生出勤数据表,我们正在迁移到另一个学生信息系统,他们希望平面文件的方式是每个学生每天一行,列出所有7个周期。现在,我们的数据被存储为每个时段每天一条记录(见附件模式),这将是格式化此数据以匹配我上面列出的内容的最佳方式。我还附上了他们想要的截图(每一行都是一列)

添加了数据的屏幕截图。


看看PIVOT和UNPIVOT


是一个例子,所以pivot不是我需要的。最后我和一位同事讨论了我的问题,他告诉我使用子查询。我就是这样解决的

select distinct student_id,school_year,school_number,absent_date,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='H') as daily_code,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='1') as per_1,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='2') as per_2,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='3') as per_3,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='4') as per_4,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='5') as per_5,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='6') as per_6,
(select absent_code from attend_student_detail a2 where a2.student_id=a.student_id and a2.absent_date=a.absent_date and a2.absent_period='7') as per_7 

FROM attend_student_detail a

Order By Absent_Date, Student_ID

我完全被你的问题弄糊涂了。你说他们要列出“全部7个周期”。我在您的架构中没有看到句点。。。我看到你缺席了。我还看到有移动超过7个Abs代码周期。我看到Abs代码周期1到Abs代码周期14。如果您给出一个使用真实数据的示例,而不仅仅是模式和布局,那将非常有帮助。其他学校有超过7节课,我们只有7节课。很抱歉搞混了。看起来很有趣,我得研究一下如何将它应用到我的情况中。从那篇文章来看,我几乎需要将两者结合起来?因为我不需要合计出席人数。我想我需要的一个很好的例子是,假设我参加了今天的出勤,我有一个学生缺席,我只会看到一行,有7个不同的期间列,每个列都有缺席代码。我想我需要以某种方式使用案例陈述,但我只需要在每个学生每天的日期线上?思想?