下表列出了设备运行状态的前几行,每个民意调查:
samplevalue = 2
处于运营状态,samplevalue = 1
处于运营状态
source target sampletime samplevalue
------------------------------------------------------------------------------------------------
128.42.196.11 Se2/0/2(Serial2/0/2) 9/30/15 11:10 2
128.42.196.11 Fa3/0/0(FastEthernet3/0/0) 9/30/15 11:10 1
128.42.196.11 Gi1/0/0.10(GigabitEthernet1/0/0.10) 9/30/15 11:10 1
128.42.196.11 Se2/0/2.305(Serial2/0/2.305) 9/30/15 11:10 2
128.42.196.11 Se2/0/2.309(Serial2/0/2.309) 9/30/15 11:10 2
128.42.196.11 Gi1/0/0.20(GigabitEthernet1/0/0.20) 9/30/15 11:10 1
128.42.196.11 Se2/0/2.300(Serial2/0/2.300) 9/30/15 11:10 2
128.42.196.11 Gi0/0/0(GigabitEthernet0/0/0) 9/30/15 11:10 1
128.42.196.11 Se2/0/2.306(Serial2/0/2.306) 9/30/15 11:10 2
128.42.196.11 PO2/1/0(POS2/1/0--SONET/SDH Medium/Section/Line) 9/30/15 11:09 2
128.42.196.11 Tu10(Tunnel10-mpls layer) 9/30/15 11:09 2
128.42.196.11 Tu4(Tunnel4) 9/30/15 11:09 2
128.42.196.11 Gi1/0/0.40(GigabitEthernet1/0/0.40) 9/30/15 11:09 1
128.42.196.11 Se2/0/1(Serial2/0/1) 9/30/15 11:09 1
128.42.196.11 Gi1/0/0.20(GigabitEthernet1/0/0.20) 9/30/15 11:09 1
128.42.196.11 Tu10(Tunnel10) 9/30/15 11:08 2
128.42.196.11 Se2/0/0(Serial2/0/0) 9/30/15 11:08 1
128.42.196.11 Se2/0/2.309(Serial2/0/2.309) 9/30/15 11:08 2
128.42.196.11 Se2/0/2.306(Serial2/0/2.306) 9/30/15 11:08 2
从上表中我们必须计算设备的特定接口与sampletime = 1
和sampletime = 2
的时间,以及接口的最新状态。
我可以处理以下查询:
with temp as (
select
p.source,
p.target,
p.samplevalue,
p.sampletime,
lag(p.samplevalue,1) over (order by p.source, p.target, p.sampletime desc) as newvalue,
lag(p.sampletime,1) over (order by p.source, p.target, p.sampletime desc) as changedat
from INTERFACE_OPERSTATUS p
)
select *
from(
select
ROW_NUMBER() over (partition by source, target order by source, target, changedat desc) as therow,
source,
target,
samplevalue as oldvalue,
newvalue,
changedat,
DATEDIFF(MINUTE,changedat,GETDATE()) as time
FROM temp
where
newvalue <> samplevalue
and newvalue = 2
) a
where therow >= 1
以上查询的问题是:
答案 0 :(得分:-1)
以下针对Oracle数据库进行了测试,因为我没有准备好访问MS-SQL:
SELECT SOURCE, TARGET, SUM(UP_TIME) AS UP_TIME, SUM(DOWN_TIME) AS DOWN_TIME FROM (
SELECT LO.*, (SAMPLE_TIME - PREVIOUS_SAMPLE_TIME) * 24 * 60 AS ACTUAL_TIME,
CASE SAMPLE_VALUE WHEN 1 THEN ((SAMPLE_TIME - PREVIOUS_SAMPLE_TIME) * 24 * 60) ELSE 0 END AS UP_TIME,
CASE SAMPLE_VALUE WHEN 2 THEN ((SAMPLE_TIME - PREVIOUS_SAMPLE_TIME) * 24 * 60) ELSE 0 END AS DOWN_TIME
FROM
(
select SOURCE, TARGET, SAMPLE_VALUE, SAMPLE_TIME,
LAG(SAMPLE_TIME, 1, NULL) OVER (PARTITION BY SOURCE,TARGET ORDER BY SOURCE, TARGET, SAMPLE_TIME) AS PREVIOUS_SAMPLE_TIME
from INTERFACE_OPERSTATUS
) LO
) GROUP BY SOURCE, TARGET ORDER BY SOURCE, TARGET;
上面的查询应该适用于MS-SQL,因为我没有使用Oracle唯一的功能,因此语法更改很少。