I have a table that looks like this
id | name | count |
---|---|---|
1 | Nishu | 4 |
2 | Shivam | 2 |
3 | Himanshu | 1 |
I want to get the Output like this:-
id | name | count |
---|---|---|
1 | Nishu | 4 |
1 | Nishu | 4 |
1 | Nishu | 4 |
1 | Nishu | 4 |
2 | Shivam | 2 |
2 | Shivam | 2 |
3 | Himanshu | 1 |
3 | Himanshu | 1 |
. . . . . . . . . . . . . . . .
CodePudding user response:
You can use a cross join against generate_series()
select t.*
from the_table t
cross join generate_series(1, t.count) as g
order by t.id;
CodePudding user response:
Using RECURSIVE CTE you can do:
WITH RECURSIVE cte as (
SELECT 1 as x,m.* FROM mytable m
union all
SELECT x 1,m.*
FROM cte,mytable m
WHERE x<m.count)
SELECT DISTINCT *
FROM cte
ORDER BY count DESC;
see: DBFIDDLE
more info: