【问题标题】:Redshift generate rows as many as value in another columnRedshift 生成的行数与另一列中的值一样多
【发布时间】:2022-02-10 02:59:00
【问题描述】:

df

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

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

预期_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
  • unique_id 可以是任何随机字符,只要我以后可以在其上使用 count(distinct) 即可。 我读到generate_series(1,10000) i 在更高版本的 Postgres 中可用,但在 Redshift 中不可用

【问题讨论】:

  • generate_series() 在 Amazon Redshift 中可用,但它仅在领导节点上运行,这意味着它不能用于访问表数据的查询中。它可用于生成表,但不能与现有表连接。您可以创建一个数字表并加入它。
  • 任何提示如何创建与列中的值一样多的数字表?

标签: amazon-web-services amazon-redshift


【解决方案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;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多