recursive-query

Tree structure in sql in Oracle.How to show tree,child nodes and parent nodes in SQL Oracle

元气小坏坏 提交于 2019-11-30 16:45:34
I would like to show a tree structure in SQL with child nodes and parent nodes. I have a table like: Employee ------------- ID (int) FirstName (varchar) LastName (varchar) ParentID (int) Job (varchar) which represents an employee. ParentID represent the manager of the employee has. I would like to have this table only with this structure. I would like to show the whole tree structure. I would like to show only the children nodes I would like to show only the parent nodes SampleDataImage Query - The whole tree structure : SELECT * FROM Employee START WITH ParentID IS NULL CONNECT BY PRIOR ID =

How to get all children of a parent and then their children using recursion in query

我的未来我决定 提交于 2019-11-30 08:39:01
I have structure like this: <Unit> <SubUnit1> <SubSubUnit1/> <SubSubUnit2/> ... <SubSubUnitN/> </SubUnit1/> <SubUnit2> <SubSubUnit1/> <SubSubUnit2/> ... <SubSubUnitN/> </SubUnit2/> ... <SubUnitN> <SubSubUnit1/> <SubSubUnit2/> ... <SubSubUnitN/> </SubUnitN/> </Unit> This structure has 3 levels: main Unit, SubUnits and SubSubUnits. I want to select all children by UnitId. If I search by Unit, I have to get all tree. If I search by SubUnit1, I have to get SubUnit1 and all children of SubUnit1. If I search SubSubUnit2, I have to get itself. Here is my try: with a(id, parentid, name) as ( select id

MySQL recursive tree search

最后都变了- 提交于 2019-11-30 07:41:08
问题 I have a database with a tree of names that can go down a total of 9 levels deep and I need to be able to search down a signal branch of the tree from any point on the branch. Database: +----------------------+ | id | name | parent | +----------------------+ | 1 | tom | 0 | | 2 | bob | 0 | | 3 | fred | 1 | | 4 | tim | 2 | | 5 | leo | 4 | | 6 | sam | 4 | | 7 | joe | 6 | | 8 | jay | 3 | | 9 | jim | 5 | +----------------------+ Tree: tom fred jay bob tim sam joe leo jim For example: If I search

Building a Table Dependency Graph With A Recursive Query

浪尽此生 提交于 2019-11-30 07:37:29
I am trying to build a dependency graph of tables based on the foreign keys between them. This graph needs to start with an arbitrary table name as its root. I could, given a table name look up the tables that reference it using the all_constraints view, then look up the tables that reference them, and so on, but this would be horrible inefficient. I wrote a recursive query that does this for all tables, but when I add: START WITH Table_Name=:tablename It doesn't return the entire tree. select parent, child, level from ( select parent_table.table_name parent, child_table.table_name child from

How sql with-recursive statement interpreted?

有些话、适合烂在心里 提交于 2019-11-30 05:36:38
问题 I would like to ask get some help about understanding how "with recursive" works. More precisely WHY the anchor query (the non-recursive term) isn't replicated into the sub call of the CTE. I tried my best to understand alone but I'm not sure. First of all let's take the example of PostgreSQL which is the simplest one I found (make the sum of 1 to 100) : WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t; My code walkthrough ( I used links

SQL Server CTE hierarchy issue

 ̄綄美尐妖づ 提交于 2019-11-30 05:09:05
问题 i have an application serve multilevel of permissions and roles i have this HIERARCHY : Country ....Region ........City ............Association ................Center ....................School ........................Class this HIERARCHY i name it "EntityLevels" | ID | Name | ParentID | |----|-------------|----------| | 1 | Country | Null | | 2 | Region | 1 | | 3 | City | 2 | | 4 | Association | 3 | | 5 | Center | 4 | | 6 | School | 5 | | 7 | Class | 6 | i have also a Groups Table which

Calculating the Weighted Average Cost of products stock

家住魔仙堡 提交于 2019-11-30 04:05:02
I have to calculate my products stock cost, so for every product after each buy, i have to recalculate the Weighted Average Cost . I got a view thats bring me the current product's stock after each in/out: document_type document_date product_id qty_out qty_in price row_num stock_balance SI 01/01/2014 52 0 600 1037.28 1 600 SI 01/01/2014 53 0 300 1357.38 2 300 LC 03/02/2014 53 100 0 1354.16 3 200 LC 03/02/2014 53 150 0 1355.25 4 50 LC 03/02/2014 52 100 0 1035.26 5 500 LC 03/02/2014 52 200 0 1035.04 6 300 LF 03/02/2014 53 0 1040 1356.44 7 1090 LF 03/02/2014 52 0 1560 1045 8 1860 LC 04/02/2014 52

Recursive query challenge - simple parent/child example

强颜欢笑 提交于 2019-11-30 03:54:29
Note: with help from RhodiumToad on #postgresql, I've arrived at a solution, which I posted as answer. If anyone can improve on this, please chime in! I have not been able to adapt a previous recursive query solution to the following directed acyclic graph that includes multiple "root" (ancestor-less) nodes. I'm trying to write a query whose output is what is commonly known as a closure table: a many-to-many table that stores every path from each node to each of its descendants and to itself: 1 2 11 8 4 5 7 \/ | | \ | / 3 | \ 6 \ | \ / 9 | 10 \/ / 12 13 \ / 14 CREATE TABLE node ( id SERIAL

SQL Multi Condition CTE Recursion

ε祈祈猫儿з 提交于 2019-11-30 03:03:15
问题 I the database i have the 2 following pieces of information for each identifier. The company that controls them, and companies where they have small bits of control. Something along the lines, 2 tables(ignoring some unique identifiers): organizations orgid | org_immediate_parent_orgid 1 | 2 2 | 2 3 | 1 5 | 4 The relation orgid --> org_immediate_parent_orgid means company has parent. Por me its relevant only org_immediate_parent_orgid --> orgid the parent of the companies has as subsidiary org

Tree structure in sql in Oracle.How to show tree,child nodes and parent nodes in SQL Oracle

你说的曾经没有我的故事 提交于 2019-11-30 00:03:15
问题 I would like to show a tree structure in SQL with child nodes and parent nodes. I have a table like: Employee ------------- ID (int) FirstName (varchar) LastName (varchar) ParentID (int) Job (varchar) which represents an employee. ParentID represent the manager of the employee has. I would like to have this table only with this structure. I would like to show the whole tree structure. I would like to show only the children nodes I would like to show only the parent nodes SampleDataImage 回答1: