SQL 根据字段值重复行,同时增加另一个字段

SQL repeating rows based on a field value , also increment another field

我被困住了,需要一些帮助。我正在使用 SQL 2017.

如何根据字段 WC_Count 重复以下数据,同时增加操作数?

原样

Job StockCode Warehouse Op Op_Count WC_Count
000000000219587 CODE1 1W 1 6 4
000000000219587 CODE2 1W 2 6 4
000000000219587 CODE3 1W 3 6 4
000000000219587 CODE4 1W 4 6 4
000000000219587 CODE5 1W 5 6 4
000000000219587 CODE6 1W 6 6 4

成为

Job StockCode Warehouse Op Op_Count WC_Count
000000000219587 CODE1 1W 1 6 4
000000000219587 CODE2 1W 2 6 4
000000000219587 CODE3 1W 3 6 4
000000000219587 CODE4 1W 4 6 4
000000000219587 CODE5 1W 5 6 4
000000000219587 CODE6 1W 6 6 4
000000000219587 CODE1 1W 7 6 4
000000000219587 CODE2 1W 8 6 4
000000000219587 CODE3 1W 9 6 4
000000000219587 CODE4 1W 10 6 4
000000000219587 CODE5 1W 11 6 4
000000000219587 CODE6 1W 12 6 4
000000000219587 CODE1 1W 13 6 4
000000000219587 CODE2 1W 14 6 4
000000000219587 CODE3 1W 15 6 4
000000000219587 CODE4 1W 16 6 4
000000000219587 CODE5 1W 17 6 4
000000000219587 CODE6 1W 18 6 4
000000000219587 CODE1 1W 19 6 4
000000000219587 CODE2 1W 20 6 4
000000000219587 CODE3 1W 21 6 4
000000000219587 CODE4 1W 22 6 4
000000000219587 CODE5 1W 23 6 4
000000000219587 CODE6 1W 24 6 4

您可以使用递归子查询。您没有描述递增 op 的确切逻辑,所以我将只使用 row_number():

with cte as (
      select Job, StockCode, Warehouse, Op, Op_Count, WC_Count, 1 as n
      from t
      union all
      select Job, StockCode, Warehouse, Op, Op_Count, WC_Count, n + 1
      from cte
      where n < wc_count
     )
select Job, StockCode, Warehouse,
       row_number() over (order by n, op) as op,
       op_count, wc_count
from cte;

Here 是一个 db<>fiddle.