Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PostgreSQL中计算组度量的百分比更改_Sql_Postgresql - Fatal编程技术网

在PostgreSQL中计算组度量的百分比更改

在PostgreSQL中计算组度量的百分比更改,sql,postgresql,Sql,Postgresql,我有一个样本表如下: id | timestamp | agentid | input_interface | sourceipv4address | totalbytes_sum -------+------------+-----------------+-----------------------------+-------------------+---------------- 10733 | 1593648000 | 203.12

我有一个样本表如下:

  id   | timestamp  |     agentid     |       input_interface       | sourceipv4address | totalbytes_sum
-------+------------+-----------------+-----------------------------+-------------------+----------------
 10733 | 1593648000 | 203.121.214.129 | 203.121.214.129 interface 1 | 10.10.10.10       |           3857
 10734 | 1593648000 | 203.121.214.129 | 203.121.214.129 interface 1 | 10.10.10.101      |          45960
 10731 | 1593648600 | 203.121.214.129 | 203.121.214.129 interface 1 | 10.10.10.10       |          20579
 10736 | 1593648600 | 203.121.214.129 | 203.121.214.129 interface 1 | 10.10.10.101      |          21384
 10737 | 1593648600 | 203.121.214.129 | 203.121.214.129 interface 1 | 10.10.10.107      |           2094
此表通过每10分钟从网络中采集样本来填充。基本上,我试图构建一个视图来计算每个组(agentid、input_接口、sourceipv4address)的totalbytes_和的百分比变化,并将其显示为:

timestamp | agentid | input_interface | sourceipv4address | totalbytes_sum | percent
需要根据当前10分钟和前10分钟进行计算。我可以保证,在相同的10分钟内,特定agentid、input_接口、sourceipv4address组合只会有一个副本

如果某个组合在前10分钟内没有记录,则百分比将为+%100

我试图应用分区/顺序逻辑,但没有成功。偏移功能看起来也不错,但我被卡住了

有人能帮我吗


谢谢

您的时间戳都是一样的。我假设它们在实际数据中相隔约600秒

请尝试类似的方法,如果对您不起作用或您需要任何解释,请在评论中告诉我:

select timestamp, agentid, input_interface, 
       sourceipv4address, totalbytes_sum,
       timestamp - lag(timestamp) over w as elapsed_time,  -- illustration column
       lag(totalbytes_sum) over w as last_totalbytes_sum,  -- illustration column
       case
         when coalesce(lag(timestamp) over w, 0) = 0 then 100.0
         when timestamp - lag(timestamp) over w > 600 then 100.0
         else 100.0 * (totalbytes_sum - (lag(totalbytes_sum) over w)) / 
                        (lag(totalbytes_sum) over w)
       end as percent
  from sample_table
window w as (partition by agentid, input_interface, sourceipv4address
                 order by timestamp)
;

你的时间戳都一样。我假设它们在实际数据中相隔约600秒

请尝试类似的方法,如果对您不起作用或您需要任何解释,请在评论中告诉我:

select timestamp, agentid, input_interface, 
       sourceipv4address, totalbytes_sum,
       timestamp - lag(timestamp) over w as elapsed_time,  -- illustration column
       lag(totalbytes_sum) over w as last_totalbytes_sum,  -- illustration column
       case
         when coalesce(lag(timestamp) over w, 0) = 0 then 100.0
         when timestamp - lag(timestamp) over w > 600 then 100.0
         else 100.0 * (totalbytes_sum - (lag(totalbytes_sum) over w)) / 
                        (lag(totalbytes_sum) over w)
       end as percent
  from sample_table
window w as (partition by agentid, input_interface, sourceipv4address
                 order by timestamp)
;