日期:2014-05-17 浏览次数:20604 次
create table 考勤表(carddate datetime, empno varchar(10),cardtime2 varchar(10))
insert into 考勤表
select '2014-02-12 00:00:00', 'A012152', '13:23:16' union all
select '2014-02-12 00:00:00', 'A012152', '12:04:01' union all
select '2014-02-12 00:00:00', 'A012152', '17:35:21' union all
select '2014-02-12 00:00:00', 'A012152', '07:45:56' union all
select '2014-02-11 00:00:00', 'A012152', '13:19:18' union all
select '2014-02-11 00:00:00', 'A012152', '12:03:26' union all
select '2014-02-11 00:00:00', 'A012152', '07:44:19' union all
select '2014-02-11 00:00:00', 'A012152', '17:35:22' union all
select '2014-02-10 00:00:00', 'A012152', '17:34:14' union all
select '2014-02-10 00:00:00', 'A012152', '13:22:35' union all
select '2014-02-10 00:00:00', 'A012152', '12:02:54' union all
select '2014-02-10 00:00:00', 'A012152', '07:44:33'
go
if OBJECT_ID('tempdb..#temp') is not null
drop table #temp
if OBJECT_ID('tempdb..#temp1') is not null
drop table #temp1
select *,identity(int,1,1) as id into #temp
from 考勤表
select *,
(select COUNT(*) from #temp b
where a.empno = b.empno and a.carddate = b.carddate and a.id >= b.id) rownum
into #temp1
from #temp a
select carddate,
empno,
MAX(case when rownum = 1 then cardtime2 else null end) as cardtime1,
MAX(case when rownum = 2 then cardtime2 else null end) as cardtime1,
MAX(case when rownum = 3 then cardtime2&nbs