从表中:
| name | range |
------------------------
| 'Range1' | '456-458' |
| 'Range2' | '11-13' |
只是想得到这个结果:
| name | range | value |
--------------------------------
| 'Range1' | '456-458' | 456 |
| 'Range1' | '456-458' | 457 |
| 'Range1' | '456-458' | 458 |
| 'Range2' | '11-13' | 11 |
| 'Range2' | '11-13' | 12 |
| 'Range2' | '11-13' | 13 |
如果源表只有一个范围,则查询可以正常工作:
WITH data AS (
SELECT 'Range1' name, '456-458' range FROM dual
)
SELECT ROWNUM, name, range, LEVEL value
FROM data, dual
WHERE LEVEL >= to_number(SUBSTR(range, 1, INSTR(range,'-')-1))
CONNECT BY LEVEL <= to_number(SUBSTR(range, INSTR(range,'-')+1));
但返回数万行,如果需要两个范围:
WITH data AS (
SELECT 'Range1' name, '456-458' range FROM dual
UNION
SELECT 'Range2' name, '11-13' range FROM dual
)
SELECT ROWNUM, name, range, LEVEL value FROM data, dual
WHERE LEVEL >= to_number(SUBSTR(range, 1, INSTR(range,'-')-1))
CONNECT BY LEVEL <= to_number(SUBSTR(range, INSTR(range,'-')+1));
是否可以改进此查询以获得所需,或者我的方法最初是错误的?
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production