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

Oracle SQL crea diversi livelli di dati da un'unica tabella

Puoi utilizzare un CTE ricorsivo:

WITH CTE(ITEM_VALUE, ITEM_ABOVE, DESCRIPTION, ITEM_GROUP) AS 
(
   SELECT ITEM_VALUE, ITEM_ABOVE, DESCRIPTION, DESCRIPTION AS ITEM_GROUP
   FROM mytable
   WHERE ITEM_ABOVE = 'CAR'

   UNION ALL

   SELECT t1.ITEM_VALUE, t1.ITEM_ABOVE, t1.DESCRIPTION, t2.ITEM_GROUP
   FROM mytable t1
   JOIN CTE t2 ON t1.ITEM_ABOVE = t2.ITEM_VALUE
)
SELECT ITEM_VALUE, ITEM_ABOVE, DESCRIPTION, ITEM_GROUP
FROM CTE