Oracle SQL 来自单个 table 的 3 级结构
Oracle SQL 3 Level Structure from a single table
我有一个 table STRUCTURE
,其中在查询时包含以下数据:
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
这应该有所帮助,两个自连接和 CASE WHEN
语句:
WITH
sa AS (SELECT *
FROM structure
WHERE structure_id = 'LOCA'),
sb AS (SELECT s1.description, s1.name_value, s1.level_id,
CASE WHEN s1.structure_item_type_db = 1
THEN s1.description
ELSE s2.description
END area,
CASE WHEN s1.structure_item_type_db = 1
THEN s1.item_above
ELSE s2.item_above
END item_above
FROM sa s1
LEFT JOIN sa s2 ON s2.name_value = s1.item_above)
SELECT s1.description,
CASE WHEN s1.level_id = 3
THEN s1.description
ELSE s2.description
END area_group, s1.area
FROM sb s1
LEFT JOIN sb s2 ON s2.name_value = s1.item_above
在您的数据中,第 6 行 item_above
的值为 3500,并且没有包含此类 name_value
的行,因此 left join
在此不附加任何内容。添加的列 ID
仅用于展示目的。
输出:
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 & South
North Other North & South North Other
North Other North & South North Other
我有一个 table STRUCTURE
,其中在查询时包含以下数据:
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
这应该有所帮助,两个自连接和 CASE WHEN
语句:
WITH
sa AS (SELECT *
FROM structure
WHERE structure_id = 'LOCA'),
sb AS (SELECT s1.description, s1.name_value, s1.level_id,
CASE WHEN s1.structure_item_type_db = 1
THEN s1.description
ELSE s2.description
END area,
CASE WHEN s1.structure_item_type_db = 1
THEN s1.item_above
ELSE s2.item_above
END item_above
FROM sa s1
LEFT JOIN sa s2 ON s2.name_value = s1.item_above)
SELECT s1.description,
CASE WHEN s1.level_id = 3
THEN s1.description
ELSE s2.description
END area_group, s1.area
FROM sb s1
LEFT JOIN sb s2 ON s2.name_value = s1.item_above
在您的数据中,第 6 行 item_above
的值为 3500,并且没有包含此类 name_value
的行,因此 left join
在此不附加任何内容。添加的列 ID
仅用于展示目的。
输出:
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 & South
North Other North & South North Other
North Other North & South North Other