In Oracle DB there is a table with some data:
LAG | HNR | STREET |
---|---|---|
00020 | 44 | Aachener Straße |
00020 | 44/2 | Aachener Straße |
00020 | 44/1 | Aachener Straße |
00020 | 46 | Aachener Straße |
00020 | 46/6 | Aachener Straße |
00020 | 46/5 | Aachener Straße |
00020 | 46/3 | Aachener Straße |
00020 | 46/1 | Aachener Straße |
00020 | 46/2 | Aachener Straße |
00020 | 47 | Aachener Straße |
00020 | 48 | Aachener Straße |
00020 | 48/1 | Aachener Straße |
00020 | 48/2 | Aachener Straße |
00020 | 48A | Aachener Straße |
Please find a minimal reproducible example here: http://www.sqlfiddle.com/#!4/19d625/1
Now I am trying to sort this data via the "HNR"
in the order of 25 > 25A > 25B > 25/3
. So, the highest priority has a simple number, then goes the same number with letters (also sorted), and lastly goes the same number with a sub-number after /
(sorted).
Using this code
SELECT F.LAG,
F.HNR,
F.STREET
FROM DB.F F
ORDER BY F.LAG,
F.STREET,
TO_NUMBER(regexp_substr(F.HNR, '[[:digit:]]{1,4}')),
TO_CHAR(regexp_substr(F.HNR, '[[:alpha:]]{1}')),
TO_NUMBER(regexp_substr(F.HNR, '[^/]+', 1, 2));
I am getting this:
LAG | HNR | STREET |
---|---|---|
00020 | 44/1 | Aachener Straße |
00020 | 44/2 | Aachener Straße |
00020 | 44 | Aachener Straße |
00020 | 46/1 | Aachener Straße |
00020 | 46/2 | Aachener Straße |
00020 | 46/3 | Aachener Straße |
00020 | 46/5 | Aachener Straße |
00020 | 46/6 | Aachener Straße |
00020 | 46 | Aachener Straße |
00020 | 47 | Aachener Straße |
00020 | 48/1 | Aachener Straße |
00020 | 48/2 | Aachener Straße |
00020 | 48A | Aachener Straße |
00020 | 48 | Aachener Straße |
Instead of this:
LAG | HNR | STREET |
---|---|---|
00020 | 44 | Aachener Straße |
00020 | 44/1 | Aachener Straße |
00020 | 44/2 | Aachener Straße |
00020 | 46 | Aachener Straße |
00020 | 46/1 | Aachener Straße |
00020 | 46/2 | Aachener Straße |
00020 | 46/3 | Aachener Straße |
00020 | 46/5 | Aachener Straße |
00020 | 46/6 | Aachener Straße |
00020 | 47 | Aachener Straße |
00020 | 48 | Aachener Straße |
00020 | 48A | Aachener Straße |
00020 | 48/1 | Aachener Straße |
00020 | 48/2 | Aachener Straße |
What am I doing wrong?
I have seen SQL: ORDER BY using a substring within a specific column… possible? and Order by alphabet and then by numbers, but I did not find there anything relevant.