Sample Header Ad - 728x90

How to sum values within blocks with Postgres?

-1 votes
1 answer
141 views
Given this table called prices: | my_date | my_value | my_separator | |:---- |:------:| -----:| | 2012-02-10 | 100 | 1 | | 2012-02-11 | 120 | 1 | | 2012-02-12 | 120 | 1 | | 2012-02-13 | 110 | 1 | | 2012-02-14 | 100 | 0 | | 2012-02-15 | 115 | 0 | | 2012-02-16 | 130 | 1 | | 2012-02-17 | 190 | 1 | | 2012-02-18 | 180 | 1 | | 2012-02-19 | 150 | 0 | | 2012-02-20 | 130 | 1 | I need to sum the values in my_value column based on the condition that my_separator is 1 within that group (so excluding the values where my_separator = 0). That should return the following values: * for the date range 2012-02-10-2012-02-13 => 100 + 120 + 120 + 110 = 450 * for the date range 2012-02-16-2012-02-18 => 130 + 190 + 180 = 500 * for the date 2012-02-20 => 130 I tried using window functions but I could not come up with anything valid. Any help?
Asked by Randomize (1203 rep)
Aug 2, 2024, 01:39 PM
Last activity: Aug 2, 2024, 06:21 PM