0

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

. . . . . . . . . . . . . . . .

2 Answers 2

2

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;

Online example

Sign up to request clarification or add additional context in comments.

Comments

1

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.