I have one table, example:
| ID | numbers |
| -------- | -------- |
| 1 | 200 |
| 2 | 210 |
| 3 | 320 |
| 4 | 340 |
| 5 | 360 |
| 6 | 480 |
| 7 | 490 |
| 8 | 500 |
| 9 | 610 |
| 10 | 630 |
| 11 | 700 |
Everywhere where the number change more than 100 ( ID 3,(minimum 200 on ID 1 and ID 3 320 more than 100 ) so restart the minimum search) need to find the minimum after the jump.
first minimum ID 1 after have one jump on ID 3 then restart to search the minimum and the current minimum is 320 on ID 3, after again restart the search have one jump on ID 6 and restart the minimum search the minimum is 480 on ID 6, then jump again on ID 9, so restart the min search and the current minimum is 630 on ID 9.
General minimum search not take the highest number just after, I cannot set fix number in the code ( like ID number ) because later change. I have the query what is find the more than 100 jump, and restart the minimum search.
select pos.min(numbers) as minimumtemp from example_table;
Expected output in this case is minimum = 610
Any solution is help thank you!
2
Answers
One option is to use a running sum that increased by 1 whenever the value of the numbers jumps by 100+:
see demo
step-by-step demo:db<>fiddle
numbers
value to the current record withlag()
window functionnumbers
value and check if is>= 100
numbers
value, the difference calculation would result inNULL
value. To avoid this, useCOALESCE
to set allNULL
value to true (which represents a change of calculation group like your 100 jump)true == 1
,false == 0
)numbers
jumps >= 100 (and the very first record) have an1
marker, which represent the first record of a group. Do a cumulateSUM
using an ordered window function to create group ids (each1
record increases the id, whereas each0
record gets the same id as the previous record, which means, it is the same group, meaning there was no 100 jump)DISTINCT ON (group_id)
to get only one record per ordered group. The groups must be ordered by numbers, so that the minimum number becomes the first record per group, which is taken by theDISTINCT ON
.Notice: If the
numbers
values are strictly increasing, you could stop at step 3 and simply useWHERE change == true
to get the jump records, because they are automatically the records per group with the minimum value.