使用序列列表

时间:2017-09-08 16:22:10

标签: postgresql

我需要使用postgres创建一个表,该表将行乘以2列中数字的差值,并提供相应的序列。这很难解释,我会留下一张图片来拯救我们千言万语:

enter image description here

我在SQL中找到了对这个问题的部分答案,但它只乘以一列,而我在Posgresql中使用它时遇到了麻烦: How to multiply a single row with a number from column in sql

1 个答案:

答案 0 :(得分:0)

您可以使用generate_series功能:wiki

create table table_a(
  a       integer primary key,
  start_a integer,
  end_a   integer
);

insert into table_a values
  (1, 1, 3), 
  (2, 2, 5);

create table table_b as 
  select a, start_a, end_a, g as start_b, g+1 as end_b
    from table_a, lateral generate_series(start_a, end_a-1) g;

select * from table_b;

您可以在此处试用:https://www.postgresql.org/docs/current/static/functions-srf.html