SQL 语句!!!!!!!!
表TT
idno icdate ictime
000008 2007/09/26 08:00:00
000008 2007/09/26 12:30:00
000008 2007/09/26 13:30:00
000008 2007/09/26 17:00:00
000008 2007/09/27 00:30:00
得到结果
idno icdate T1 T2 T3 T4 T5 T6 T7 T8
000008 2007/09/26 08:00:00 12:30:00 13:30:00 17:00:00 00:30:00 NULL NULL NULL
------解决方案--------------------我是来学习的
------解决方案--------------------没看懂
------解决方案----------------------如果你的值不满八个,使用静态SQL
create table tb(idno varchar(10),icdate varchar(10),ictime varchar(10))
insert into tb values('000008', '2007/09/26', '08:00:00')
insert into tb values('000008', '2007/09/26', '12:30:00')
insert into tb values('000008', '2007/09/26', '13:30:00')
insert into tb values('000008', '2007/09/26', '17:00:00')
insert into tb values('000008', '2007/09/27', '00:30:00')
go
select idno ,icdate,
max(case px when 1 then ictime end) T1,
max(case px when 2 then ictime end) T2,
max(case px when 3 then ictime end) T3,
max(case px when 4 then ictime end) T4,
max(case px when 5 then ictime end) T5,
max(case px when 6 then ictime end) T6,
max(case px when 7 then ictime end) T7,
max(case px when 8 then ictime end) T8
from
(
select px=(select count(1) from tb where idno=a.idno and icdate = a.icdate and ictime<a.ictime)+1 , * from tb a
) t
group by idno,icdate
drop table tb
/*
idno icdate T1 T2 T3 T4 T5 T6 T7 T8
------ ---------- -------- -------- -------- -------- ---- ---- ---- ----
000008 2007/09/26 08:00:00 12:30:00 13:30:00 17:00:00 NULL NULL NULL NULL
000008 2007/09/27 00:30:00 NULL NULL NULL NULL NULL NULL NULL
(所影响的行数为 2 行)
*/