Orcale the start with ... connect by understanding usage

1. Basic grammar

. 1  SELECT ... from TableName
 2      Start with the condition. 1
 3      Connect by Condition 2
 . 4  WHERE condition 3;

E.g:

1 select * from table
2     start with org_id = ‘HBHqfWGWPy’
3     connect by prior org_id = parent_id;

 

  Briefly a tree structure is stored in a table, such as a table, there are two fields: org_id, parent_id, then the parent who is represented by each record is a tree structure can be formed using the above query syntax tree can get all the records, in which:  

        Condition 1  is the root of the defining statement, of course, defined conditions can be relaxed to obtain a plurality of the root node, the tree is more practical.
        Condition 2  is connected condition, by which a recording PRIOR expressed , such as  the CONNECT BY  PRIOR org_id = parent_id ; means that on a record org_id parent_id section is recorded , i.e., the father of this record is the record.
        Condition 3  is a filter condition for all records returned by filtration.
 
    Briefly as follows:
        when scanning the tree structure table, so each node needs to access a tree structure, a node can only be accessed once, the step of accessing the following:
        first step: starting from the root;
        Step: Access the node;
        third step: to determine whether the node is not accessible to a child node, if so, then it turned to the left-most sub-sections have not been accessed, and the second step, otherwise the fourth step;
        the fourth step : If the node is the root node, the access is completed, otherwise the fifth step;
        step five: return to the parent node of the node, and perform the third step.
        In short: the process of scanning the entire tree structure that is, in order tree traversal process.
 
1. Described tree structure
     data stored in the tree structure table, the hierarchical relationships between data, i.e., parent-child relationship, to describe the relationships between columns and the columns in the table by, as the EMP table EMPNO and MGR, indicating that the employee EMPNO No, MGR who represent the leadership of the employee's number, MGR value that is equal to the EMPNO value of the child node of the parent node. In each row of the table has a parent node represents MGR (except the root node), each parent node by node, can determine the entire tree structure.

     Use START WITH and CONNECT BY clauses can query the tree structure table in the SELECT command, the command format is as follows:
1  the SELECT ...
 2 the CONNECT BY {PRIOR is the column name 1 = column name 2 | column name 1 = PRIOR is split name} 2
 . 3  [ the START the WITH ] ;
    
  Wherein: CONNECT BY clause indicates each row of data will be retrieved by the hierarchical order, and a predetermined data table connected to the relationship in the tree structure. PRIOR operator must be placed in front of the two columns of one connection relationship. For the parent-child relationship between the nodes, PRIOR is represented in the operator side of the parent node, child node represents the other side, so as to determine the order of search tree structure is a top-down or bottom up .
 
     In the connection relationship in addition to using the column names, but also allows the use of column expression. START WITH clause is optional, which is used to find the root node identification as a tree structure, if the clause is omitted, then all the rows that satisfy the query as the root node.
     START WITH: not only can specify a root node, you can specify multiple root nodes.

  

2. About PRIOR
     position PRIOR operator is placed before and after the equal sign, and determines the search sequence when the query.
     PRIOR when the CONNECT BY clause is placed in front of the middle number, is forced from the root node to sequentially retrieved leaf node, i.e. the child nodes of the tree structure through the direction indicated by the parent node, which we call top-down manner. As:
   . 1 the CONNECT BY PRIOR is the EMPNO = MGR     

     When PIROR operator is placed behind the CONNECT BY clause, moderate number, is forced to order the root node is retrieved from the leaf node, i.e. the child node to the parent node of the tree structure through direction, which we call a bottom-up manner. E.g:    
   . 1 the CONNECT BY the EMPNO = PRIOR is MGR
     also specify a start node in this manner.
 
3. Find the definition of the starting node
     in the query tree top-down structure, not only from the root node, any node can also be defined as the starting node, in order to start looking down. The results of this look is to the node of a tree begins.
 
4. The LEVEL
     table having a tree structure, each row of data is a node in the tree, since different nodes in which the level position, the rows can each have a number of layers. The layer number is determined from the root node. No matter from which node starts, the initial root level number is always 1, the child of the root node is 2, and so on. Figure 1.2 shows the hierarchical tree structure on the.
 
5. And cutting a branch node
     when the query tree structure, can remove some of the rows of the table, you can cut a tree branch, a WHERE clause to define a single node in the tree structure, the tree to remove a single node, but it does not affect its descendant nodes (when top-down retrieval) or predecessor node (top to bottom when retrieved).
 
6. Sort displayed
     as in other queries in the query tree structure may be used ORDER BY clause, change the order of search results, rather than in the order of traversal of the tree structure.
 
7. Examples
     oracle provides start with connect by grammatical structure can achieve recursive queries.
1. A simple example:
 
 1 SQL> select *  from test;
 2 BILL_MONTH           DAY_NUMBER MSISDN
 3 -------------------- ---------- --------------------
 4 200803                        1 13800
 5 200803                        3 13800
 6 200803                        2 13800
 7 200803                        2 13801
 8 200803                        4 13804
 9 200803                        5 13804
10 200803                        7 13804
11 200803                        8 13804
12 200803                        6 13802
13 200803                        6 13801
14 200803                        7 13801
15 200803                        8 13801
16 rows selected
17  
18 SQL>
19 SQL> select * from test
20   2       start with day_number=1
21   3       connect by  prior day_number=day_number-1 and prior msisdn= msisdn
22   4      ;
23 
24 BILL_MONTH           DAY_NUMBER MSISDN
25 -------------------- ---------- --------------------
26 200803                        1 13800
27 200803                        2 13800
28 200803                        3 13800

 

上面的语句查找出了从1开始,并且day_number 逐渐+1 递增的,并且 msisdn 相同的哪些个数据.
2. start with  connect by 语法结构
    如上面说看到的例子,其语法结构为start with condition  connect by  condition (含 prior 关键字)
start with conditon 给出的seed 数据的范围, connect by  后面给出了递归查询的条件,prior 关键字表示父数据,prior 条件表示子数据需要满足父数据的什么条件。在下面的这个start with connect by 结构中,就表示查找出了从1开始,父数据的day_number等于子数据的day_number-1而且父数据的msisdn=子数据的msisdn.
start with day_number=1
connect by  prior day_number=day_number-1 and prior msisdn= msisdn

 

Guess you like

Origin www.cnblogs.com/JamesHooke/p/11031906.html