world!
I have a first "level" table, which look loke this:
| level | id | level_date |
|---|---|---|
| 1 | A | 2021-12-02 |
| 2 | A | 2021-12-04 |
| 3 | A | 2021-12-08 |
| 1 | B | 2021-12-02 |
| 2 | B | 2021-12-05 |
| 3 | B | 2021-12-09 |
and a second "battles" table:
| id | battle_date |
|---|---|
| A | 2021-12-01 |
| A | 2021-12-03 |
| A | 2021-12-06 |
| A | 2021-12-07 |
| B | 2021-12-01 |
| B | 2021-12-02 |
| B | 2021-12-03 |
What I am trying to do is to find average a battle count, required to get to each level.
When battle_date > level_X-1_date, but battle_date < level_X_date that means that this battle is required to get to level X, and should be counted for level X.
So for player A we have one battle to get to level 1, one battle to get to level 2, and two battles to get to level 3. And for player B we have one battle to get to level 1, two battles to get to level 2, and zero battles to get to level 3
The resulting table should look like this:
| level | avg_battle_count |
|---|---|
| 1 | 1 |
| 2 | 1.5 |
| 3 | 1 |
I'm pretty sure this is kind of "gaps and islands" problem, but I don't know how exactly should i build a query which consider windowed function to count avg(battle_count) for levels

I did not tried it by I think this should yield the result you are looking for: