connect-by

Mixing together Connect by, inner join and sum with Oracle

走远了吗. 提交于 2019-12-05 13:18:29
I need help with a oracle query. Here is my setup: I have 2 tables called respectively "tasks" and "timesheets". The "tasks" table is a recursive one, that way each task can have multiple subtasks. Each timesheet is associated with a task (not necessarily the "root" task) and contains the number of hours worked on it. Example: Tasks id:1 | name: Task A | parent_id: NULL id:2 | name: Task A1 | parent_id: 1 id:3 | name: Task A1.1 | parent_id: 2 id:4 | name: Task B | parent_id: NULL id:5 | name: Task B1 | parent_id: 4 Timesheets id:1 | task_id: 1 | hours: 1 id:2 | task_id: 2 | hours: 3 id:3 |

oracle connect by multiple parents

巧了我就是萌 提交于 2019-12-04 18:16:21
I am facing an issue using connect by. I have a query through which I retrieve a few columns including these three: ID ParentID ObjectID Now for the same ID and parentID , there are multiple objects associated e.g. ID ParentID ObjectID 1 0 112 1 0 113 2 0 111 2 0 112 3 1 111 4 1 112 I am trying to use connect by but I'm unable to get the result in a proper hierarchy. I need it the way it is showed below. Take an ID - parentID combo, display all rows with that ID-parentID and then all the children of this ID i.e. whose parentID = ID ID ParentID ObjectID 1 0 112 1 0 113 3 1 111 4 1 112 2 0 111 2

Oracle Self-Join on multiple possible column matches - CONNECT BY?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 13:45:49
I have a query requirement from ----. Trying to solve it with CONNECT BY , but can't seem to get the results I need. Table (simplified): create table CSS.USER_DESC ( USER_ID VARCHAR2(30) not null, NEW_USER_ID VARCHAR2(30), GLOBAL_HR_ID CHAR(8) ) -- USER_ID is the primary key -- NEW_USER_ID is a self-referencing key -- GLOBAL_HR_ID is an ID field from another system There are two sources of user data (datafeeds)... I have to watch for mistakes in either of them when updating information. Scenarios: A user is given a new User ID... The old record is set accordingly and deactivated (typically a

Calculate the percentage of the root owned by its parents

大城市里の小女人 提交于 2019-12-04 09:12:50
问题 In simplified terms, I'm trying to calculate the percentage of the root of a tree owned by its parents, further up the tree. How can I do this in SQL alone? Here's my (sample) schema. Please note that though the hierarchy itself is quite simple there is an additional holding_id , which means that a single parent can "own" different parts of their child. create table hierarchy_test ( id number -- "root" ID , parent_id number -- Parent of ID , holding_id number -- The ID can be split into

Hierarchical Query Needs to Pull Children, Parents and Siblings

本小妞迷上赌 提交于 2019-12-04 07:34:13
Can now pull the data, but am wondering if there is a better way to optimize the query for large data sets. http://sqlfiddle.com/#!4/0ef0c/5 So basically I want to be able to supply the query a given org id and have it recursively pull its parents, its children, its siblings and its aunts and uncles. And then pull any Activities that are associated with that org hierarchy. Org1 is the top level org, but it may or may not have a null parent. Basically I was doing an up and down query to pull the children and the parent, but can only seem to get the siblings by adding another query. Finally got

Does PostgreSQL have a pseudo-column like “LEVEL” in Oracle?

佐手、 提交于 2019-12-01 15:47:13
Does PostgreSQL have a pseudo-column like "LEVEL" in Oracle? If not, then how can we create a column similar to "LEVEL"? Erwin Brandstetter Postgres does not have hierarchical queries . No CONNECT BY , therefore also no LEVEL . The additional module tablefunc provides the function connectby() doing almost the same. See: What is the equivalent PostgreSQL syntax to Oracle's CONNECT BY ... START WITH? Or you can do similar things with a standard recursive CTE and a level column that's incremented with every recursion. This query in Oracle: SELECT employee_id, last_name, manager_id, LEVEL FROM

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 =

Why does CONNECT BY LEVEL on a table return extra rows?

余生长醉 提交于 2019-11-30 13:13:10
问题 Using CONNECT BY LEVEL seems to return too many rows when performed on a table. What is the logic behind what's happening? Assuming the following table: create table a ( id number ); insert into a values (1); insert into a values (2); insert into a values (3); This query returns 12 rows (SQL Fiddle). select id, level as lvl from a connect by level <= 2 order by id, level One row for each in table A with the value of column LVL being 1 and three for each in table A where the column LVL is 2, i

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:

CONNECT BY or hierarchical queries in RDBMS other than Oracle

北城余情 提交于 2019-11-29 10:43:45
Oracle ships with a very handy feature. You can create hierarchical queries (recursive behaviour) using the following clause: CONNECT BY [NOCYCLE] {condition [AND condition...]} [START WITH condition] As documented here: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/queries003.htm I'm wondering, are there any other established RDBMS that support an equivalent or similar syntax? Or can recursive behaviour like this be generically simulated using regular SQL? A good example that I'd like to be able to simulate is this (taken from the Oracle documentation): SELECT LPAD(' ', 2 *