recursive-query

Get the paths from a database of points in sql

試著忘記壹切 提交于 2019-12-02 04:40:40
Consider that I have a table of points, each point has 2 coordinates. For example: Source | Destination 1 | 2 2 | 3 3 | 7 5 | 7 9 | 12 I'd like to write a query in SQL that gives me the following: All the paths that these points make. Each path must have connected points, as in the next point's source coordinate is the same as the previous point's destination point. a path can't be cyclic. For example, in the table above, running the query should return 3*paths: (1,2) (2,3) (3,7) (5,7) (9,12) I thought about duplicating the points table, let's call them A and B, then set a condition: SELECT

how can I get all ids starting from a given id recursively in a postgresql table that references itself?

北城以北 提交于 2019-12-02 04:19:14
the title may not be very clear so let's consider this example (this is not my code, just taking this example to model my request) I have a table that references itself (like a filesystem) id | parent | name ----+----------+------- 1 | null | / 2 | 1 | home 3 | 2 | user 4 | 3 | bin 5 | 1 | usr 6 | 5 | local Is it possible to make a sql request so if I choose : 1 I will get a table containing 2,3,4,5,6 (because this is the root) so matching : /home /home/user /home/user/bin /usr etc... 2 I will get a table containing 3,4 so matching : /home/user /home/user/bin and so on Use recursive common

Help with recursive CTE query joining to a second table

China☆狼群 提交于 2019-12-02 04:05:02
问题 My objective is to recurse through table tbl and while recursing through that table select a country abbreviation (if it exists) from another table tbl2 and append those results together which are included in the final output. The example I'll use will come from this post tbl2 has a Foreign Key 'tbl_id' to tbl and looks like this INSERT INTO @tbl2( Id, Abbreviation, tbl_id ) VALUES (100, 'EU', 1) ,(101, 'AS', 2) ,(102, 'DE', 3) ,(103, 'CN', 5) *Note: not all the countries have abbreviations.

Recursive query for parent child hierarchy. Get descendants from a top node

岁酱吖の 提交于 2019-12-02 03:22:44
I have a table that stores hierarchy data in parent child format with one top node. Multiple levels, and each parent having multiple children. How can I write a recursive query to select only the parent child rows from a particular node down to the last child? Example table Parent|child 1 |2 1 |3 2 |4 2 |5 3 |6 3 |7 6 |8 How can I retrieve only rows from node 3 and all its descendants? If your DBMS is SQL Server you can accomplish this through Common Table Expressions (CTE) using recursion. I believe this works on all versions 2008R2 and above. The below query will give you all the Parent -

Postgres query to get all the children ids

我与影子孤独终老i 提交于 2019-12-02 03:19:45
问题 I'm an SQL noob and wrote only very basic queries so far. I have a table that looks like this item_full_name varchar(65535) item_id bigint item_owners varchar(255) item_approver_group varchar(255) item_state varchar(255) item_parent_id bigint item_children varchar(65535) Initially item_children is empty for all the rows but each item has a item_parent_id and is not null. I want to write a query that looks at all the rows & corresponding parent ids and updates each row's item_children with a

Postgres query to get all the children ids

∥☆過路亽.° 提交于 2019-12-02 01:16:12
I'm an SQL noob and wrote only very basic queries so far. I have a table that looks like this item_full_name varchar(65535) item_id bigint item_owners varchar(255) item_approver_group varchar(255) item_state varchar(255) item_parent_id bigint item_children varchar(65535) Initially item_children is empty for all the rows but each item has a item_parent_id and is not null. I want to write a query that looks at all the rows & corresponding parent ids and updates each row's item_children with a string of children ids separated by comma. for eg. item_full_name | item_id | item_owners | item_parent

Help with recursive CTE query joining to a second table

廉价感情. 提交于 2019-12-02 00:32:44
My objective is to recurse through table tbl and while recursing through that table select a country abbreviation (if it exists) from another table tbl2 and append those results together which are included in the final output. The example I'll use will come from this post tbl2 has a Foreign Key 'tbl_id' to tbl and looks like this INSERT INTO @tbl2( Id, Abbreviation, tbl_id ) VALUES (100, 'EU', 1) ,(101, 'AS', 2) ,(102, 'DE', 3) ,(103, 'CN', 5) *Note: not all the countries have abbreviations. The trick is, I want all the countries in Asia to at least show the abbreviation of Asia which is 'AS'

how to make a sql loop?

。_饼干妹妹 提交于 2019-12-01 22:21:01
问题 here is the simplified table filesystem (id, name, parentId); and some entries (1, 'root', NULL) (2, 'folder', 1) (3, 'subfolder', 2) (4, 'subsubfolder', 3) is there a way using native SQL to print the absolute path of one entry ? for instance, the last entry would print 'root/folder/subfolder/subsubfolder'. the entry 2 would print 'root/folder' and so on. 回答1: You can do something like this with tree(id, Level, Hierarchy) as ( select id, 0, cast(Name as varchar(max)) from filesystem union

get ALL last level children (leafs) from a node (hierarhical queries Oracle 11G)

早过忘川 提交于 2019-12-01 09:25:18
I am trying and searching the way to get ALL last level children (leafs) from a node , in a hierchical query in Oracle 11g database. I have 2 tables: "Nodes" (A list of all nodes with their respective value), and "Relation" which specify father-child relation: --NODES-- ID_NODE - VALUE 1 3 2 6 3 9 4 2 5 4 6 5 7 2 8 7 9 8 10 1 --RELATION-- ID_FATHER - ID_CHILD 1 2 1 3 1 4 2 5 2 6 4 7 5 8 5 9 7 10 I have read about CONNECT_BY_ISLEAF which returns 1 if it is a leaf, but I cannot query CONNECT_BY_ISLEAF like the Oracle example, and I don´t get any result. Even though I don´t know exactly how to

Recursive select via LINQ? [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-01 08:34:44
Possible Duplicate: linq to sql recursive query I got stuck with having to build a Recursive select via LINQ for the self referencing table. I use this class: public class DivisionHierarchy { public Division Division { get; set; } public IEnumerable<DivisionHierarchy> Divisions { get; set; } } and I created this function but somehow it is infinite. public IEnumerable<DivisionHierarchy> GetDivisionHierarchy(IEnumerable<Division> allDivisions, Division parentDivision) { Guid? parentDivisionId = null; if (parentDivision != null) parentDivisionId = parentDivision.DivisionID; var childDivisions =