hierarchy

How to query and parse adjacent list hierarchy using cte?

梦想的初衷 提交于 2020-01-13 06:04:26
问题 How can I query this parent-child hierarchy to produce a result set in which the levels are in their own columns? Sample data: SET NOCOUNT ON; USE Tempdb; IF OBJECT_ID('dbo.Employees', 'U') IS NOT NULL DROP TABLE dbo.Employees; CREATE TABLE dbo.Employees ( empid INT NOT NULL PRIMARY KEY, mgrid INT NULL REFERENCES dbo.Employees, empname VARCHAR(25) NOT NULL, salary MONEY NOT NULL, CHECK (empid <> mgrid), CHECK (empid > 0) ); CREATE UNIQUE INDEX idx_unc_mgrid_empid ON dbo.Employees(mgrid, empid

How to query and parse adjacent list hierarchy using cte?

大憨熊 提交于 2020-01-13 06:04:08
问题 How can I query this parent-child hierarchy to produce a result set in which the levels are in their own columns? Sample data: SET NOCOUNT ON; USE Tempdb; IF OBJECT_ID('dbo.Employees', 'U') IS NOT NULL DROP TABLE dbo.Employees; CREATE TABLE dbo.Employees ( empid INT NOT NULL PRIMARY KEY, mgrid INT NULL REFERENCES dbo.Employees, empname VARCHAR(25) NOT NULL, salary MONEY NOT NULL, CHECK (empid <> mgrid), CHECK (empid > 0) ); CREATE UNIQUE INDEX idx_unc_mgrid_empid ON dbo.Employees(mgrid, empid

Sql Hierarchy ID Sorting By Level

◇◆丶佛笑我妖孽 提交于 2020-01-13 03:37:09
问题 Is it possible to sort sql data in a hierarchy by it's hierarchy id, and then for each level sort it say alphabetically? So say we have an Employees Table that lists the organizational hierarchy based on the Employees ID You have Bob (5) who has Phil (17) and Charlie(28) Reporting to him, and Josie (6) has Tyler (15) and Mike (56) Reporting to her. If you sort it by HierarchyID it will look like: Bob (/5/) --Phil (/5/17/) --Charlie (/5/28/) Josie (/6/) --Tyler (/6/15/) --Mike (/6/56/) But It

Hierarchical Query Needs to Pull Children, Parents and Siblings

被刻印的时光 ゝ 提交于 2020-01-13 02:47:08
问题 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

HIERARCHY_REQUEST_ERR while trying to add elements to xml file in a for loop

与世无争的帅哥 提交于 2020-01-11 14:08:51
问题 As the title suggests, I am trying to add elements to an xml doc using a for loop. I have an ArrayList of strings called names that I wish to iterate through, and for each name create a <user> element with attribute name and with a child <record> that has the attributes id , time , date , and project . Unfortunately, if you scroll down in the code below to the createDoc() method, when I try to call doc.appendChild(user) , I get the following error: Exception in thread "main" org.w3c.dom

SQL Server get parent list

不打扰是莪最后的温柔 提交于 2020-01-11 03:53:12
问题 I have a table like this: id name parent_id 1 ab1 3 2 ab2 5 3 ab3 2 4 ab4 null 5 ab5 null 6 ab6 null I need to do a query with input id = 1 (for an example) and results will be like this: id name parent_id 5 ab5 null 2 ab2 5 3 ab3 2 1 ab1 3 (List all parents at all level start at item id = 1) 回答1: Something like this perhaps? WITH parents(id,name,parent,level) AS ( SELECT ID, NAME, PARENT, 0 as level FROM TABLE WHERE ID = 1 UNION ALL SELECT ID, NAME, PARENT, Level + 1 FROM TABLE WHERE id =

javascript: access object (array) by array notation string

喜你入骨 提交于 2020-01-10 06:06:13
问题 I would like to access the object provided only it's string path in form of array is known. 1.) there is an object, where root["obj1"]["obj2"] = 1; (in common case root["obj1"]...["objN"] ) 2.) I have ONLY string objectPath known: var objectPath = 'root["obj1"]["obj2"]' 3.) I need NOT only READ the object, but SET it's value, like objectPath = 2; //so root["obj1"]["obj2"] === 2 As I understand there might be some options with eval(), but it gets the value, not the variable; one can loop

How to query graph/hierarchical data in mysql

血红的双手。 提交于 2020-01-10 02:16:06
问题 Suppose I have a table of objects structured in a hierarchy: A |--B |--C | +--D +--E They are stored in a "parent-child" table thus: parent child A B A C C D A E How do I query this to get the structure defined above? I think I need something that produces info like this: object full_path A NULL B A C A D A.C E A I cannot figure out how to do the objects nested more than one level deep. It feels like I might need to iterate over the table (no idea if this is possible in SQL), or otherwise use

Ordering hierarchy from recursive query results in SQL 2005

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-09 23:49:11
问题 I've got a 'Task' table with the following columns (the TaskOrder is for ordering the children within the scope of the parent, not the entire table): TaskId ParentTaskId TaskName TaskOrder I've got this CTE query to return all the rows: with tasks (TaskId, ParentTaskId, [Name]) as ( select parentTasks.TaskId, parentTasks.ParentTaskId, parentTasks.[Name] from Task parentTasks where ParentTaskId is null union all select childTasks.TaskId, childTasks.ParentTaskId, childTasks.[Name] from Task

Should i have Parent or ListOfChild property in Hierarcical object

余生长醉 提交于 2020-01-06 20:36:37
问题 i have a doubt on how to model a hierarchical object for storing a Tag tree: thinking to db table i can use public class Tag { public int Id { get; set; } public int Description { get; set; } private readonly Tag parentTag; public Tag ParentTag { get { return parentTag; } } } the code from parent property come from Hierarchical object and AutoFixture to avoid circular reference but as suggested there, and thinking to object maybe is better having child collection instead of parent: naturally