简体   繁体   English

查看以返回给定任何子节点的根节点(在 Oracle 中)

[英]View to return the root node given any children (in Oracle)

We manufacture equipment, and we give every unit we produce a serial number (s/n).我们制造设备,并为我们生产的每台设备提供一个序列号 (s/n)。 We have a parent/child table which we use to establish the relationship between the top-level assemblies and its children.我们有一个父/子表,我们使用它来建立顶级程序集与其子级之间的关系。 I am providing a sample layout of such table below.我在下面提供了此类表格的示例布局。

CREATE TABLE PAR_CHI
(PARENT_SN VARCHAR2(50 BYTE) NOT NULL,
 CHILD_SN VARCHAR2(50 BYTE) NOT NULL,
 PRIMARY KEY (PARENT_SN, CHILD_SN));

I have some data that can be loaded in this table, found below.我有一些可以加载到下表中的数据,如下所示。

insert into PAR_CHI values (' ', '135887957');
insert into PAR_CHI values ('135887957', '135562597');
insert into PAR_CHI values ('135562597', '135424162');
insert into PAR_CHI values ('135424162', '135422839');
insert into PAR_CHI values ('135887957', '135623876');
insert into PAR_CHI values ('135623876', '135519894');
insert into PAR_CHI values ('135519894', '135517981');
insert into PAR_CHI values ('135887957', '136526805');

I have no experience with hierarchical queries, so I have done some online research about it and how it works.我没有分层查询的经验,所以我做了一些关于它以及它是如何工作的在线研究。 So far I have learned that a query like the one below can be used to find the 'tree' (or hierarchy) for a specific top-level assembly to return its full genealogy.到目前为止,我已经了解到,可以使用像下面这样的查询来查找特定顶级程序集的“树”(或层次结构),以返回其完整的系谱。

select PARENT_SN, CHILD_SN, level, connect_by_root(CHILD_SN) as root, sys_connect_by_path(CHILD_SN, '/') as path, connect_by_isleaf
from PAR_CHI
start with CHILD_SN = '135887957'
connect by PARENT_SN = prior CHILD_SN;

I have also learned that a query like the one below could be used to traverse the hierarchy in opposite direction (given a component s/n, to find its parent and all the way to the top-level assembly).我还了解到,像下面这样的查询可用于以相反的方向遍历层次结构(给定组件 s/n,以查找其父级并一直到顶级组件)。

select PARENT_SN, CHILD_SN, level, connect_by_root(CHILD_SN) as root, sys_connect_by_path(CHILD_SN, '/') as path, connect_by_isleaf
from PAR_CHI
start with CHILD_SN = '135517981'
connect by CHILD_SN = prior PARENT_SN;

There is a way to get the 'real' parent for a specific serial number if 'connect_by_isleaf' is used, as shown below.如果使用“connect_by_isleaf”,则有一种方法可以获取特定序列号的“真实”父级,如下所示。

select PARENT_SN, CHILD_SN, level, connect_by_root(CHILD_SN) as root, sys_connect_by_path(CHILD_SN, '/') as path, connect_by_isleaf
from PAR_CHI
where connect_by_isleaf = 1
start with CHILD_SN = '135517981'
connect by CHILD_SN = prior PARENT_SN;

I have a specific need to create a view or a table (preferably a view), but this object, whatever it is, cannot call another object to build a dataset (let me explain: for instance, in SSRS Reports we can call a store procedure that would build a dataset given specific search criteria, and then the report would display the records in the dataset).我有创建视图或表(最好是视图)的特定需求,但是这个对象,无论它是什么,都不能调用另一个对象来构建数据集(让我解释一下:例如,在 SSRS 报告中,我们可以调用存储程序将在给定特定搜索条件的情况下构建数据集,然后报告将显示数据集中的记录)。 This object must be ready for the user to perform a straight 'select statement' on it, and get the desired results.该对象必须准备好供用户对其执行直接的“选择语句”,并获得所需的结果。

The object should return one row every time, showing the top-level assembly s/n (root) given any s/n the user is searching for.该对象应每次返回一行,显示给定用户正在搜索的任何 s/n 的顶级程序集 s/n(根)。

Let me provide some examples of expected results given search data.让我提供一些给定搜索数据的预期结果示例。

  1. If the user enters s/n '135887957', the dataset would return one row showing the values below:如果用户输入 s/n '135887957',数据集将返回一行显示以下值:

Root S/N Searched S/N 135887957根 S/N 已搜索 S/N 135887957

This record corresponds to the top-level assembly itself, and the 'Root s/n' column is blank (not null).此记录对应于顶级程序集本身,并且“Root s/n”列为空白(非空)。

  1. If the user enters s/n '135562597', the dataset would return one row showing the values below.如果用户输入 s/n '135562597',数据集将返回一行显示以下值。

Root S/N Searched S/N 135887957 135562597根 S/N 已搜索 S/N 135887957 135562597

This record shows that the entered s/n '135562597' has root s/n '135887957'.此记录显示输入的 s/n '135562597' 具有根 s/n '135887957'。 Since this is a level 2 component, the result returns the top-level assembly.由于这是一个 2 级组件,因此结果返回顶级程序集。

  1. If the user enters s/n '135519894', the dataset would return one row showing the values below.如果用户输入 s/n '135519894',数据集将返回一行显示以下值。

Root S/N Searched S/N 135887957 135519894根 S/N 已搜索 S/N 135887957 135519894

This record shows that the entered s/n '135519894' has root s/n '135887957' (strictly speaking, the 'parent' of this level 3 component is '135623876', but we need to get the top-level assembly, not the 'real' parent).这条记录显示输入的s/n '135519894'有root s/n '135887957'(严格来说,这个3级组件的'parent'是'135623876',但是我们需要得到顶层组件,而不是“真正的”父母)。

  1. If the user enters s/n '135422839', the dataset should return one row showing the values below.如果用户输入 s/n '135422839',则数据集应返回显示以下值的一行。

Root S/N Searched S/N 135887957 135422839根 S/N 已搜索 S/N 135887957 135422839

This record shows that the entered s/n '135422839' has root s/n '135887957' (strictly speaking, the 'parent' of this level 4 component is '135424162', but we need to get the top-level assembly, not the 'real' parent).这条记录显示输入的s/n '135422839'有root s/n '135887957'(严格来说,这个4级组件的'parent'是'135424162',但是我们需要得到顶层组件,而不是“真正的”父母)。

As you can see, we do not really need any other data like the level, or the 'real' parent' or the path, but if we can add it, it would be helpful;如您所见,我们实际上并不需要任何其他数据,例如关卡、“真正的”父级或路径,但如果我们可以添加它,那将会很有帮助; just the s/n that corresponds to the top-level assembly.只是对应于顶级程序集的 s/n。 One big difference between the code that I have seen in other posts and my specific need is that we do not have a 'starting point';我在其他帖子中看到的代码与我的特定需求之间的一大区别是我们没有“起点”。 all the queries out there use a 'start with' statement, but since we need to put this in a view, we do not really have that starting record to start searching from.那里的所有查询都使用“start with”语句,但是由于我们需要将其放在视图中,因此我们实际上并没有开始搜索的起始记录。

Could anyone advice how we can build this object given the needs we have?鉴于我们的需求,有人可以建议我们如何构建这个对象吗?

Thanks in advance.提前致谢。

In the example, the root element should not have any parent, if you remove the blank and recreate the table as this then your existing solution should work.在示例中,根元素不应有任何父元素,如果您删除空白并重新创建表,那么您现有的解决方案应该可以工作。

CREATE TABLE PAR_CHI
(PARENT_SN VARCHAR2(50 BYTE) ,
 CHILD_SN VARCHAR2(50 BYTE) NOT NULL
);
create or replace view test_hierarchy as 
select PARENT_SN, CHILD_SN as ROOT, level level_in, 
connect_by_root(CHILD_SN) as Searched_val, 
sys_connect_by_path(CHILD_SN, '/') as path
from PAR_CHI
where connect_by_isleaf = 1
--start with CHILD_SN = '135422839'
connect by CHILD_SN = prior PARENT_SN
;

you can query this view and pass the serial number to search and it will give you all the required fields您可以查询此视图并将序列号传递给搜索,它将为您提供所有必填字段

样本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM