recursive-query

Sql Recursive query to identify relation between 2 users of family tree

久未见 提交于 2019-12-25 04:54:08
问题 Users Data UserId DisplayName 12 Rahul 13 Anjali 14 Faruk 15 Shabina 16 Shakira 17 Ali 18 Ronak 19 Dali 20 Bali 21 Kali 22 Katrina 23 Sita 24 Gita 25 Ram 26 Shyam 27 Suhana 28 Suhas 29 Raj 30 Taslim 31 Ritik 32 Tejas 33 Dipika 34 Bush 35 Dyna 36 Bushiar 37 Salman 38 Ruksana 39 Khushi 40 Tazz 41 Miki 42 Krish 43 Kumbh Family Tree Data ID UserID RelativeId Relation 1 12 13 Spouse 3 12 15 Daughter 4 12 16 Daughter 5 12 17 Son 6 12 18 Son 7 13 12 Spouse 9 13 15 Daughter 10 13 16 Daughter 11 13 17

SQL Query: Fetch ordered rows from a table - II

孤街醉人 提交于 2019-12-25 03:46:32
问题 Following are some entries from a table: id r_id a_id p_id 1 9 9 0 2 9 105 108 3 9 102 9 4 9 106 105 5 9 108 102 6 10 10 0 7 10 15 18 8 10 12 10 9 10 16 15 10 10 18 12 I'm looking for an SQL query that will give an output like: 1 9 9 0 3 9 102 9 5 9 108 102 2 9 105 108 4 9 106 105 6 10 10 0 8 10 12 10 10 10 18 12 7 10 15 18 9 10 16 15 Well, I asked a similar question here but the question was not complete and I also got few excellent answers. Editing that question might make the answers

Retrieve top-level parent MySQL

爱⌒轻易说出口 提交于 2019-12-25 02:31:35
问题 I have the following table: id | parent_id | searchable | value -------------------------------------------- 1 | 0 | 0 | a 2 | 1 | 0 | b 3 | 2 | 1 | c 4 | 0 | 0 | d 5 | 4 | 1 | e 6 | 0 | 0 | f 7 | 6 | 0 | g 8 | 6 | 0 | h 9 | 0 | 1 | i I need to extract all the top level records (so the ones where the parent_id = 0 ). But only the records where the parent OR one of his children is searchable ( searchable = 1 ) So in this case, the output should be: id | parent_id | searchable | value ---------

Why this recursive concat produces: Data too long

时间秒杀一切 提交于 2019-12-24 20:16:45
问题 I have this table on MySQL 8: create table tbl(id varchar(2), val int); insert into tbl values ('A', 1), ('B', 2), ('C', 3), ('D', 4), ('E', 5); The following query should find out which record sets have values that add up to a value not greater than 6 (without importance of order): with recursive cte(greatest_id, ids, total) as ( select id, concat(id, ''), val from tbl union all select tbl.id, concat(cte.ids, tbl.id), cte.total + tbl.val from cte inner join tbl on tbl.id > cte.greatest_id

Oracle - Recursive query using START WITH… CONNECTED BY…?

余生长醉 提交于 2019-12-24 13:25:59
问题 Say i have the following table, using Oracle 10g ARTIFACT_LABEL | DEPENDANT_ON test1 | abc1 test1 | abc2 test1 | abc3 abc3 | xyz1 abc4 | xyz2 and i want to generate a tree structure knowing what test1 depends on, (so i want to return, abc1, abc2, abc3, xyz1 ) ive been using the following: SELECT ARTIFACT_LABEL, DEPENDANT_ON FROM DEPENDANCIES START WITH ARTIFACT_LABEL = 'test1' CONNECT BY NOCYCLE PRIOR ARTIFACT_LABEL = DEPENDANT_ON But this isnt working (it seems to just be doing a SELECT

TSQL - Recursively select date between 2 given dates

不问归期 提交于 2019-12-24 13:15:42
问题 I have table in my db called Tasks . Every record in that table has 2 fields: StartDate , EndDate I need to create recursive stored procedure that will send mails in middle of those dates. For example: Start is 2013-10-22 12:00:00:000 End is 2013-10-24 12:00:00:000 I can do: SELECT DATEADD(ms, DATEDIFF(ms,'2013-10-22 12:00:00:000', '2013-10-24 12:00:00:000')/2, '2013-10-22 12:00:00:000') and then check if now is greater than that date, if Yes then I can send mail. But I need to do that

Recursive CTE with three tables

倾然丶 夕夏残阳落幕 提交于 2019-12-24 05:39:21
问题 I'm using SQL Server 2008 R2 SP1. I would like to recursively find the first non-null manager for a certain organizational unit by "walking up the tree". I have one table containing organizational units "ORG", one table containing parents for each org. unit in "ORG", lets call that table "ORG_PARENTS" and one table containing managers for each organizational unit, lets call that table "ORG_MANAGERS". ORG has a column ORG_ID: ORG_ID 1 2 3 ORG_PARENTS has two columns. ORG_ID, ORG_PARENT 1, NULL

Recursive CTE with three tables

天涯浪子 提交于 2019-12-24 05:38:18
问题 I'm using SQL Server 2008 R2 SP1. I would like to recursively find the first non-null manager for a certain organizational unit by "walking up the tree". I have one table containing organizational units "ORG", one table containing parents for each org. unit in "ORG", lets call that table "ORG_PARENTS" and one table containing managers for each organizational unit, lets call that table "ORG_MANAGERS". ORG has a column ORG_ID: ORG_ID 1 2 3 ORG_PARENTS has two columns. ORG_ID, ORG_PARENT 1, NULL

SQL - calculate forecast average

给你一囗甜甜゛ 提交于 2019-12-23 23:26:23
问题 I'm trying to calculate a forecast of sales based on the 3 previous months which either can be actuals or forecast. company_id Year Month Actuals Forecast 123456 2014 1 10 123456 2014 2 15 123456 2014 3 17 123456 2014 4 14.00 123456 2014 5 15.33 123456 2014 6 15.44 123456 2014 7 14.93 Month 4 = (10+15+17)/3 Month 5 = (15+17+14)/3 Month 6 = (17+14+15.33)/3 Month 7 = (14+15.33+15.44)/3 Let's say I want to calculate the forecast for the next 18 months for each company. I'm looking at the data

How to determine Strahler number on a directed graph for a stream network

南笙酒味 提交于 2019-12-23 19:18:57
问题 Question / example / expected values I need to determine a Strahler number or Strahler stream order for a directed graph representing a stream network. I can derive information forwards and backwards using WITH RECURSIVE queries, but it seems I need to do something different to determine the Strahler number. For example, here is a 19 segment stream network with 10 tributaries and one outlet. The upstream portion of each segment is represented by a node ID. And the same data in a table