5

私の表は、次のようにポンプのオン/オフ状態を示しています

Value   timestamp
1       2013-09-01 00:05:41.987
0       2013-09-01 00:05:48.987
1       2013-09-01 00:05:59.987
0       2013-09-01 00:06:15.987
1       2013-09-01 00:06:34.987
etc etc. 

これらを 1 か月分取り、オン (1) の分数とオフ (0) の分数、つまりデューティ サイクルを教えてくれる MSSQL クエリが必要です。

4

3 に答える 3

1

使用CTERowNumber() function フィドルのデモ:

declare @date date = '20130925'

;with cte as (
  select value, timestamp, row_number() over(order by timestamp) rn
  from table1
)
select c1.value, sum(datediff(second, c1.timestamp, c2.timestamp)) diffInSeconds
from cte c1 join cte c2 on c1.rn = c2.rn -1
where month(c1.timestamp) = month(@date) and month(c2.timestamp) = month(@date)
group by c1.value
于 2013-10-24T20:11:05.123 に答える