recursive-query

Database about a forum design issue

眉间皱痕 提交于 2019-12-10 12:18:59
问题 I am currently designing a database about a forum. The database will have for now the current tables - A table about the users, about the categories. The problem comes when I want to add the QuestionAndComments table. The QuestionAndComments table must have UserId (a foreign key pointing to the users table) and ParentId (will be null if it is a question and will have some value if it is a comment - the comment could be to a question or to an other comment) of the question but it must also

Find cluster given node in PostgreSQL

白昼怎懂夜的黑 提交于 2019-12-10 10:25:12
问题 I am representing a graph in Postgres 9.1 (happens to be bidirectional and cyclic): CREATE TABLE nodes ( id SERIAL PRIMARY KEY, name text ); CREATE TABLE edges ( id SERIAL PRIMARY KEY, node1_id int REFERENCES nodes(id), node2_id int REFERENCES nodes(id) ); Given a particular node ID, want to retrieve all other nodes in that cluster. I started with the "Paths from a single node" example here, and this is where I got: WITH RECURSIVE search_graph(id, path) AS ( SELECT id, ARRAY[id] FROM nodes

Recursive CTE stop condition for loops

自作多情 提交于 2019-12-10 10:23:52
问题 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

How to write a recursive query in SQL Server 2000

北城余情 提交于 2019-12-10 00:12:42
问题 I have a table which has a list which looks like this References R. Name LineNo. References A 1.1 (B,24.1) A 6.3 (A, 1.3), (D, 22.1) B 23.1 (A. 1.2) B 24.1 (B,23.1) C 2 (A, 1.1) D 3.12 (A, 6.3) The query should go one by one in the records and generate a value based on the references, pick first one lets say, which is Report Name A, Line No. 1.1, Now the reference is (B, 24.1), which means we need to find Report Name B, line no 24.1 and pick its value. In the same table R.Name B and Line No B

How to traverse a hierarchical tree-structure structure backwards using recursive queries

喜夏-厌秋 提交于 2019-12-09 04:39:37
问题 I'm using PostgreSQL 9.1 to query hierarchical tree-structured data, consisting of edges (or elements) with connections to nodes. The data are actually for stream networks, but I've abstracted the problem to simple data types. Consider the example tree table. Each edge has length and area attributes, which are used to determine some useful metrics from the network. CREATE TEMP TABLE tree ( edge text PRIMARY KEY, from_node integer UNIQUE NOT NULL, -- can also act as PK to_node integer

Recursive query in DB2 to get all items in the chain

佐手、 提交于 2019-12-09 01:39:39
问题 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

Infinite loop in CTE when parsing self-referencing table

五迷三道 提交于 2019-12-08 23:06:38
问题 I'm using the following Common Table Expression to parse self-referencing table. But the CTE does not work, produces and infinite loop and generates an error: Msg 530, Level 16, State 1, Line 1 The statement terminated. The maximum recursion 100 has been exhausted before statement completion. How to modify this CTE to get it work? SET NOCOUNT ON; USE tempdb; IF OBJECT_ID('dbo.Employees', 'U') IS NOT NULL DROP TABLE dbo.Employees; CREATE TABLE dbo.Employees ( empid INT NOT NULL PRIMARY KEY,

How to optimize LINQ-to-SQL for recursive queries?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 09:10:37
I have the following SQL table: ObjectTable -------------------------------------------------- | ID | Name | Order | ParentID | | int PK | nvarchar(50) | int | int FK | ObjectTable.ParentID is a nullable field with a relationship to a different Object record's ID. LINQ-to-SQL generates an class that looks like: public class DbObject{ int ID { get; set; } string Name { get; set; } int Order { get; set; } int? ParentID { get; set; } DbObject Parent { get; set; } EntitySet<DbObject> ChildObjects { get; set; } } When I load a DbObject instance, I need to be able to recursively access the child

How to optimize LINQ-to-SQL for recursive queries?

為{幸葍}努か 提交于 2019-12-08 08:19:38
问题 I have the following SQL table: ObjectTable -------------------------------------------------- | ID | Name | Order | ParentID | | int PK | nvarchar(50) | int | int FK | ObjectTable.ParentID is a nullable field with a relationship to a different Object record's ID. LINQ-to-SQL generates an class that looks like: public class DbObject{ int ID { get; set; } string Name { get; set; } int Order { get; set; } int? ParentID { get; set; } DbObject Parent { get; set; } EntitySet<DbObject> ChildObjects

Recursive QuerySet with django

点点圈 提交于 2019-12-08 07:29:05
问题 I have this model referencing itself to allow building a tree: class PartCategory(models.Model): parent = models.ForeignKey('PartCategory', on_delete=models.DO_NOTHING, null=True, default=None, blank=True) name = models.TextField() I now have an SQL query to get one element and all of its child in one select (in this example the element with id=64): WITH RECURSIVE under_partcategory(id,name, parent_id,level) AS ( select api_partcategory.id,api_partcategory.name,api_partcategory.parent_id,0