Cod*_*rer 5 sql sql-server algorithm stored-procedures
我有SourceTable这样的2列名称:
Col 1 | Col 2
------------------
A | 2
B | 3
C | 4
D | 2
E | 1
F | 0
Run Code Online (Sandbox Code Playgroud)
第一列有一些字母,第二列有其频率.
我们需要编写一个存储过程并获得这样的输出TargetTable.
我们不能使用任何循环或迭代.
Col 1
-----
A
A
B
B
B
C
C
C
C
D
D
E
Run Code Online (Sandbox Code Playgroud)
递归 CTE 怎么样?
with x as (
select col1, 1 as i, col2 as lim
from t
where col2 > 0
union all
select col1, i + 1, lim
from x
where i + 1 <= lim
)
select col1
from x
order by col1;
Run Code Online (Sandbox Code Playgroud)