以预期结果为例,你没有显示<条码>。 例如,你按日期分组。 举例来说,按日期进行分组,并计算整个午夜的幅度。
段 次 页 次
( 2024-03-07 23:10:00 )->( 2024-03-07 23:59:59 ) date 2024-03-07
( 2024-03-08 00:00:00 )->( 2024-03-08 03:10:00 ) date 2024-03-08
See example
select Machine,logDate
,sum(case when Status= good then elong end) GoodLong
,sum(case when Status= bad then elong end) BadLong
,round(cast(sum(case when Status= good then elong end) as float)/3600.0,4) GoodLongH
,round(cast(sum(case when Status= bad then elong end) as float)/3600.0,4) BadLongH
from(
select *,cast(logtime as date) LogDate
,datediff(s,logtime,lead(logtime,1,logtime)over(order by logtime)) elong
from( -- for over-midnight periods
select Machine, Status,n
,case when n=1 and logDate<>nextDate then
cast(nextDate as datetime)
else logTime
end LogTime
,case when n=0 and logDate<>nextDate then dateadd(s,-1,cast(nextDate as datetime))
else nextTime
end nextTime
from(
select *
,cast(logtime as date) logDate
,lead(logtime,1,logtime)over(order by logtime) nextTime
,cast(lead(logtime,1,logtime)over(order by logtime) as date) nextDate
from Logwork) a
left join (select 0 n union all select 1 )t(n)
on (n=0 ) or (n=1 and logDate<>nextDate )
)b
)с
group by Machine,LogDate
每天24小时
insert into LogWork values
( Mac1 , good , 2024-03-07 1:10:05 )
,( Mac1 , good , 2024-03-07 2:15:00 )
,( Mac1 , good , 2024-03-07 3:20:00 )
,( Mac1 , bad , 2024-03-07 4:10:00 )
,( Mac1 , good , 2024-03-07 5:10:00 )
,( Mac1 , good , 2024-03-07 11:10:00 )
,( Mac1 , bad , 2024-03-07 15:10:00 )
,( Mac1 , good , 2024-03-07 16:10:00 )
,( Mac1 , good , 2024-03-07 23:10:00 )
,( Mac1 , bad , 2024-03-08 03:10:00 )
,( Mac1 , good , 2024-03-08 05:10:00 )
;
产出
Machine |
logDate |
GoodLong |
BadLong |
GoodLongH |
BadLongH |
Mac1 |
2024-03-07 |
74995 |
7200 |
20.8319 |
2 |
Mac1 |
2024-03-08 |
11400 |
7200 |
3.1667 |
2 |
计算th夜产出的顺序
Machine |
Status |
n |
LogTime |
nextTime |
LogDate |
elong |
Mac1 |
good |
0 |
2024-03-07 01:10:05.000 |
2024-03-07 02:15:00.000 |
2024-03-07 |
3895 |
Mac1 |
good |
0 |
2024-03-07 02:15:00.000 |
2024-03-07 03:20:00.000 |
2024-03-07 |
3900 |
Mac1 |
good |
0 |
2024-03-07 03:20:00.000 |
2024-03-07 04:10:00.000 |
2024-03-07 |
3000 |
Mac1 |
bad |
0 |
2024-03-07 04:10:00.000 |
2024-03-07 05:10:00.000 |
2024-03-07 |
3600 |
Mac1 |
good |
0 |
2024-03-07 05:10:00.000 |
2024-03-07 11:10:00.000 |
2024-03-07 |
21600 |
Mac1 |
good |
0 |
2024-03-07 11:10:00.000 |
2024-03-07 15:10:00.000 |
2024-03-07 |
14400 |
Mac1 |
bad |
0 |
2024-03-07 15:10:00.000 |
2024-03-07 16:10:00.000 |
2024-03-07 |
3600 |
Mac1 |
good |
0 |
2024-03-07 16:10:00.000 |
2024-03-07 23:10:00.000 |
2024-03-07 |
25200 |
Mac1 |
good |
0 |
2024-03-07 23:10:00.000 |
2024-03-07 23:59:59.000 |
2024-03-07 |
3000 |
Mac1 |
good |
1 |
2024-03-08 00:00:00.000 |
2024-03-08 03:10:00.000 |
2024-03-08 |
11400 |
Mac1 |
bad |
0 |
2024-03-08 03:10:00.000 |
2024-03-08 05:10:00.000 |
2024-03-08 |
7200 |
Mac1 |
good |
0 |
2024-03-08 05:10:00.000 |
2024-03-08 05:10:00.000 |
2024-03-08 |
0 |