Reputation: 1
I have data on table test like this
customer_no | name | chance
---------------------------
00000000001 | AAAA | 3
00000000002 | BBBB | 2
00000000003 | CCCC | 1
Now,i want to select from table test with multiple output that calculate by value of field chance
output like this
customer_no | name
------------------
00000000001 | AAAA
00000000001 | AAAA
00000000001 | AAAA
00000000002 | BBBB
00000000002 | BBBB
00000000003 | CCCC
how to select command in pgsql database?
Upvotes: 0
Views: 87
Reputation: 36087
Try this:
SELECT customer_no, name FROM (
SELECT test.*,
generate_series(1,chance) i
FROM test
) test;
Here is a demo.
Upvotes: 4
Reputation: 117337
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
Upvotes: 1