Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is it possible to set a `order` field with the result of ORDER BY

My table:

id label
1 art2
2 art1
3 art4
4 art3

Expected Output:

id label order
1 art2 2
2 art1 1
3 art4 4
4 art3 3

So, I want to set the "order" field with result of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT * FROM table ORDER BY label ASC;

Is it possible with a single request? Or should I just get result and then update order in my app ?

>Solution :

You can’t do it directly by using the generic ORDER BY clause, though you can achieve that result if the ORDER BY clause is found within a window function.

SELECT id, label, ROW_NUMBER() OVER(ORDER BY label) AS order
FROM table 

Check the demo here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading