现有一个表,其数据如下,同一个卡一天可以得到6笔以下有效数据:
ID 卡号 打卡时间
1 100000 20100801 08:00
1 100000 20100801 12:00
1 100000 20100801 13:00
1 100000 20100801 17:00
1 100000 20100801 17:20
1 100000 20100801 19:20
2 100020 20100801 08:00
2 100020 20100801 13:00
2 100020 20100801 17:00
要实现的是将以上的数据插入一个考勤日档表如:
卡号 打卡时间1 打卡时间2 打卡时间3 打卡时间4 打卡时间5 打卡时间6
100000 20100801 08:00 20100801 12:00 20100801 13:00 20100801 17:00 20100801 17:20 20100801 19:20
100020 20100801 08:00 20100801 13:00 20100801 17:00
应该如何写代码~
ID 卡号 打卡时间
1 100000 20100801 08:00
1 100000 20100801 12:00
1 100000 20100801 13:00
1 100000 20100801 17:00
1 100000 20100801 17:20
1 100000 20100801 19:20
2 100020 20100801 08:00
2 100020 20100801 13:00
2 100020 20100801 17:00
要实现的是将以上的数据插入一个考勤日档表如:
卡号 打卡时间1 打卡时间2 打卡时间3 打卡时间4 打卡时间5 打卡时间6
100000 20100801 08:00 20100801 12:00 20100801 13:00 20100801 17:00 20100801 17:20 20100801 19:20
100020 20100801 08:00 20100801 13:00 20100801 17:00
应该如何写代码~
select a.卡号
,max(case when rn=1 then a.打卡时间 end)
,max(case when rn2 then a.打卡时间 end)
,max(case when rn=3 then a.打卡时间 end)
,max(case when rn=4 then a.打卡时间 end)
,max(case when rn=5 then a.打卡时间 end)
,max(case when rn=6 then a.打卡时间 end)
from
(select t1.*,row_number() over(partition by 卡号 ordeer by 打卡时间) rn
from t1
)a
group by a.卡号
如果是不定的话,参考
http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?73160
select a.卡号
,max(case when rn=1 then a.打卡时间 end)
,max(case when rn2 then a.打卡时间 end)
,max(case when rn=3 then a.打卡时间 end)
,max(case when rn=4 then a.打卡时间 end)
,max(case when rn=5 then a.打卡时间 end)
,max(case when rn=6 then a.打卡时间 end)
from
(select t1.*,row_number() over(partition by 卡号 ordeer by 打卡时间) rn
from t1
)a
group by a.卡号
首先对t2表不要产生nolog
alter table t2 nologging
然后插入
2、也可以进行多进程插入。