SQL, how to sort columns by percentage?

Hey. I am trying to sort in a percent column in sql, but I can’t bring β€œ100%” from bottom to top result, who has a solution?

I used concat (x / y * 100, '%') as a percentage function that gave me different percentages, for example. 50%, 60%, 99%, 100%.

However, the percent order desc then gives me the following order: 99% 60% 50% 100%

thanks

+4
source share
2 answers

Add this to your order proposal.

SELECT ... FROM ... WHERE ... ORDER BY (x/y*100) ASC 

The reason that the ORDER BY percentage DESC does not work is because the percentage column is a row, not a numeric one.

+5
source

You must order x/y , this will give you the correct sort order. Use an expression that is multiplied by 100 and adds a percent sign to the end to format the output, not to sort it.

+1
source

Source: https://habr.com/ru/post/1438527/