I have a dataset like so:
print_id
--------
2b
1
2
4b
4a
3
6
2a
5
The print ID can be in the format of: /([0-9]+)([a-e]{1})?/
What I want to is order them first by number, and by letter if there is any. If there is no letter, then it's the first in the order for that number. So the result should be like so:
print_id
--------
1
2
2a
2b
3
4a
4b
5
6
I tried ORDER BY (print_id + 0) it sorts the numbers correctly but it just doesn't quite do the trick. Any suggestions?
You can have multiple expressions in the ORDER BY
clause:
ORDER BY print_id + 0, print_id
This will first try to order them numerically (discarding the letter suffix), and if they are numerically equivalent, it will order them by their string values (including the letter suffix). For the rule that you described, the only time this will break is if you have leading zeroes; for example, this ORDER BY
clause will sort '01b'
above '1a'
. Can that happen in your data?