hierarchy

Get all parents for a child

强颜欢笑 提交于 2019-11-29 05:50:02
问题 I want to retrieve the parentid of an id, if that parentid has a parent again retrieve it, and so on. Kind of hierarchy table. id----parentid 1-----1 5-----1 47894--5 47897--47894 am new to sql server and tried, some queries like: with name_tree as ( select id, parentid from Users where id = 47897 -- this is the starting point you want in your recursion union all select c.id, c.parentid from users c join name_tree p on p.id = c.parentid -- this is the recursion ) select * from name_tree; It

How to query graph/hierarchical data in mysql

巧了我就是萌 提交于 2019-11-29 05:20:00
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 some kind of query I've never encountered before. Additional Info: A need not be the only orphan

Angular 2 - How does ng-bootstrap provide the NgbRadioGroup and NgbButtonLabel to their NgbRadio directive?

落花浮王杯 提交于 2019-11-29 04:53:31
Here is the label code: import {Directive} from '@angular/core'; @Directive({ selector: '[ngbButtonLabel]', host: {'[class.btn]': 'true', '[class.active]': 'active', '[class.disabled]': 'disabled', '[class.focus]': 'focused'} }) export class NgbButtonLabel { active: boolean; disabled: boolean; focused: boolean; } and here is the radio button code: import {Directive, forwardRef, Input, Renderer2, ElementRef, OnDestroy} from '@angular/core'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; import {NgbButtonLabel} from './label'; const NGB_RADIO_VALUE_ACCESSOR = { provide:

Get the level of a hierarchy

偶尔善良 提交于 2019-11-29 03:50:57
问题 I have an array of objects, Where each object has an id and a ParentId property (so they can be arranged in trees). They are in no particular order. Please note that the id 's and parentId 's will not be integers, they will be strings (just wanted to have the sample code cleaner..) There is only one root: lets say its id :1 The data looks like so: data = [ { id:"id-2", parentId:"id-3" }, { id:"id-4", parentId:"2" }, { id:"id-3", parentId:"id-4" }, { id:"id-5", parentId:"id-4" }, { id:"id-6",

Ordering hierarchy from recursive query results in SQL 2005

[亡魂溺海] 提交于 2019-11-29 02:34:18
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 childTasks join tasks on childTasks.ParentTaskId = tasks.TaskId ) select * from tasks This query returns

Interesting tree/hierarchical data structure problem

◇◆丶佛笑我妖孽 提交于 2019-11-29 02:23:54
Colleges have different ways of organizing their departments. Some schools go School -> Term -> Department . Others have steps in between, with the longest being School -> Sub_Campus -> Program -> Term -> Division -> Department . School , Term , and Department are the only ones that always exist in a school's "tree" of departments. The order of these categories never changes, with the second example I gave you being the longest. Every step down is a 1:N relationship. Now, I'm not sure how to set up the relationships between the tables. For example, what columns are in Term ? Its parent could

Creating a JSON Tree from a string hierarchy

拥有回忆 提交于 2019-11-29 02:01:44
Given these 4 variables, var el1 = {name:'ronaldo', team: 'europe/spain/realmadrid'} var el2 = {name:'messi', team: 'europe/spain/barcelona'} var el3 = {name:'gerald', team: 'europe/england/liverpool'} var el4 = {name:'unknown english', team: 'europe/england'} I need to produce this JSON tree hierarchy, { "text":"europe", "leaf":false, "children":[ { "text":"spain", "leaf":false, "children":[ { "text":"realmadrid", "leaf":false, "children":[ { "text":"ronaldo", "leaf":true } ] }, { "text":"barcelona", "leaf":false, "children":[ { "text":"messi", "leaf":true } ] } ] }, { "text":"england", "leaf

Is it good practice to often use instanceof?

六眼飞鱼酱① 提交于 2019-11-28 20:52:01
The scenario. I'm writting game-related code. In that game a Player (its also a class) has a list of Item . There are other types of items that inherit from Item , for example ContainerItem , DurableItem or WeaponItem . Obviously it is very conveniant for me to just have List<Item> . But when I get the players items, the only way for me to distinguish between what type of item is by using the instanceof keyword. I'm sure I've read that reliaing on it is bad practice. Is it ok to use it in this case? Or should I rethink all of my structure? Let's say I am writing some inventory code: public

Create nested list from PHP array for dropdown select field

Deadly 提交于 2019-11-28 19:54:03
I am struggling with an array I want to turn into a nested < select > I need: <select> <option value="1">Top1</option> <option value="2">Top2</option> <option value="9">Top3</option> <option value="7"> - - Top3.1</option> <option value="5"> - - Top3.2</option> <option value="12">- - - - Top3.2.1</option> <option value="6">Top4</option> <option value="4">Top5</option> <option value="8"> - - Top5.1</option> <option value="3"> - - Top5.2</option> I can't work with optgroup, because everything is selectable. As far as I know, you can't select optgroup labels. My array looks like this: [44] =>

CTE to traverse back up a hierarchy?

与世无争的帅哥 提交于 2019-11-28 18:54:31
I can find all the children of a given record in a hierarchical data model (see code below) but I'm not sure how to traverse back up the Parent/Child chain with a given Child ID. Can anyone point me in the right direction to figure out how to do this? Is this possible in Linq to SQL as well? WITH TaskHierarchy (TaskID, [Subject], ParentID, HierarchyLevel, HierarchyPath) AS ( -- Base case SELECT TaskID, [Subject], ParentID, 1 as HierarchyLevel, CONVERT(VARCHAR(MAX),'/') AS HierarchyPath FROM Task WHERE TaskID = 2 UNION ALL -- Recursive step SELECT t.TaskID, t.Subject, t.ParentID, th