문제)
다음과 같은 id값이 있는데 order by id로 하면 원하는 sorting이 되지 않습니다.
order by id 원하는 sorting
1.1.1 1.1.1
1.1.11 1.1.2
1.1.2 1.1.11
1.10.1 1.2.1
1.10.2 1.2.2
1.11.1 1.10.1
1.11.2 1.10.2
1.2.1 1.11.1
1.2.2 1.11.2
10g 이기때문에 정규식이 먹습니다.
어떻게 하면 원하는 sorting을 할 수 있는지요?
답변)
with test as (
select '1.1.1' id from dual union all
select '1.1.11' from dual union all
select '1.1.2' from dual union all
select '1.10.1' from dual union all
select '1.10.2' from dual union all
select '1.11.1' from dual union all
select '1.11.2' from dual union all
select '1.2.1' from dual union all
select '1.2.2' from dual)
SELECT ID
, regexp_replace (ID, '([0-9]+)', '0000\1.') b
, regexp_replace (regexp_replace (ID, '([0-9]+)', '000\1.')
, '([0-9]{4}\.)|.', '\1') a
FROM TEST
ORDER BY regexp_replace (regexp_replace (ID, '([0-9]+)', '000\1.')
, '([0-9]{4}\.)|.', '\1')