recursive-query

Recursive CTE stop condition for loops

微笑、不失礼 提交于 2019-12-06 04:07:01
I need to iterate a graph with loops using the recursive CTE. The problem is the loop part. I want if there are loops, then the shortest path to be selected. This basically means ignoring the loops because the recursion is "width first". The example below shows the returned data: The problem is the commented out INSERT statement that creates a loop. With it uncommented, obviously, the query will never finish. What I need is to return the same data as it would be without the loop. DROP TABLE IF EXISTS edges; CREATE TABLE edges( src integer, dst integer, data integer ); INSERT INTO edges VALUES

sql recursive function - to find managers

浪子不回头ぞ 提交于 2019-12-06 03:17:37
问题 Lets say I have the following table User_ID Manager_ID --------------------- Linda Jacob Mark Linda Kevin Linda Steve Mark John Kevin Basically the requirement is to pull all the managers under the user_id you are searching for. So for instance if I send in 'Linda' then it should return me: 'Mark', 'Kevin', 'Steve', 'John' or if I send in 'Mark' then it should return me: Steve I have heard of recursive function but I am unsure of how to do this. Any help would be appreciated. 回答1: Use: WITH

Recursively list concents of Oracle's DBA_DEPENDENCIES view

妖精的绣舞 提交于 2019-12-06 01:33:13
I would like a list of dependent tables (ultimately) of a given view. For example: SELECT NAME, TYPE, REFERENCED_NAME, REFERENCED_TYPE FROM DBA_DEPENDENCIES WHERE OWNER='FOO' AND NAME='VIEW_O1' The results: VIEW_O1 VIEW TABLE_01 TABLE VIEW_O1 VIEW TABLE_02 TABLE VIEW_O1 VIEW TABLE_03 TABLE VIEW_O1 VIEW VIEW_02 VIEW VIEW_O1 VIEW VIEW_03 VIEW I would like it to resemble: VIEW_O1 VIEW TABLE_01 TABLE VIEW_O1 VIEW TABLE_02 TABLE VIEW_O1 VIEW TABLE_03 TABLE VIEW_O1 VIEW VIEW_02 VIEW VIEW_O1 VIEW VIEW_03 VIEW VIEW_O2 VIEW TABLE_03 TABLE VIEW_O2 VIEW TABLE_04 TABLE VIEW_O3 VIEW TABLE_05 TABLE VIEW_O3

Deleting rows recursively in a self-referencing table using a CTE. How does the process take place?

拥有回忆 提交于 2019-12-06 00:51:38
问题 I'm working on a side project, and in order to delete a row and all its descendants in a self-referencing table, I'm using a recursive CTE like this inside a trigger: CREATE TRIGGER dbo.tr_Comment_Delete ON dbo.Comment INSTEAD OF DELETE AS ;WITH IDs AS ( SELECT id FROM DELETED UNION ALL SELECT c.id FROM Comment AS c INNER JOIN IDs AS i ON c.parent_comment_id = i.id ) DELETE FROM Comment WHERE id IN (SELECT id FROM IDs); GO This is the self-referencing table Although I have this code working

Self-join Query

為{幸葍}努か 提交于 2019-12-05 21:47:28
Is it possible to do parent-child query just using join without looping through temporary table? Database sample : menuid name parent url ---------------------------------------------------------- A0000 Master A0000 # A0001 Rekening A0000 /master/rekening.aspx A0002 Master Nominal A0001 /master/nominal.aspx A0003 Master Satuan Other A0001 /master/satuan.aspx A0004 Master Kondisi A0000 /master/kondisi.aspx A0005 Master Tujuan A0003 /master/tujuan.aspx A0006 Master Item A0003 /master/item.aspx A0007 Master Warehouse A0000 /master/warehouse.aspx A0008 Master Kapal A0006 /master/kapal.aspx Desired

How CTE really works?

怎甘沉沦 提交于 2019-12-05 20:24:24
问题 I came across this CTE solution for concatenating row elements and I thought it's brilliant and I realized how powerful CTEs can be. However, in order to use such a tool effectively I need to know how it works internally to build that mental image which is essential for beginners, like me, to use it in different scenarios. So I tried to slow motion the process of the above snippet and here is the code USE [NORTHWIND] GO /****** Object: Table [dbo].[Products2] Script Date: 10/18/2011 08:55:07

postgres - with recursive

本秂侑毒 提交于 2019-12-05 19:10:19
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 where level_id = 197 UNION ALL SELECT t.level_id, t.parent_level FROM level t, recursetree rt WHERE rt

SQL select descendants of a row

走远了吗. 提交于 2019-12-05 12:00:26
Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes ( id INTEGER PRIMARY KEY, parent INTEGER -- references nodes(id) ); Although cycles can be created in this representation, let's assume we never let that happen. The table will only store a collection of roots (records where parent is null) and their descendants. The goal is to, given an id of a node on the table, find all nodes that are descendants of it. A is a descendant of B if either A 's parent is B or A 's parent is a descendant of B . Note the recursive definition. Here is some sample data: INSERT INTO nodes

Applying CTE for recursive queries

走远了吗. 提交于 2019-12-05 10:46:22
I am trying to apply CTE and recursive queries. The database is MariaDB 10.2 or greater. Business rules are as follows: An account can either be a holding or a portfolio. A holding consists of a given amount of money. Holdings can be active and inactive. A portfolio contains zero or more accounts, and these accounts can belong to more than one portfolio. The total value of each account is multiplied by a "weight" factor when determining the value of a portfolio. My schema is as follows (note char is used for id type for illustration purposes only, but I will really use int ): CREATE TABLE IF

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

寵の児 提交于 2019-12-05 08:17:11
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 ) Something like this: WITH RECURSIVE hierarchy (folderid, ParentFolderId, FolderName) as ( SELECT folderid, ParentFolderId, FolderName FROM folders WHERE ParentFolderID = -1 UNION ALL SELECT folderid, ParentFolderId, FolderName FROM folders f JOIN hierarchy p ON p