在postgres中查找系列的长度

时间:2018-05-14 22:47:37

标签: postgresql window-functions

postgres的棘手问题。想象一下,我有一组行,其中包含一个名为(例如)成功的布尔列。像这样:

id | success  
9  | false  
8  | false  
7  | true  
6  | true  
5  | true  
4  | false  
3  | false  
2  | true  
1  | false  

我需要计算最新(非)成功系列的长度。 E. g。在这种情况下,它将是" 3"成功和" 2"没有成功。或者使用窗口函数,然后像:

id | success | length  
9  | false   | 2  
8  | false   | 2  
7  | true    | 3  
6  | true    | 3  
5  | true    | 3  
4  | false   | 1  
3  | true    | 2  
2  | true    | 2  
1  | false   | 1  

(请注意,我通常只需要最新系列的长度,而不是所有这些系列)

我到目前为止找到的最接近的答案是这篇文章: https://jaxenter.com/10-sql-tricks-that-you-didnt-think-were-possible-125934.html (见#5)

然而,postgres并不支持" IGNORE NULLS"选项,以便查询不起作用。没有" IGNORE NULLS"它只是在长度列中返回空值。

这是我能得到的最接近的地方:

WITH
  trx1(id, success, rn) AS (
    SELECT id, success, row_number() OVER (ORDER BY id desc) 
    FROM results
  ),
  trx2(id, success, rn, lo, hi) AS (
    SELECT trx1.*,
    CASE WHEN coalesce(lag(success) OVER (ORDER BY id DESC), FALSE) != success THEN rn END,
    CASE WHEN coalesce(lead(success) OVER (ORDER BY id DESC), FALSE) != success THEN rn END
    FROM trx1
  )
SELECT trx2.*, 1
- last_value (lo) IGNORE nulls OVER (ORDER BY id DESC ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW)
+ first_value(hi) OVER (ORDER BY id DESC ROWS BETWEEN CURRENT ROW
AND UNBOUNDED FOLLOWING)
AS length FROM trx2;

您对此类查询有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您可以使用窗口函数row_number()来指定系列:

select max(id) as max_id, success, count(*) as length
from (
    select *, row_number() over wa - row_number() over wp as grp
    from my_table
    window
        wp as (partition by success order by id desc),
        wa as (order by id desc)
    ) s
group by success, grp
order by 1 desc

 max_id | success | length 
--------+---------+--------
      9 | f       |      2
      7 | t       |      3
      4 | f       |      2
      2 | t       |      1
      1 | f       |      1
(5 rows)

DbFiddle.

答案 1 :(得分:0)

尽管Klin的回答完全正确,但我想发布另一个我朋友建议的解决方案:

with last_success as (
  select max(id) id from my_table where success
)
select count(mt.id) last_fails_count
from my_table mt, last_success lt
where mt.id > lt.id;

--------------------
| last_fails_count |
--------------------
| 2                |
--------------------

DbFiddle

如果我只需要获得最后一次失败或成功的系列,那么速度提高两倍。