查询以升序间隔格式获取数据

时间:2013-09-23 10:50:37

标签: sql oracle

表中的

列:interval varchar(12)

当用户在此列中输入数据时,如下所示:

1
10-20
3-5
5-3
25-34
20-25
2
4-8

并且输出应该是这样的:

1
2
3-5
4-8
9-10
10-20
20-25
25-34

1 个答案:

答案 0 :(得分:2)

SELECT  col
FROM
(
        SELECT '1' col  FROM DUAL UNION ALL
        SELECT '10-20'  FROM DUAL UNION ALL
        SELECT '3-5'    FROM DUAL UNION ALL
        SELECT '5-3'    FROM DUAL UNION ALL
        SELECT '25-34'  FROM DUAL UNION ALL
        SELECT '20-25'  FROM DUAL UNION ALL
        SELECT '2'      FROM DUAL UNION ALL
        SELECT '4-8'    FROM DUAL
)
ORDER   BY
        TO_NUMBER(regexp_substr(col, '\w+', 1, 1))
,       TO_NUMBER(regexp_substr(col, '\w+', 1, 2))


1
2
3-5
4-8
5-3
10-20
20-25
25-34