繁体   English   中英

将数据从一列分解为多列

[英]Break Data out of one column into multiple columns

我有一张满是学生的出勤数据的表,我们正在迁移到另一个学生信息系统,他们希望平面文件成为每位学生每天一行的方式,列出了所有7个期间。 现在,我们的数据被存储为每个时期每天一次的记录(请参阅随附的架构),什么是格式化此数据以使其与我上面列出的内容相匹配的最佳方法。 我还要附上他们想要的屏幕截图(每行都是一列)。

添加了数据的屏幕截图。

我们当前的数据库架构

它需要是什么

在此处输入图片说明

看一下PIVOT和UNPIVOT。

是一个例子

好吧,枢轴不是我所需要的。 我最终和一个同事谈论了我的问题,他告诉我使用子查询。 所以这就是我解决的方法!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM