Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Oracle SQL erstellt verschiedene Datenebenen aus einer einzigen Tabelle

Sie können einen rekursiven CTE verwenden:

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