Filtering by window function result in Postgresql

后端 未结 2 1319
心在旅途
心在旅途 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 01:56

    I don't know if this qualifies as "more elegant" but it is written in a different manner than Cybernate's solution (although it is essentially the same)

    WITH window_table AS 
    ( 
       SELECT s.*, 
              sum(volume) OVER previous_rows as total
       FROM stuff s
       WINDOW previous_rows as 
            (ORDER BY priority desc ROWS between UNBOUNDED PRECEDING and CURRENT ROW)
    ) 
    SELECT * 
    FROM window_table
    WHERE total < 1000
    ORDER BY priority DESC 
    

    If by "more elegant" you mean something that avoids the sub-select, then the answer is "no"

提交回复
热议问题