-1

I have data in following form, I want to sort the balance column based on the same value in reference column.

reference | balance        
----------------------
ADE       |    100
ADE       |     95
ADE       |     90
AVR       |     83
AQE       |     95
AQE       |     71

Result would look like as below

reference | balance        
----------------------
ADE       |     90
ADE       |     95
ADE       |    100
AVR       |     83
AQE       |     71
AQE       |     95
Shadow
  • 33,525
  • 10
  • 51
  • 64
unknown
  • 193
  • 3
  • 6
  • 15
  • You may use a case expression within your order by clause to specify the order of references, or you could have a table/ CTE with the default references order you want and then do a join with your table to get that default order. Check this demo https://dbfiddle.uk/Ap99t31I – ahmed Jun 12 '23 at 02:23

1 Answers1

0

You should first sort the table with reference column in ascending order and then with the balance column in ascending order. Please see the SQL query below to achieve the same

SELECT reference, balance
FROM your_table
ORDER BY reference ASC, balance ASC;
Rinsad Ahmed
  • 1,877
  • 1
  • 10
  • 28
  • but this will change the sequence of the records. i dont want to make it ascending order for reference column (it should stay the same) – unknown Jun 12 '23 at 02:02
  • @unknown, this comment had to be clarified in your question, your question was closed as duplicate because you didn't clarify that in it. – ahmed Jun 12 '23 at 02:26