У меня есть таблица STRUCTURE
, которая содержит следующие данные при запросе с:Oracle SQL Структура 3 уровня из одной таблицы
SELECT *
FROM STRUCTURE
WHERE structure_id = 'LOCA'
structure_id name_value description level_no item_above structure_item_type_db Level_id level_description
LOCA 3000 North 4 300 1 4 Area
LOCA 827 North A 4 3000 2 4 Area
LOCA 828 North 1 4 3000 2 4 Area
LOCA 829 North 2 4 3000 2 4 Area
LOCA 830 North 3 4 3000 2 4 Area
LOCA 300 North & South 3 LOCA 1 3 Area Group
LOCA 3010 North Other 4 300 1 4 Area
LOCA 879 North Other 4 3010 2 4 Area
У меня есть этот код, чтобы получить 2 Уровни (Описание и Area_Group), но я не могу получить 3-го уровня.
WITH CTE(NAME_VALUE, ITEM_ABOVE, DESCRIPTION, Area_Group) AS
(
SELECT NAME_VALUE, ITEM_ABOVE, DESCRIPTION, DESCRIPTION AS Area_Group
FROM STRUCTURE
WHERE ITEM_ABOVE = 'LOCA' AND Structure_id = 'LOCA'
UNION ALL
SELECT t1.NAME_VALUE, t1.ITEM_ABOVE, t1.DESCRIPTION, t2.Area_Group
FROM STRUCTURE t1
JOIN CTE t2 ON t1.ITEM_ABOVE = t2.NAME_VALUE
WHERE Structure_id = 'LOCA'
)
SELECT DESCRIPTION, Area_Group
FROM CTE
дает мне:
description Area_Group
North North & South
North A North & South
North 1 North & South
North 2 North & South
North 3 North & South
North & South North & South
North Other North & South
North Other North & South
Я хочу, чтобы выводить данные (WHERE structure_id = 'LOCA'
необходимы, поскольку есть другие структуры, которые не используются в данном сценарии), чтобы выглядеть следующим образом:
description Area_Group Area
North North & South North
North A North & South North
North 1 North & South North
North 2 North & South North
North 3 North & South North
North & South North & South North
North Other North & South North Other
North Other North & South North Other
Так что, если представить структуру как структуру дерева ...
Area_Group - North & South
└ Area - North
└ description - North A
description - North 1
description - North 2
description - North 3
└ Area - North Other
└ description - North Other
Где 'Площадь Group' и' Area' определены? Ваш вопрос не очень ясен. –
В поле 'level_description', позвольте мне добавить еще некоторый контекст для ожидаемого вывода. – Matt
@GordonLinoff Я добавил древовидную структуру для ясности. – Matt