如何从一行中选择多行输出的数据(PostgreSQL)

如何从一行中选择多行输出的数据(PostgreSQL),postgresql,Postgresql,我有这样的表格测试数据 customer_no | name | chance --------------------------- 00000000001 | AAAA | 3 00000000002 | BBBB | 2 00000000003 | CCCC | 1 customer_no | name ------------------ 00000000001 | AAAA 00000000001 | AAAA 00000000001 | AAAA 00000

我有这样的表格测试数据

customer_no | name | chance
---------------------------
00000000001 | AAAA |     3
00000000002 | BBBB |     2
00000000003 | CCCC |     1
customer_no | name
------------------
00000000001 | AAAA
00000000001 | AAAA
00000000001 | AAAA
00000000002 | BBBB
00000000002 | BBBB
00000000003 | CCCC
现在,我想从具有多个输出的表test中进行选择,这些输出通过fieldchance值进行计算

像这样输出

customer_no | name | chance
---------------------------
00000000001 | AAAA |     3
00000000002 | BBBB |     2
00000000003 | CCCC |     1
customer_no | name
------------------
00000000001 | AAAA
00000000001 | AAAA
00000000001 | AAAA
00000000002 | BBBB
00000000002 | BBBB
00000000003 | CCCC
如何在pgsql数据库中选择命令

with recursive CTE_nums as (
    select max(chance) as num from test
    union all
    select num - 1 from CTE_nums
    where num > 1
)
select
   t.customer_no, t.name
from test as t
   inner join CTE_nums as n on n.num <= t.chance
order by 1, 2
试试这个:

SELECT  customer_no, name FROM (
  SELECT  test.*, 
          generate_series(1,chance) i
  FROM test
) test;
下面是一个例子。

试试这个:

SELECT  customer_no, name FROM (
  SELECT  test.*, 
          generate_series(1,chance) i
  FROM test
) test;

这是一个。

+1,完全忘记了postgres中的generate_系列,我一直在使用SQL Server:+1,完全忘记了postgres中的generate_系列,我一直在使用SQL Server: