Redshift 生成的行与另一列中的值一样多

Redshift generate rows as many as value in another column

df

customer_code contract_code    product  num_products
    C0134        AB01245        toy_1        4 
    B8328        EF28421        doll_4       2 

我想根据列 num_products 中的整数值转换此 table 并为每一行生成一个唯一的 ID:

Expected_df

unique_id  customer_code contract_code     product      num_products
      A1           C0134        AB01245        toy_1        1 
      A2           C0134        AB01245        toy_1        1
      A3           C0134        AB01245        toy_1        1
      A4           C0134        AB01245        toy_1        1
      A5           B8328        EF28421        doll_4       1
      A6           B8328        EF28421        doll_4       1

您需要使用递归 CTE 来生成数字序列。然后将其与您的数据结合起来以生成额外的行。在下面的示例中,我使用 row_number() 来获取 unique_id。

这应该可以满足您的需求或至少给您一个开始:

create table df (customer_code varchar(16),
                 contract_code varchar(16),
                 product varchar(16),
                 num_products int);

insert into df values
('C0134', 'AB01245', 'toy_1', 4),
('B8328', 'EF28421', 'doll_4', 2);

with recursive nums (n) as 
( select 1 as n
  union all
  select n+1 as n
  from nums 
  where n < (select max(num_products) from df) )
select row_number() over() as unique_id, customer_code, contract_code, product, num_products 
from df d
left join nums n
on d.num_products >= n.n;

SQLfiddle 在 http://sqlfiddle.com/#!15/d829b/12