Oracle Database
Factoraje de subconsultas recursivas utilizando la cláusula WITH (expresiones de tabla comunes AKA)
Buscar..
Observaciones
La factorización de subconsultas recursiva está disponible en Oracle 11g R2.
Un simple generador de enteros
Consulta :
WITH generator ( value ) AS (
SELECT 1 FROM DUAL
UNION ALL
SELECT value + 1
FROM generator
WHERE value < 10
)
SELECT value
FROM generator;
Salida :
VALUE
-----
1
2
3
4
5
6
7
8
9
10
Dividir una cadena delimitada
Datos de muestra :
CREATE TABLE table_name ( value VARCHAR2(50) );
INSERT INTO table_name ( value ) VALUES ( 'A,B,C,D,E' );
Consulta :
WITH items ( list, item, lvl ) AS (
SELECT value,
REGEXP_SUBSTR( value, '[^,]+', 1, 1 ),
1
FROM table_name
UNION ALL
SELECT value,
REGEXP_SUBSTR( value, '[^,]+', 1, lvl + 1 ),
lvl + 1
FROM items
WHERE lvl < REGEXP_COUNT( value, '[^,]+' )
)
SELECT * FROM items;
Salida :
LIST ITEM LVL
--------- ---- ---
A,B,C,D,E A 1
A,B,C,D,E B 2
A,B,C,D,E C 3
A,B,C,D,E D 4
A,B,C,D,E E 5
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow