2012-05-18 31 views
0

我有一张表格,里面装满了学生的考勤数据,我们正在迁移到另一个学生信息系统,他们希望平面文件的格式是每个学生每天一行所有7个时期列出。现在,我们的数据每个时期每天存储一条记录(请参阅附加模式)对这些数据进行格式设置以符合我上面列出的内容的最佳方式是什么?我还附上他们想要的截图(每一行都是一列)。将一列中的数据分成多列

新增数据的截屏。

Our current Database Schema

What it needs to be

enter image description here

+0

我完全被你的问题搞糊涂了。你说他们希望列出所有的“7个时期”。我在你的模式中看不到时期......我看到了absent_period。我也看到有超过7个Abs代码期。我可以看到Abs代码周期1到Abs代码周期14.如果您使用真实数据而不仅仅是模式和布局,这将非常有帮助。 –

+0

我添加了数据的屏幕截图。有7个以上的时间适应其他学校,我们只有7个抱歉的混乱。 – Mysteri0n

+0

什么版本的SQL? –

回答

0

好了,所以支点是我需要的不是什么。我最终与一位收藏家谈论了我的问题,他告诉我使用子查询。所以这就是我解决它的方法!

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 
1

看看PIVOT和UNPIVOT。

Here就是一个例子

+0

看起来有趣,必须研究这个数字如何应用到我的情况。 – Mysteri0n

+0

从阅读那篇文章,它几乎看起来像我需要两者的组合?因为我不需要统计出席人数。我认为我需要的一个很好的例子是说我正在参加今天的出勤活动,并且我有一个缺席的学生,我会看到只有一行7个不同时期的专栏,每个专栏中都会有缺席的代码。我认为我需要使用案例陈述,但我只需要每个学生每天的日期线?思考? – Mysteri0n