Considering the following query:
SELECT 1 AS a, '100' AS b
UNION
SELECT 1 AS a, '50' AS b
ORDER BY a, b;
which results in:
a b
1 '100'
1 '50'
What I really want is to sort column b by number and not text.
One possible solution could be:
SELECT 1 AS a, '100' AS b, '100'::int AS c
UNION
SELECT 1 AS a, '50' AS b, '50'::int AS c
ORDER BY a, c;
Which results in the ordering of:
a b c
1 '50' 50
1 '100' 100
as desired.
This is quite satisfying but if I have 1 mio. result rows then I would also have 1 mio. values transferred in the result response which I do not need.
Is there a neater way of converting column values when ordering?
I am looking for a way of letting the SQL server convert the column values "within" the ORDER BY
clause but only returning the "original" result columns.