Home > front end >  Sort SQL by value
Sort SQL by value

Time:11-10

I have data like this:

Customer ID Name Type Last Submit
1 Patricio C January 2022
2 Dale A June 2022
3 Yvonne C July 2022
4 Pawe C JUne 2022
5 Sergio B August 2022
6 Roland C August 2022
7 Georg D November 2022
8 Catherine D October 2022
9 Pascale E October 2022
10 Irene A November 2022

How to sort type A out of the queue first like A,B,C,D,E,F, then the last submit is at the top.

The example output:

Customer ID Name Type Last Submit
10 Irene A November 202[![enter image description here][1]][1]2
1 Dale A June 2022
5 Sergio B August 2022
6 Roland C August 2022
3 Yvonne C July 2022
4 Pawe C June 2022
1 Patricio C January 2022
7 Georg D November 2022
8 Catherine D October 2022
9 Pascale E October 2022

CodePudding user response:

So basically you want to sort by 2 different columns, this is detailed in this other answer: SQL multiple column ordering

In your example you would do

ORDER BY type, last_submit

CodePudding user response:

Hi you can use simple order by in postgresql like this

SELECT
     *
FROM
    table (your table name)
ORDER BY
    type ASC, last_submit DESC; 

CodePudding user response:

In this case, you need to sort your query using the two columns in order.

Add this part to the end of your query.

ORDER BY type, last_submit DESC; 

Check out this question "SQL multiple column ordering"

  • Related