滞后窗函数返回不正确的结果

时间:2015-02-17 23:47:55

标签: sql postgresql postgresql-9.3

我有一个带有各种日期戳测试结果的表,其中一些以json格式存储(本机存储在数据库中)。我尝试使用某些窗口函数在一个时间窗口中从测试结果中提取特定数据,但部分结果没有意义。有些测试每天都在运行,有些则不那么频繁。对于每个独特测试的结果,我有兴趣看到:

  • 最新结果的日期
  • 测试名称(标识符)
  • 最近的结果(小数值)
  • 过去21天的最差(最低十进制值)测试结果
  • 与过去21天的最差测试结果相对应的日期
  • 第二个最差(第二个最低十进制值)测试结果

以下是过去几周内一项测试(tname)的数据和结果得分样本:

  tstamp   |      tname      | score 
------------+-----------------+--------
2015-02-17 | dfw001.ix-cr-02 | 0.7
2015-02-15 | dfw001.ix-cr-02 | 0.6
2015-02-14 | dfw001.ix-cr-02 | 0.6
2015-02-14 | dfw001.ix-cr-02 | 0.7
2015-02-13 | dfw001.ix-cr-02 | 0.6
2015-02-12 | dfw001.ix-cr-02 | 0.7
2015-02-11 | dfw001.ix-cr-02 | 0.7
2015-02-10 | dfw001.ix-cr-02 | 0.7
2015-02-09 | dfw001.ix-cr-02 | 0.7
2015-02-08 | dfw001.ix-cr-02 | 0.7
2015-02-08 | dfw001.ix-cr-02 | 0.5
2015-02-07 | dfw001.ix-cr-02 | 0.7
2015-02-07 | dfw001.ix-cr-02 | 0.5
2015-02-06 | dfw001.ix-cr-02 | 0.7
2015-02-05 | dfw001.ix-cr-02 | 0.7
2015-02-04 | dfw001.ix-cr-02 | 0.7
2015-01-30 | dfw001.ix-cr-02 | 0.7

这是我正在运行的SQL查询:

SELECT * FROM 
    (SELECT tstamp,
            concat_ws('/',attrs->>'RCluster ID', regexp_replace(replace(replace(attrs->>'ASN HTML','</a>',''),'<a href= ''http://ncapp100.prod.com/Cluster3.php?asn=',''),'\d+(&d=5d'' target=''_blank''>)','')) AS tname ,
            metrics->>'PT TWBR' AS score, 
            first_value(metrics->>'PT TWBR') OVER 
                (PARTITION BY concat_ws('/',attrs->>'Route Cluster ID', regexp_replace(replace(replace(attrs->>'ASN HTML','</a>',''),'<a href= ''http://ncapp100.prod.com/Cluster3.php?asn=',''),'\d+(&d=5d'' target=''_blank''>)','')) 
                ORDER BY metrics->>'PT TWBR') AS worst_score,
            first_value(tstamp) OVER
                (PARTITION BY concat_ws('/',attrs->>'Route Cluster ID', regexp_replace(replace(replace(attrs->>'ASN HTML','</a>',''),'<a href= ''http://ncapp100.prod.com/Cluster3.php?asn=',''),'\d+(&d=5d'' target=''_blank''>)','')) 
                ORDER BY metrics->>'PT TWBR') AS worst_date,
            lead(metrics->>'PT TWBR', 1) OVER 
                (PARTITION BY concat_ws('/',attrs->>'Route Cluster ID', regexp_replace(replace(replace(attrs->>'ASN HTML','</a>',''),'<a href= ''http://ncapp100.prod.com/Cluster3.php?asn=',''),'\d+(&d=5d'' target=''_blank''>)','')) 
                ORDER BY metrics->>'PT TWBR') AS prv_score
    FROM btworks 
    WHERE 
            age(now(),tstamp) < '21 days' 
    ORDER BY tstamp DESC, rank
        ) AS stuff 
WHERE 
    tstamp = '2015-02-17';

以上查询中的数据与我在上面发布的数据集有关:

   tstamp   |         tname         | score | worst_score | worst_date | prv_score 
------------+-----------------------+-------+-------------+------------+-----------
 2015-02-17 | dfw001.ix-cr-02       | 0.7   | 0.5         | 2015-02-08 | 0.7

我看到的问题出在prv_score列中。它应该显示0.6的值,这与2015-02-13相对应,但是它返回0.7。我想通过metrics->>'PT TWBR'排序,我总是按分数排序,结果lead(metrics->>'PT TWBR', 1)会给我下一个最高分的价值。因此,我对ORDER BY metrics->>'PT TWBR'为什么没有按预期工作的困惑感到困惑。

提前感谢。

0 个答案:

没有答案