select identity(int,1,1) as id,0 as nid,* into #t from 表 order by Sname,dt,Stimeupdate a
set
nid = b.cnt
from
#t a,
(select c.*,count(d.Sname) as cnt
from #t c,#t d
where c.Sname=d.Sname and c.dt=d.dt and c.Stime>=d.Stime)
where
a.Sname = b.Sname and a.dt = b.dt and a.Stime = b.Stimeselect
Sname,
dt,
T1 = max(case nid when 1 then Stime end),
T2 = max(case nid when 2 then Stime end),
T3 = max(case nid when 3 then Stime end),
T4 = max(case nid when 4 then Stime end),
T5 = max(case nid when 5 then Stime end),
T6 = max(case nid when 6 then Stime end),
SHour = isnull(datediff( hh ,max(case nid when 1 then Stime end),max(case nid when 2 then Stime end)),0)
+ isnull(datediff( hh ,max(case nid when 3 then Stime end),max(case nid when 4 then Stime end)),0)
+ isnull(datediff( hh ,max(case nid when 5 then Stime end),max(case nid when 6 then Stime end)),0)
from
#t
group by
Sname,dt
set
nid = b.cnt
from
#t a,
(select c.*,count(d.Sname) as cnt
from #t c,#t d
where c.Sname=d.Sname and c.dt=d.dt and c.Stime>=d.Stime)
where
a.Sname = b.Sname and a.dt = b.dt and a.Stime = b.Stimeselect
Sname,
dt,
T1 = max(case nid when 1 then Stime end),
T2 = max(case nid when 2 then Stime end),
T3 = max(case nid when 3 then Stime end),
T4 = max(case nid when 4 then Stime end),
T5 = max(case nid when 5 then Stime end),
T6 = max(case nid when 6 then Stime end),
SHour = isnull(datediff( hh ,max(case nid when 1 then Stime end),max(case nid when 2 then Stime end)),0)
+ isnull(datediff( hh ,max(case nid when 3 then Stime end),max(case nid when 4 then Stime end)),0)
+ isnull(datediff( hh ,max(case nid when 5 then Stime end),max(case nid when 6 then Stime end)),0)
from
#t
group by
Sname,dt
解决方案 »
- 关于恢复数据库文件时候出错?正确的SQL 应该怎么写
- 再 问 sql 语句??
- Update语句一个很令人费解的超时问题
- DBA维护时遇到的问题:SQL跟踪是一个跟踪里定义多个事件好还是分开定义好
- 谢谢了,请进来
- 打sql sp3补丁时候报错:MSSQLSERVER不是一个 Sql server2000实例
- 类的问题?
- SQL的横纵轴转换的问题?如何实现Access里的Transform的功能呀?
- 讨论:ADODB.Recordset的复制问题
- 如何将查询结果导出到电子表格。
- 怎样写触发器?
- 在sql server2000中建立一个视图(view),该视图是表A与表B的完全外联接,联完后有些字段是NULL,如何给它一个默认值?
create table #t1(Sname varchar(20),dt datetime,Stime datetime)
insert into #t1 select '张三','2005-03-11','07:36:00'
insert into #t1 select '张三','2005-03-11','12:01:00'
insert into #t1 select '张三','2005-03-11','12:39:00'
insert into #t1 select '张三','2005-03-11','19:11:00'
insert into #t1 select '李四','2005-03-11','07:30:00'
insert into #t1 select '李四','2005-03-11','12:06:00'
insert into #t1 select '李四','2005-03-11','12:36:00'
insert into #t1 select '李四','2005-03-11','17:36:00'
insert into #t1 select '李四','2005-03-11','19:36:00'
insert into #t1 select '王五','2005-03-11','07:55:00'
insert into #t1 select '王五','2005-03-11','12:01:00'
insert into #t1 select '王五','2005-03-11','12:44:00'
insert into #t1 select '王五','2005-03-11','17:04:00'
insert into #t1 select '王五','2005-03-11','17:55:00'
insert into #t1 select '王五','2005-03-11','23:03:00'
--执行交叉表查询
select identity(int,1,1) as id,0 as nid,* into #t from #t1 order by Sname,dt,Stimeupdate a
set
nid = b.cnt
from
#t a,
(select
c.Sname,c.dt,c.Stime,count(d.Sname) as cnt
from
#t c,#t d
where
c.Sname=d.Sname and c.dt=d.dt and c.Stime>=d.Stime
group by
c.Sname,c.dt,c.Stime) b
where
a.Sname = b.Sname and a.dt = b.dt and a.Stime = b.Stimeselect
Sname,
dt,
T1 = max(case nid when 1 then Stime end),
T2 = max(case nid when 2 then Stime end),
T3 = max(case nid when 3 then Stime end),
T4 = max(case nid when 4 then Stime end),
T5 = max(case nid when 5 then Stime end),
T6 = max(case nid when 6 then Stime end),
SHour = isnull(datediff( mi ,max(case nid when 1 then Stime end),max(case nid when 2 then Stime end))/60,0)
+ isnull(datediff( mi ,max(case nid when 3 then Stime end),max(case nid when 4 then Stime end))/60,0)
+ isnull(datediff( mi ,max(case nid when 5 then Stime end),max(case nid when 6 then Stime end))/60,0)
from
#t
group by
Sname,dt
--输出结果
SName Dt T1 T2 T3 T4 T5 T6 SHour
----- ---------- -------- -------- -------- -------- -------- -------- -----
李四 2005-03-11 07:30:00 12:06:00 12:36:00 17:36:00 19:36:00 NULL 9
王五 2005-03-11 07:55:00 12:01:00 12:44:00 17:04:00 17:55:00 23:03:00 13
张三 2005-03-11 07:36:00 12:01:00 12:39:00 19:11:00 NULL NULL 10
create table #t1(Sname varchar(20),dt datetime,Stime datetime)
insert into #t1 select '张三','2005-03-11','07:36:00'
insert into #t1 select '张三','2005-03-11','12:01:00'
insert into #t1 select '张三','2005-03-11','12:39:00'
insert into #t1 select '张三','2005-03-11','19:11:00'
insert into #t1 select '李四','2005-03-11','07:30:00'
insert into #t1 select '李四','2005-03-11','12:06:00'
insert into #t1 select '李四','2005-03-11','12:36:00'
insert into #t1 select '李四','2005-03-11','17:36:00'
insert into #t1 select '李四','2005-03-11','19:36:00'
insert into #t1 select '王五','2005-03-11','07:55:00'
insert into #t1 select '王五','2005-03-11','12:01:00'
insert into #t1 select '王五','2005-03-11','12:44:00'
insert into #t1 select '王五','2005-03-11','17:04:00'
insert into #t1 select '王五','2005-03-11','17:55:00'
insert into #t1 select '王五','2005-03-11','23:03:00'
--执行交叉表查询
select identity(int,1,1) as id,0 as nid,* into #t from #t1 order by Sname,dt,Stimeupdate a
set
nid = b.cnt
from
#t a,
(select
c.Sname,c.dt,c.Stime,count(d.Sname) as cnt
from
#t c,#t d
where
c.Sname=d.Sname and c.dt=d.dt and c.Stime>=d.Stime
group by
c.Sname,c.dt,c.Stime) b
where
a.Sname = b.Sname and a.dt = b.dt and a.Stime = b.Stimeselect
Sname,
dt,
T1 = max(case nid when 1 then Stime end),
T2 = max(case nid when 2 then Stime end),
T3 = max(case nid when 3 then Stime end),
T4 = max(case nid when 4 then Stime end),
T5 = max(case nid when 5 then Stime end),
T6 = max(case nid when 6 then Stime end),
SHour = isnull(datediff( mi ,max(case nid when 1 then Stime end),max(case nid when 2 then Stime end))/60,0)
+ isnull(datediff( mi ,max(case nid when 3 then Stime end),max(case nid when 4 then Stime end))/60,0)
+ isnull(datediff( mi ,max(case nid when 5 then Stime end),max(case nid when 6 then Stime end))/60,0)
from
#t
group by
Sname,dt
--输出结果
SName Dt T1 T2 T3 T4 T5 T6 SHour
----- ---------- -------- -------- -------- -------- -------- -------- -----
李四 2005-03-11 07:30:00 12:06:00 12:36:00 17:36:00 19:36:00 NULL 9
王五 2005-03-11 07:55:00 12:01:00 12:44:00 17:04:00 17:55:00 23:03:00 13
张三 2005-03-11 07:36:00 12:01:00 12:39:00 19:11:00 NULL NULL 10