recursive-query

SQL grouping interescting/overlapping rows

时间秒杀一切 提交于 2019-12-23 10:21:45
问题 I have the following table in Postgres that has overlapping data in the two columns a_sno and b_sno . create table data ( a_sno integer not null, b_sno integer not null, PRIMARY KEY (a_sno,b_sno) ); insert into data (a_sno,b_sno) values ( 4, 5 ) , ( 5, 4 ) , ( 5, 6 ) , ( 6, 5 ) , ( 6, 7 ) , ( 7, 6 ) , ( 9, 10) , ( 9, 13) , (10, 9 ) , (13, 9 ) , (10, 13) , (13, 10) , (10, 14) , (14, 10) , (13, 14) , (14, 13) , (11, 15) , (15, 11); As you can see from the first 6 rows data values 4,5,6 and 7 in

Create a recursive view that has a “with recursive” statement in Teradata

天大地大妈咪最大 提交于 2019-12-23 08:36:28
问题 I would like to create a recursive view in Teradata (i.e., CREATE RECURSIVE VIEW ) from the following reproducible example: CREATE VOLATILE TABLE vt1 ( foo VARCHAR(10) , counter INTEGER , bar INTEGER ) ON COMMIT PRESERVE ROWS; INSERT INTO vt1 VALUES ('a', 1, '1'); INSERT INTO vt1 VALUES ('a', 2, '2'); INSERT INTO vt1 VALUES ('a', 3, '2'); INSERT INTO vt1 VALUES ('a', 4, '4'); INSERT INTO vt1 VALUES ('a', 5, '1'); INSERT INTO vt1 VALUES ('b', 1, '3'); INSERT INTO vt1 VALUES ('b', 2, '1');

Can we use the output of one recursive query into another recursive query?

元气小坏坏 提交于 2019-12-23 05:29:05
问题 I wanted to find the topological sort of a DAG. create table topo( v1 int, v2 int ); Insert into topo values (1,3),(2,5),(3,4),(4,5),(4,6),(5,7),(6,5),(7,null) WITH RECURSIVE path(S,d) AS( select t1.v1, 0 from topo t1 left outer join topo as t2 on t1.v1=t2.v2 where t2.v2 IS null UNION ALL select distinct t1.v2, path.d + 1 from path inner join topo as t1 on t1.v1=path.S ) select S from path group by S order by MAX(d); This code gives the output of the topological order of a graph. Now i want

How to group/List all nodes of a undirected graph using teradata sql

限于喜欢 提交于 2019-12-23 05:00:49
问题 I have data for many diff. set of undirected graphs in a table (like adjacent list relationship, one node is connected which all node) and I need to group all individual undirected graphs. Eg: all nodes of the particular undirected graphs will be in a group & group name will be the min. of the node. sel d.adj_node, min(d.adj_node) Over (Partition By a.node) as grp table a left join table b on a.adj_node=b.node left join table c on b.adj_node=c.node ​left join table d ​on c.adj_node=d.node​;

postgres - with recursive

人盡茶涼 提交于 2019-12-22 10:03:55
问题 I expected the following to return all the tuples, resolving each parent in the hierarchy up to the top, but it only returns the lowest levels (whose ID is specified in the query). How do I return the whole tree for a given level_id? create table level( level_id int, level_name text, parent_level int); insert into level values (197,'child',177), ( 177, 'parent', 3 ), ( 2, 'grandparent', null ); WITH RECURSIVE recursetree(level_id, levelparent) AS ( SELECT level_id, parent_level FROM level

How can you detect a parent with a nested relationship in a database using SQL?

蓝咒 提交于 2019-12-22 05:10:28
问题 I'm using Firebird 2.1. There is a table name Folders , with the fields: FolderID ParentFolderID FolderName ParentFolderID is -1 if it's the root folder -- otherwise it contains the parent folder's ID. How can I find all parents (up to the root folder) of a low level node? Do I need a recursive query? (Firebird supports them) 回答1: Something like this: WITH RECURSIVE hierarchy (folderid, ParentFolderId, FolderName) as ( SELECT folderid, ParentFolderId, FolderName FROM folders WHERE

How to grep a word inside xml files in a folder

送分小仙女□ 提交于 2019-12-22 01:39:50
问题 I know I can use grep to find a word in all the files present in a folder like this grep -rn core . But my current directory has many sub-directories and I just want to search in all xml files present in the current directory and its all sub directories . How can I do that ? I tried this grep -rn core *.xml // Does not work But it searches for xml files present in the current directory only. It does not do it recursively. 回答1: Try the --include option grep -R --include="*.xml" "pattern" /path

Aggregating connected sets of nodes / edges

断了今生、忘了曾经 提交于 2019-12-21 05:05:08
问题 I have a connected set of edges with unique nodes. They are connected using a parent node. Consider the following example code and illustration: CREATE TABLE network ( node integer PRIMARY KEY, parent integer REFERENCES network(node), length numeric NOT NULL ); CREATE INDEX ON network (parent); INSERT INTO network (node, parent, length) VALUES (1, NULL, 1.3), (2, 1, 1.2), (3, 2, 0.9), (4, 3, 1.4), (5, 4, 1.6), (6, 2, 1.5), (7, NULL, 1.0); Visually, two groups of edges can be identified. How

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

随声附和 提交于 2019-12-20 04:32:52
问题 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

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

不打扰是莪最后的温柔 提交于 2019-12-20 04:32:41
问题 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