Filtering by window function result in Postgresql

后端 未结 2 1293
心在旅途
心在旅途 2021-02-07 01:34

Ok, initially this was just a joke we had with a friend of mine, but it turned into interesting technical question :)

I have the following stuff table:

2条回答
  •  爱一瞬间的悲伤
    2021-02-07 02:17

    I haven't worked with PostgreSQL. However, my best guess would be using an inline view.

    SELECT a.*
    FROM (
        SELECT s.*, sum(volume) OVER previous_rows AS total
        FROM stuff AS s
        WINDOW previous_rows AS (
             ORDER BY priority desc
             ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
        )
        ORDER BY priority DESC
    ) AS a
    WHERE a.total < 1000;
    

提交回复
热议问题