简体   繁体   English

sql来填写缺失的数据?

[英]sql to fill in missing data?

Giving the following query" 提供以下查询”

select ping_date,packet_loss,ping_avg,ping_source,ping_destination
from router_ping 
where ping_date > sysdate - NUMTODSINTERVAL (24, 'HOUR') AND (ping_source = 'zja68f-wr2' AND ping_destination = 'zja68f-wr2'  OR ping_source = 'zja68f-wr2' AND ping_destination = 'zja68f-wr2' )
order by TRUNC(ping_date), ping_date ASC

Will output: 将输出:

 PACKET_LOSS    PING_AVG    PING_DATE   PING_DESTINATION    PING_SOURCE
0                 273   2015-05-07 17:40:16.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 17:45:27.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 17:50:15.0   zja68f-wr2      zfr11f-wr2

0                 273   2015-05-07 18:00:19.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 18:05:18.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 18:10:15.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 18:15:12.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 18:20:13.0   zja68f-wr2      zfr11f-wr2
0                 273   2015-05-07 18:25:14.0   zja68f-wr2      zfr11f-wr2

This result there is a missing row for 2015-05-07 17:55:xx.x . 结果是2015-05-07 17:55:xx.x缺少一行。 Can this be dynamically added for any missing rows? 可以为缺少的行动态添加吗? adding a null value for PACKET_LOSS and PING_AVG column. 为PACKET_LOSS和PING_AVG列添加一个空值。

You might want to consider if my rather arduous solution is worth the complexity or not. 您可能要考虑我的繁重解决方案是否值得复杂性解决。 However, by using a connect by "trick" in Oracle, we can simulate this behavior by incrementing in 5 minute intervals to dynamically create the time table. 但是,通过在Oracle中使用“按技巧”连接,我们可以通过以5分钟为间隔递增以动态创建时间表来模拟此行为。 Then joining it to he router_ping table and by noting the row before and after me to determine if I'm in the middle of "router ping sequence" and show data as appropriate. 然后将其加入到router_ping表中,并注意我之前和之后的行,以确定我是否处于“路由器ping序列”的中间,并显示适当的数据。 (Note: I had to change your where clause as I wasn't getting any data returned based on your output.) (注意:由于没有根据您的输出返回任何数据,因此我不得不更改了where子句。)

select ping_date, packet_loss, ping_avg, coalesce(ping_source, previous_ping_source), coalesce(ping_destination, previous_ping_destination)
from
(
select coalesce(ping_date, ping_interval) as ping_date, 
coalesce(pings.packet_loss, dates.packet_loss) as packet_loss,
coalesce(pings.ping_avg, dates.ping_avg) as ping_avg,
pings.ping_source, 
pings.ping_destination,
coalesce(pings.ping_source, lag(pings.ping_source, 1, 0) over (order by ping_interval ASC)) previous_ping_source,
coalesce(pings.ping_destination, lag(pings.ping_destination, 1, 0) over (order by ping_interval ASC)) previous_ping_destination,
coalesce(pings.ping_source, lead(pings.ping_source, 1, 0) over (order by ping_interval ASC)) next_ping_source
from
(select ping_date,packet_loss,ping_avg,ping_source,ping_destination
from router_ping 
where ping_date > sysdate - NUMTODSINTERVAL (48, 'HOUR') AND (ping_source = 'zfr11f-wr2' AND ping_destination = 'zja68f-wr2'  OR ping_source = 'zja68f-wr2' AND ping_destination = 'zja68f-wr2' )
) pings
right outer join
(select (select min(ping_date) from router_ping) + (interval '5' minute) * level as ping_interval,
0 as packet_loss, 0 as ping_avg, 'NO DATA ROW' as data_indicator
from dual
connect by level <= 20) dates
on (pings.ping_date = dates.ping_interval))
where (previous_ping_source is not null and next_ping_source is not null)
order by ping_date
; 

Sample data and table used for example: 示例数据和表格例如:

create table router_ping
(ping_date timestamp,
ping_destination varchar2(50),
ping_source varchar2(50),
ping_avg    number,
packet_loss number);

insert into router_ping 
(ping_date, ping_destination, ping_source, ping_avg, packet_loss)
values
(to_date('2015-05-08 17:40:16','YYYY-MM-DD HH24:MI:SS'), 'zja68f-wr2', 'zfr11f-wr2', 273, 0);

insert into router_ping 
(ping_date, ping_destination, ping_source, ping_avg, packet_loss)
values
(to_date('2015-05-08 17:45:16','YYYY-MM-DD HH24:MI:SS'), 'zja68f-wr2', 'zfr11f-wr2', 273, 0);

insert into router_ping 
(ping_date, ping_destination, ping_source, ping_avg, packet_loss)
values
(to_date('2015-05-08 17:50:16','YYYY-MM-DD HH24:MI:SS'), 'zja68f-wr2', 'zfr11f-wr2', 273, 0);

insert into router_ping 
(ping_date, ping_destination, ping_source, ping_avg, packet_loss)
values
(to_date('2015-05-08 18:00:16','YYYY-MM-DD HH24:MI:SS'), 'zja68f-wr2', 'zfr11f-wr2', 273, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM