获取记录,直到总和等于某个否

时间:2019-07-08 14:13:18

标签: postgresql

下面的查询对我来说很好,但是我想要的是,如果so_far类似于297,下一个some_num_field为4,则查询将返回记录,总计为297。在这种情况下,它应该获得下一行由created_at的some_num_field值小于<= 3(300-297)

的订单
WITH eligible as (
    select id, name, some_num_field,
        sum(some_num_field) over (ORDER BY created_at) as so_far
    from test
)

SELECT *
FROM eligible
WHERE so_far <= 300

0 个答案:

没有答案