Generating Rows Based on Column Value

前端 未结 3 442
情书的邮戳
情书的邮戳 2020-12-19 17:15

One of my tables in my database contains rows with requisition numbers and other related info. I am trying to create a second table (populated with an INSERT INTO

3条回答
  •  有刺的猬
    2020-12-19 17:34

    A simple method uses recursive CTEs:

    with cte as (
          select requsition, quantity, 1 as series
          from t
          union all
          select requsition, quantity, 1 + series
          from t
          where lev < quantity
        )
    select requsition, quantity, series
    from cte;
    

    With default setting, this works up to a quantity of 100. For larger quantities, you can add option (maxrecursion 0) to the query.

提交回复
热议问题