我需要一些帮助来将 eccesive 值拆分为以下行

I need some help in splitting an eccesive value into following rows

我在这个逻辑上需要一些帮助: 我有一个这样创建的 table:

我需要一个SQL,我根据这个逻辑计算一个新的 ImpegnoSlot:

每行不能有高于 settings.MaxImpegno 的列 impegno(在本例中为 3),所以我需要一个 select,其中 eccessive 数量被添加到以下行,最多为 3 ,像这样(不工作)

SELECT p.IdSlot, 
   p.Orario, 
   p.ImpegnoSlot,
   IF(p.NewImpegno < s.MaxImpegnoForno, p.ImpegnoSlot, p.NewImpegno) as ImpegnoNew FROM (SELECT es.*,               
           ( es.ImpegnoSlot + 
             (SELECT IFNULL(SUM(es2.ImpegnoSlot - s.MaxImpegnoForno), 0)
                FROM elenco_slot es2,
                     settings s
               WHERE es2.Orario < es.Orario
                 AND (es2.ImpegnoSlot - s.MaxImpegnoForno) > 0
              ORDER BY es2.orario)
           ) as NewImpegno
     FROM elenco_slot es
    WHERE 1 = 1
    ORDER BY es.orario) p,
   settings s ORDER BY p.Orario

我需要的是这个:

有人可以帮忙吗? 谢谢

编辑 1:

我以文本形式提供 table 数据:

1   15:00:00    0.00
2   15:15:00    0.00
3   15:30:00    0.00
4   15:45:00    0.00
5   16:00:00    0.00
6   16:15:00    0.00
7   16:30:00    0.00
8   16:45:00    0.00
9   17:00:00    6.00
10  17:15:00    1.00
11  17:30:00    1.00
12  17:45:00    0.00
13  18:00:00    0.00
14  18:15:00    0.00
15  18:30:00    0.00
16  18:45:00    0.00
17  19:00:00    0.00
18  19:15:00    0.00
19  19:30:00    0.00
20  19:45:00    0.00
21  20:00:00    0.00
23  20:15:00    0.00
24  20:30:00    0.00
25  20:45:00    0.00
26  21:00:00    0.00
27  21:15:00    0.00
28  21:30:00    0.00
29  21:45:00    0.00
30  22:00:00    0.00
31  22:15:00    0.00
32  22:30:00    0.00
33  22:45:00    0.00
34  23:00:00    0.00
35  23:15:00    0.00
36  23:30:00    0.00
37  23:45:00    0.00

并且预期结果为文本

4   15:45:00    0.00    0.00
5   16:00:00    0.00    0.00
6   16:15:00    0.00    0.00
7   16:30:00    0.00    0.00
8   16:45:00    0.00    0.00
9   17:00:00    6.00    6.00
10  17:15:00    1.00    3.00
11  17:30:00    1.00    2.00
12  17:45:00    0.00    0.00
13  18:00:00    0.00    0.00
14  18:15:00    0.00    0.00
15  18:30:00    0.00    0.00
16  18:45:00    0.00    0.00
17  19:00:00    0.00    0.00
18  19:15:00    0.00    0.00
19  19:30:00    0.00    0.00
20  19:45:00    0.00    0.00
21  20:00:00    0.00    0.00
23  20:15:00    0.00    0.00
24  20:30:00    0.00    0.00
25  20:45:00    0.00    0.00
26  21:00:00    0.00    0.00
27  21:15:00    0.00    0.00
28  21:30:00    0.00    0.00
29  21:45:00    0.00    0.00
30  22:00:00    0.00    0.00
31  22:15:00    0.00    0.00
32  22:30:00    0.00    0.00
33  22:45:00    0.00    0.00
34  23:00:00    0.00    0.00
35  23:15:00    0.00    0.00
36  23:30:00    0.00    0.00
37  23:45:00    0.00    0.00

此方法使用一个变量,应该适用于所有版本的 mySQL。
根据请求,我添加了第二个版本,其中包含一个最大值参数。见第一版后。

create table settings(
id int not null primary key auto_increment,
imp int);
insert into settings (imp)
values
(0),(0),(6),(1),(1),(0);
✓

✓
set @c = 0;
select
  id,
  imp raw_value,
  case when imp + @c > 2 
    then 3 else imp + @c end smoothed_value,
  @c := case 
    when (imp + @c) > 2 
      then imp + @c - 3
    else 0 end calculation
FROM settings
✓

id | raw_value | smoothed_value | calculation
-: | --------: | -------------: | ----------:
 1 |         0 |              0 |           0
 2 |         0 |              0 |           0
 3 |         6 |              3 |           3
 4 |         1 |              3 |           1
 5 |         1 |              2 |           0
 6 |         0 |              0 |           0

db<>fiddle here

create table settings(
id int not null primary key auto_increment,
imp int);
insert into settings (imp)
values
(0),(0),(36),(1),(8),(0),(0),(0);
✓

✓
set @max = 10;
set @c = 0;
select
  id,
  imp raw_value,
  case when imp + @c > @max 
    then @max else imp + @c end smoothed_value,
  @c := case 
    when (imp + @c) > @max 
      then imp + @c - @max
    else 0 end carry_over_to_next_line
FROM settings
✓

✓

id | raw_value | smoothed_value | carry_over_to_next_line
-: | --------: | -------------: | ----------------------:
 1 |         0 |              0 |                       0
 2 |         0 |              0 |                       0
 3 |        36 |             10 |                      26
 4 |         1 |             10 |                      17
 5 |         8 |             10 |                      15
 6 |         0 |             10 |                       5
 7 |         0 |              5 |                       0
 8 |         0 |              0 |                       0

db<>fiddle here