0

I have a table with a column named category. the data in it is 1, 2, 3 so every row has a category.

I need sort data by category like this

1,2,3,1,2,3,1,2,3,....

then if some category finished sorting continues like this

1,2,3,1,2,3,1,3,1,3,1,1,1,1,....

I am using PostgreSQL.

thanks for your answers

3 Answers 3

2

You can use window functions:

order by row_number() over (partition by category order by category)

You can specify whatever you want for the order by. For instance, if you want a random ordering:

order by row_number() over (partition by category order by random())
Sign up to request clarification or add additional context in comments.

Comments

1

You can use like this:

SELECT
   c,
   RANK () OVER ( 
      ORDER BY c 
   ) rank_number 
FROM
   ranks;

Results:

For more information: http://www.postgresqltutorial.com/postgresql-rank-function/

Comments

0
select ID from (
select *,row_number() over ( partition by id order by id ) rn from Yourtable  
) I
order by  rn,id

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.