基于两列之间的间隙重复记录

dupliacte records based on gaps bettwen two columns

我有这个数据:

   rn   sex value   from    to
    1   w   0.019500000000000000    1   25
    2   w   0.018000000000000002    26  26
    3   w   0.019500000000000000    27  28
    4   w   0.021000000000000001    29  29
    5   w   0.022500000000000002    30  30
    6   w   0.024000000000000000    31  31
    7   w   0.025500000000000001    32  32
    8   w   0.026999999999999999    33  33
    9   w   0.028500000000000001    34  34
    10  w   0.029999999999999998    35  35
    11  w   0.031500000000000000    36  36
    12  w   0.034500000000000002    37  37
    13  w   0.036000000000000004    38  38

我想根据从到之间的差距复制记录 所以上面的数据应该是这样的: 25

1   w   0.019500000000000000    1
1   w   0.019500000000000000    2
1   w   0.019500000000000000    3
1   w   0.019500000000000000    4
1   w   0.019500000000000000    5
1   w   0.019500000000000000    6
1   w   0.019500000000000000    7
1   w   0.019500000000000000    8
1   w   0.019500000000000000    9
1   w   0.019500000000000000    10
1   w   0.019500000000000000    11
1   w   0.019500000000000000    12
1   w   0.019500000000000000    13
1   w   0.019500000000000000    14
1   w   0.019500000000000000    15
1   w   0.019500000000000000    16
1   w   0.019500000000000000    17
1   w   0.019500000000000000    18
1   w   0.019500000000000000    19
1   w   0.019500000000000000    20
1   w   0.019500000000000000    21
1   w   0.019500000000000000    22
1   w   0.019500000000000000    23
1   w   0.019500000000000000    24
1   w   0.019500000000000000    25
2   w   0.018000000000000002    26
3   w   0.019500000000000000    27
3   w   0.019500000000000000    28

我正在尝试通过 connect by 子句来执行此操作,但目前运气不佳。 也许有人有不同 idea/approach?

在 SQL 服务器中你可以使用递归 cte :

with r_cte (rn, sex, value, from, to) as (
     select rn, sex, value, from, to
     from table t
     union all
     select rn, sex, value, from + 1, to
     from r_cte r
     where from < to
)
select rn, sex, value, from
from r_cte
order by rn, from
option (maxrecursion 0); -- remove this for oracle

注意:不同的数据库会有不同的语法。

您可以在 Oracle 中使用递归 CTE。语法是:

with recursive r_cte(rn, sex, value, from, to) as (
      select rn, sex, value, from, to
      from t
      union all
      select rn, sex, value, from + 1, to
      from r_cte r
      where from < to
     )
select rn, sex, value, from
from r_cte
order by rn, from;

当然,fromto 是糟糕的列名——因为它们是 SQL 关键字。如果是真名,应该转义。

更传统的方法是生成一个数字列表,然后使用这些:

with n as (
      select level - 1 as n
      from (select max(t - f + 1) as range
            from t
           ) t
      connect by level <= range
     )
select rn, sex, value, f + n
from t join
     n
     on n <= (t - f)
order by rn, f + n;

Here 是此方法的 db<>fiddle。