recursive-query

Split values in parts with sqlite

谁说胖子不能爱 提交于 2019-12-01 08:17:32
I'm struggling to convert a | a1,a2,a3 b | b1,b3 c | c2,c1 to: a | a1 a | a2 a | a3 b | b1 b | b2 c | c2 c | c1 Here are data in sql format: CREATE TABLE data( "one" TEXT, "many" TEXT ); INSERT INTO "data" VALUES('a','a1,a2,a3'); INSERT INTO "data" VALUES('b','b1,b3'); INSERT INTO "data" VALUES('c','c2,c1'); The solution is probably recursive Common Table Expression. Here's an example which does something similar to a single row: WITH RECURSIVE list( element, remainder ) AS ( SELECT NULL AS element, '1,2,3,4,5' AS remainder UNION ALL SELECT CASE WHEN INSTR( remainder, ',' )>0 THEN SUBSTR(

Recursive query for table dependencies is not recursing not as much as I'd like

邮差的信 提交于 2019-12-01 06:32:27
I had an idea that I could write a query to find all the descendent tables of a root table, based on foreign keys. Query looks like this: select level, lpad(' ', 2 * (level - 1)) || uc.table_name as "TABLE", uc.constraint_name, uc.r_constraint_name from all_constraints uc where uc.constraint_type in ('R', 'P') start with uc.table_name = 'ROOT_TAB' connect by nocycle prior uc.constraint_name = uc.r_constraint_name order by level asc; The results I get look like this: 1 ROOT_TAB XPKROOTTAB 1 ROOT_TAB R_20 XPKPART_TAB 2 CHILD_TAB_1 R_40 XPKROOTTAB 2 CHILD_TAB_2 R_115 XPKROOTTAB 2 CHILD_TAB_3 R_50

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

孤街浪徒 提交于 2019-12-01 05:47:41
问题 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

Recursive query for table dependencies is not recursing not as much as I'd like

霸气de小男生 提交于 2019-12-01 04:48:04
问题 I had an idea that I could write a query to find all the descendent tables of a root table, based on foreign keys. Query looks like this: select level, lpad(' ', 2 * (level - 1)) || uc.table_name as "TABLE", uc.constraint_name, uc.r_constraint_name from all_constraints uc where uc.constraint_type in ('R', 'P') start with uc.table_name = 'ROOT_TAB' connect by nocycle prior uc.constraint_name = uc.r_constraint_name order by level asc; The results I get look like this: 1 ROOT_TAB XPKROOTTAB 1

Split values in parts with sqlite

三世轮回 提交于 2019-12-01 03:39:28
问题 I'm struggling to convert a | a1,a2,a3 b | b1,b3 c | c2,c1 to: a | a1 a | a2 a | a3 b | b1 b | b2 c | c2 c | c1 Here are data in sql format: CREATE TABLE data( "one" TEXT, "many" TEXT ); INSERT INTO "data" VALUES('a','a1,a2,a3'); INSERT INTO "data" VALUES('b','b1,b3'); INSERT INTO "data" VALUES('c','c2,c1'); The solution is probably recursive Common Table Expression. Here's an example which does something similar to a single row: WITH RECURSIVE list( element, remainder ) AS ( SELECT NULL AS

PostgreSQL get parent categories from table

强颜欢笑 提交于 2019-12-01 01:50:17
I have the table as like below. CREATE TABLE my.categories (id bigint, parent_id bigint, name varchar(128)); INSERT INTO my.categories (id, parent_id, name) VALUES (1, null, 'LEVEL 1'); INSERT INTO my.categories (id, parent_id, name) VALUES (2, 1, 'LEVEL 2.1'); INSERT INTO my.categories (id, parent_id, name) VALUES (3, 1, 'LEVEL 2.2'); INSERT INTO my.categories (id, parent_id, name) VALUES (4, 2, 'LEVEL 3.1.1'); INSERT INTO my.categories (id, parent_id, name) VALUES (5, 2, 'LEVEL 3.1.2'); INSERT INTO my.categories (id, parent_id, name) VALUES (6, 3, 'LEVEL 3.2.1'); +----+-----------+----------

Recursive query in DB2 to get all items in the chain

流过昼夜 提交于 2019-12-01 00:25:34
I have to retrieve all clients linked via loans by giving only one as input. Example I have a table data as TABLEA LOAN_ID CLIENT_ID 1 7 1 8 2 7 4 8 4 9 4 10 5 9 5 11 13 2 14 3 If I have given only input as CLIENT_ID=7 then the query has to select all the columns from above table except last two column because client_id 7 has 1,2 LOAN_ID and in 1 the CLIENT_ID 8 has loan_id=4 and in this loan CLIENT_id 9 has again 5 as loan_id. can we write a sql query for this without stored procedure in DB2 ? Here is the answer to your question using recursive CTE query: WITH links AS ( SELECT loan_id,

How sql with-recursive statement interpreted?

女生的网名这么多〃 提交于 2019-11-30 21:22:19
I would like to ask get some help about understanding how "with recursive" works. More precisely WHY the anchor query (the non-recursive term) isn't replicated into the sub call of the CTE. I tried my best to understand alone but I'm not sure. First of all let's take the example of PostgreSQL which is the simplest one I found (make the sum of 1 to 100) : WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t; My code walkthrough ( I used links below) : Evaluate the non-recursive term. For UNION [...]. Include all remaining rows in the result of the

PostgreSQL get parent categories from table

 ̄綄美尐妖づ 提交于 2019-11-30 21:20:19
问题 I have the table as like below. CREATE TABLE my.categories (id bigint, parent_id bigint, name varchar(128)); INSERT INTO my.categories (id, parent_id, name) VALUES (1, null, 'LEVEL 1'); INSERT INTO my.categories (id, parent_id, name) VALUES (2, 1, 'LEVEL 2.1'); INSERT INTO my.categories (id, parent_id, name) VALUES (3, 1, 'LEVEL 2.2'); INSERT INTO my.categories (id, parent_id, name) VALUES (4, 2, 'LEVEL 3.1.1'); INSERT INTO my.categories (id, parent_id, name) VALUES (5, 2, 'LEVEL 3.1.2');

SQL Server CTE hierarchy issue

一个人想着一个人 提交于 2019-11-30 20:41:32
i have an application serve multilevel of permissions and roles i have this HIERARCHY : Country ....Region ........City ............Association ................Center ....................School ........................Class this HIERARCHY i name it "EntityLevels" | ID | Name | ParentID | |----|-------------|----------| | 1 | Country | Null | | 2 | Region | 1 | | 3 | City | 2 | | 4 | Association | 3 | | 5 | Center | 4 | | 6 | School | 5 | | 7 | Class | 6 | i have also a Groups Table which means Jobs | ID | Name | EntityLevels | |----|--------------------|--------------| | 1 | CountryAdmins | 1