recursive-query

How to create a MySQL hierarchical recursive query?

旧巷老猫 提交于 2021-02-17 07:05:12
问题 I have a MySQL table which is as follows: id name parent_id 19 category1 0 20 category2 19 21 category3 20 22 category4 21 ... ... ... Now, I want to have a single MySQL query to which I simply supply the id [for instance say id=19 ] then I should get all its child ids [i.e. result should have ids '20,21,22'].... The hierarchy of the children is not known; it can vary.... I know how to do it using a for loop... but how to achieve the same using a single MySQL query? 回答1: For MySQL 8+: use the

Row for each date from start date to end date

余生长醉 提交于 2021-02-17 05:18:25
问题 What I'm trying to do is take a record that looks like this: Start_DT End_DT ID 4/5/2013 4/9/2013 1 and change it to look like this: DT ID 4/5/2013 1 4/6/2013 1 4/7/2013 1 4/8/2013 1 4/9/2013 1 it can be done in Python but I am not sure if it is possible with SQL Oracle? I am having difficult time making this work. Any help would be appreciated. Thanks 回答1: connect by level is useful for these problems. suppose the first CTE named " table_DT " is your table name so you can use the select

PostgreSQL recursive parent/child query

馋奶兔 提交于 2021-02-16 08:33:38
问题 I'm having some trouble working out the PostgreSQL documentation for recursive queries, and wonder if anyone might be able to offer a suggestion for the following. Here's the data: Table "public.subjects" Column | Type | Collation | Nullable | Default -------------------+-----------------------------+-----------+----------+-------------------------------------- id | bigint | | not null | nextval('subjects_id_seq'::regclass) name | character varying | | | Table "public.subject_associations"

PostgreSQL recursive parent/child query

老子叫甜甜 提交于 2021-02-16 08:32:00
问题 I'm having some trouble working out the PostgreSQL documentation for recursive queries, and wonder if anyone might be able to offer a suggestion for the following. Here's the data: Table "public.subjects" Column | Type | Collation | Nullable | Default -------------------+-----------------------------+-----------+----------+-------------------------------------- id | bigint | | not null | nextval('subjects_id_seq'::regclass) name | character varying | | | Table "public.subject_associations"

sql select parent child recursive in one field

我的未来我决定 提交于 2021-02-07 01:59:08
问题 I do not know how to select query recursive.. id idparent jobNO -------------------------------- 1 0 1 2 1 2 3 1 3 4 0 4 5 4 5 6 4 6 how do the results like this With SqlServer id idparent jobNO ListJob ---------------------------------------- 1 0 1 1 2 1 2 1/2 3 1 3 1/3 4 0 4 4 5 4 5 4/5 6 5 6 4/5/6 回答1: You need to use a Recursive Common Table Expression. There are many useful articles online. Useful Links Simple Talk: SQL Server CTE Basics blog.sqlauthority: Recursive CTE Here is a

Recursive Subquerying with sorting

↘锁芯ラ 提交于 2021-02-02 09:57:45
问题 I looked at Tim Hall's excellent article here, that allows you to work with self-referenced entities and show hierarchical data (starting with top level nodes and joining back recursively), using CTE like syntax in Oracle. So I have code that looks like this: WITH J1(JOBMST_ID, JOBMST_NAME, JOBMST_PRNTID, JOBMST_TYPE, LVL) AS ( SELECT JOBMST_ID, JOBMST_NAME, JOBMST_PRNTID, JOBMST_TYPE, 1 FROM TIDAL.JOBMST WHERE JOBMST_PRNTID IS NULL UNION ALL SELECT J2.JOBMST_ID,J2.JOBMST_NAME,J2.JOBMST

Recursive Subquerying with sorting

浪尽此生 提交于 2021-02-02 09:56:34
问题 I looked at Tim Hall's excellent article here, that allows you to work with self-referenced entities and show hierarchical data (starting with top level nodes and joining back recursively), using CTE like syntax in Oracle. So I have code that looks like this: WITH J1(JOBMST_ID, JOBMST_NAME, JOBMST_PRNTID, JOBMST_TYPE, LVL) AS ( SELECT JOBMST_ID, JOBMST_NAME, JOBMST_PRNTID, JOBMST_TYPE, 1 FROM TIDAL.JOBMST WHERE JOBMST_PRNTID IS NULL UNION ALL SELECT J2.JOBMST_ID,J2.JOBMST_NAME,J2.JOBMST

Calculate start date of almost equal periods

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 17:22:55
问题 SQL Server CREATE TABLE [TABLE_1] ( PLAN_NR decimal(28,6) NULL, START_DATE datetime NULL, MAX_PERIODS decimal(28,6) NULL, ); INSERT INTO TABLE_1 (PLAN_NR, START_DATE, MAX_PERIODS) VALUES (1, '2020-05-01', 8), (2, '2020-08-01', 8); SQL - FIDDLE I've got a table with the columns PLAN_NR , START_DATE and MAX_PERIODS . Each period is exactly 7 days long, unless the period contains a month end. Then the period should be divided into a range before the end of the month up to and including the last

PostgreSQL: Find permission for element, traverse up to root

瘦欲@ 提交于 2021-01-29 01:50:04
问题 Here is the DB Structure I'm using Item ---- ID [PK] Name Desc Links ----- ID [FK] LID [FK] -- Link ID LType -- Link Type (Parent, Alias) Permission ---------- ID [FK] CanRead CanWrite CanDelete Let's assume, we have the below data in the table Item Table ----------- ID Name Desc ================= 0 Root Base Item 1 One First 2 Two Second 3 Three Third 4 Four Forth 5 Five Fifth 6 Six Sixth Links Table ----------- ID LID LType ================== 1 0 Parent 2 0 Parent 3 1 Parent 4 2 Parent 5 4

PostgreSQL: Find permission for element, traverse up to root

≯℡__Kan透↙ 提交于 2021-01-29 01:46:40
问题 Here is the DB Structure I'm using Item ---- ID [PK] Name Desc Links ----- ID [FK] LID [FK] -- Link ID LType -- Link Type (Parent, Alias) Permission ---------- ID [FK] CanRead CanWrite CanDelete Let's assume, we have the below data in the table Item Table ----------- ID Name Desc ================= 0 Root Base Item 1 One First 2 Two Second 3 Three Third 4 Four Forth 5 Five Fifth 6 Six Sixth Links Table ----------- ID LID LType ================== 1 0 Parent 2 0 Parent 3 1 Parent 4 2 Parent 5 4