从 table A 有条件插入到 table B

insert into table B from table A with condition

我有一个 table A 包含以下几列

Name age phoneNumber
A     26 12345
B     34 87654
C     5  98765

我的输出 table 也包含没有数据的相同列名

我需要像下面提到的那样生成输出 table

Name age phoneNumber
A     10 12345
A     10 12345
A     6 12345
B     10 87654
B     10 87654
B     10 87654
B     4 87654
C     5  98765

也就是说,每次我都会用 10 减去 age,直到 age < 10 如果在输入 table 中,如果年龄小于 10,则按原样移动该行以输出 table

任何人都可以指导我如何进行此操作以获得所需的输出

这看起来是使用递归 cte 的好地方:

with cte as (
    select name, age, age % 10 new_age, phoneNumber, 1 i from mytable
    union all
    select name, age, 10, phoneNumber, i + 1
    from cte
    where age > i * 10
)
select name, new_age age, phoneNumber from cte order by name, age desc

Demo on DB Fiddle:

name | age | phoneNumber
:--- | --: | ----------:
A    |  10 |       12345
A    |  10 |       12345
A    |   6 |       12345
B    |  10 |       87654
B    |  10 |       87654
B    |  10 |       87654
B    |   4 |       87654
C    |   5 |       98765