数据表如下: PDCHistoryID CurDT RunState
1 2012-7-30 9:21:29 Run
2 2012-7-30 9:21:32 Run
3 2012-7-30 9:21:33 Stop
4 2012-7-30 9:21:34 Run
5 2012-7-30 9:21:35 Run
6 2012-7-30 9:21:37 Run
7 2012-7-30 9:21:38 Stop
8 2012-7-30 9:21:39 Stop
9 2012-7-30 9:21:40 Stop
10 2012-7-30 9:21:41 Stop
11 2012-7-30 9:21:42 Stop
12 2012-7-30 9:21:43 Stop
13 2012-7-30 9:21:44 Run
14 2012-7-30 9:21:45 Run
15 2012-7-30 9:21:46 Run
16 2012-7-30 9:21:48 Run
17 2012-7-30 9:21:49 Run
18 2012-7-30 9:21:50 Stop
19 2012-7-30 9:21:51 Stop
20 2012-7-30 9:21:52 Stop
21 2012-7-30 9:21:53 Stop
22 2012-7-30 9:21:54 Stop
23 2012-7-30 9:21:55 Stop
24 2012-7-30 9:21:56 Stop
25 2012-7-30 9:21:58 Stop
26 2012-7-30 9:21:59 Stop计算运行时长方法:连续运行状态为'Run',直到状态变为'Stop',1.用Stop状态的时间减去一开始状态为'Run'的时间;2.用连续状态为'Run'的最后一条记录的时间减去第一条记录的时间。按时间升序。所有类似的情况,对差值求和。计算停机时长方法:连续运行状态为'Stop',直到状态变为'Run',1.用Run状态的时间减去一开始状态为'Stop'的时间;2.用连续状态为'Stop'的最后一条记录的时间减去第一条记录的时间。按时间升序。所有类似的情况,对差值求和。通过SQL语句得到设备的运行多长时间和停止多长时间,精确到秒,
结果数据如下(例如):RunState Seconds
Run 10
Stop 10
我自己弄了一个方法,但是效率似乎不高,如下:
第一步:先建立一下临时表‘temp’.增加一个自动排序ID字段“RowIndex”。
第二步:先把状态为‘Run’的记录提取出来。并把记录插入到表“temp”
第三步:用SQL语句计算运行时长。如下
select sum(DateDiff('s',t3.sdt,t3.edt)) as seconds
from(
select min(t2.CurDT) as sdt, max(t2.CurDT) as edt
from (
select t1.PDCHistoryID, t1.CurDT,(t1.PDCHistoryID - t1.RowIndex ) as delta
from
(
select PDCHistoryID,RowIndex,CurDT from temp order by PDCHistoryID ASC
) AS t1
)
AS t2
group by t2.delta
) as t3
注:停止时长类似。还有其他好方法没???
1 2012-7-30 9:21:29 Run
2 2012-7-30 9:21:32 Run
3 2012-7-30 9:21:33 Stop
4 2012-7-30 9:21:34 Run
5 2012-7-30 9:21:35 Run
6 2012-7-30 9:21:37 Run
7 2012-7-30 9:21:38 Stop
8 2012-7-30 9:21:39 Stop
9 2012-7-30 9:21:40 Stop
10 2012-7-30 9:21:41 Stop
11 2012-7-30 9:21:42 Stop
12 2012-7-30 9:21:43 Stop
13 2012-7-30 9:21:44 Run
14 2012-7-30 9:21:45 Run
15 2012-7-30 9:21:46 Run
16 2012-7-30 9:21:48 Run
17 2012-7-30 9:21:49 Run
18 2012-7-30 9:21:50 Stop
19 2012-7-30 9:21:51 Stop
20 2012-7-30 9:21:52 Stop
21 2012-7-30 9:21:53 Stop
22 2012-7-30 9:21:54 Stop
23 2012-7-30 9:21:55 Stop
24 2012-7-30 9:21:56 Stop
25 2012-7-30 9:21:58 Stop
26 2012-7-30 9:21:59 Stop计算运行时长方法:连续运行状态为'Run',直到状态变为'Stop',1.用Stop状态的时间减去一开始状态为'Run'的时间;2.用连续状态为'Run'的最后一条记录的时间减去第一条记录的时间。按时间升序。所有类似的情况,对差值求和。计算停机时长方法:连续运行状态为'Stop',直到状态变为'Run',1.用Run状态的时间减去一开始状态为'Stop'的时间;2.用连续状态为'Stop'的最后一条记录的时间减去第一条记录的时间。按时间升序。所有类似的情况,对差值求和。通过SQL语句得到设备的运行多长时间和停止多长时间,精确到秒,
结果数据如下(例如):RunState Seconds
Run 10
Stop 10
我自己弄了一个方法,但是效率似乎不高,如下:
第一步:先建立一下临时表‘temp’.增加一个自动排序ID字段“RowIndex”。
第二步:先把状态为‘Run’的记录提取出来。并把记录插入到表“temp”
第三步:用SQL语句计算运行时长。如下
select sum(DateDiff('s',t3.sdt,t3.edt)) as seconds
from(
select min(t2.CurDT) as sdt, max(t2.CurDT) as edt
from (
select t1.PDCHistoryID, t1.CurDT,(t1.PDCHistoryID - t1.RowIndex ) as delta
from
(
select PDCHistoryID,RowIndex,CurDT from temp order by PDCHistoryID ASC
) AS t1
)
AS t2
group by t2.delta
) as t3
注:停止时长类似。还有其他好方法没???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货