27,580
社区成员
发帖
与我相关
我的任务
分享
select a.id,a.time as Intime, b.Time as OutTime,
datediff(ss/*以秒记*/,a.time,b.time) as LT
from tb a cross apply
(select top (1)* from tb b where a.ID = b.ID and b.time > a.time
and b.device = 2 order by b.time desc)p
where a.device = 1
select a.id,a.time as Intime, b.Time as OutTime, datediff(ss/*以秒记*/,a.time,b.time) as LT from tb a cross apply (select top (1)* from tb b where a.ID = b.ID and b.time > a.time and b.device = 2 order by b.time desc)p where a.device = 1
select a.id ,b.Intime,b.OutTime,
datediff(s,b.OutTime,b.Intime) as LT
from #a as a cross apply(
select case when device=1 then max(time) end as Intime,
case when device=2 then min(time) end as OutTime from #a where id =a.id group by device ) as b
select #a.id ,b.Intime,b.OutTime,
datediff(s,b.OutTime,b.Intime) as LT
from #a cross join(
select case when device=1 then max(time) end as Intime,
case when device=2 then min(time) end as OutTime from #a group by device) as b
with CET AS (select a.id,a.time as Intime, b.Time as OutTime,
datediff(ss/*以秒记*/,a.time,b.time) as LT ,row_number()over(partition by a.time order by b.time) as Brn, row_number()over(partition by b.time order by a.time) as Ern
from tb a cross apply
(select top (1)* from tb b where a.ID = b.ID and b.time > a.time
and b.device = 2 order by b.time )b--错是因为这个别名 不小心打成P了,desc确实应该删掉,across apply的意思是对每一行进行计算
where a.device = 1 )
select * from CET where Brn = 1 and Ern = 1
with CET AS (select a.id,a.time as Intime, b.Time as OutTime,
datediff(ss/*以秒记*/,a.time,b.time) as LT ,row_number()over(partition by a.time order by b.time) as Brn, row_number()over(partition by b.time order by a.time) as Ern
from tb a cross apply
(select top (1)* from tb b where a.ID = b.ID and b.time > a.time
and b.device = 2 order by b.time desc)p
where a.device = 1 )
select * from CET where Brn = 1 and Ern = 1
select a.id,a.time as Intime, b.Time as OutTime,
datediff(ss/*以秒记*/,a.time,b.time) as LT
from tb a cross apply
(select top (1)* from tb b where a.ID = b.ID and b.time > a.time
and b.device = 2 order by b.time desc)b
where a.device = 1
WITH test (id,TIME,device)
AS
(
SELECT 1,'2012-09-23 12:10:01',1
UNION ALL
SELECT 1,'2012-09-24 12:10:01',2 --模拟跨天
UNION ALL
SELECT 2,'2012-09-23 12:10:01',1
UNION ALL
SELECT 2,'2012-09-23 14:10:01',2 --模拟当天
)
--SELECT * FROM test
SELECT id,InTime=MAX(CASE WHEN device=1 THEN TIME END ),OutTime=MAX(CASE WHEN device=2 THEN TIME END ),
CASE WHEN DATEPART(DAY,MAX(CASE WHEN device=1 THEN TIME END ))=DATEPART(DAY,MAX(CASE WHEN device=2 THEN TIME END ))
THEN CONVERT(VARCHAR(10),DATEDIFF (hh,MAX(CASE WHEN device=1 THEN TIME END ),MAX(CASE WHEN device=2 THEN TIME END)))+'小时'
+ CONVERT(VARCHAR(10),DATEDIFF (mi,DATEPART(mi,MAX(CASE WHEN device=1 THEN TIME END )),DATEPART(mi,MAX(CASE WHEN device=2 THEN TIME END))))
+'分'+CONVERT(VARCHAR(10),DATEDIFF (ss,DATEPART(ss,MAX(CASE WHEN device=1 THEN TIME END )),DATEPART(ss,MAX(CASE WHEN device=2 THEN TIME END))))+'秒'
ELSE CONVERT(VARCHAR(10),DATEDIFF (hh,MAX(CASE WHEN device=1 THEN TIME END ),MAX(CASE WHEN device=2 THEN TIME END)))+'小时'
+ CONVERT(VARCHAR(10),DATEDIFF (mi,DATEPART(mi,MAX(CASE WHEN device=1 THEN TIME END )),DATEPART(mi,MAX(CASE WHEN device=2 THEN TIME END))))
+'分'+CONVERT(VARCHAR(10),DATEDIFF (ss,DATEPART(ss,MAX(CASE WHEN device=1 THEN TIME END )),DATEPART(ss,MAX(CASE WHEN device=2 THEN TIME END))))+'秒'
END
FROM test
GROUP BY id
/*
----------- ------------------- ------------------- --------------------------------------
1 2012-09-23 12:10:01 2012-09-24 12:10:01 24小时0分0秒
2 2012-09-23 12:10:01 2012-09-23 14:10:01 2小时0分0秒
警告: 聚合或其他 SET 操作消除了 Null 值。
(2 行受影响)
*/