Oracle
 sql >> Database >  >> RDS >> Oracle

come duplicare i miei risultati sql?

Puoi utilizzare una query gerarchica correlata:

SELECT t.id, r.COLUMN_VALUE, t.cnt
FROM   table_name t
       CROSS JOIN
       TABLE(
         CAST(
           MULTISET(
             SELECT LEVEL
             FROM   DUAL
             CONNECT BY LEVEL <= t.cnt
           )
           AS SYS.ODCINUMBERLIST
         )
       ) r;

o una clausola ricorsiva di factoring sottoquery:

WITH row_generator ( id, lvl, cnt ) AS (
  SELECT id, 1, cnt
  FROM   table_name
UNION ALL
  SELECT id, lvl + 1, cnt
  FROM   row_generator
  WHERE  lvl < cnt
)
SELECT *
FROM   row_generator
ORDER BY id, lvl

Quindi per i dati del test:

CREATE TABLE table_name ( ID, cnt ) AS
SELECT 'A',  12 FROM DUAL UNION ALL
SELECT 'B', 138 FROM DUAL

Entrambe queste soluzioni produrrebbero:

ID | COLUMN_VALUE | CNT
:- | -----------: | --:
A  |            1 |  12
A  |            2 |  12
A  |            3 |  12
          ...
A  |           10 |  12
A  |           11 |  12
A  |           12 |  12
B  |            1 | 138
B  |            2 | 138
B  |            3 | 138
B  |            4 | 138
          ...
B  |          136 | 138
B  |          137 | 138
B  |          138 | 138

db<>gioca qui