recursive-query

SQL recursive query

白昼怎懂夜的黑 提交于 2019-12-30 18:48:44
问题 I have a Table Category, 1) Id 2) CategoryName 3) CategoryMaster with data as: 1 Computers 0 2 Software 1 3 Multimedia 1 4 Animation 3 5 Health 0 6 Healthsub 5 and i have created recursive query as: ;WITH CategoryTree AS ( SELECT *, CAST(NULL AS VARCHAR(50)) AS ParentName, 0 AS Generation FROM dbo.Category WHERE CategoryName = 'Computers' UNION ALL SELECT Cat.*,CategoryTree.CategoryName AS ParentName, Generation + 1 FROM dbo.Category AS Cat INNER JOIN CategoryTree ON Cat.CategoryMaster =

Get hierarchical structure using SQL Server

岁酱吖の 提交于 2019-12-30 07:05:12
问题 I have a self-referencing table with a primary key, id and a foreign key parent_id . +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PK | NULL | IDENTITY | | parent_id | int(11) | YES | | NULL | | | name | varchar(255) | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+ I have got a table as

Recursion On Database Query to get hierarchical result using Hibernate - Java

让人想犯罪 __ 提交于 2019-12-29 07:41:32
问题 I have a table in my Oracle database with child parent relationship like - What I need is to access the list of child in hierarchical manner in Hibernate. When Father logs in - he gets Son as child. When Grandfather logs in - he gets Son, Father, Uncle. When Super Grandfather logs in - he gets Son, Father, Uncle and Grandfather. I have a java entity for same as well. public class relations { private String child; private String parent; public getChild(); public getParent(); public setChild();

Recursive function in sql server 2005?

烂漫一生 提交于 2019-12-29 01:48:12
问题 Can anybody suggest programming examples that illustrate recursive functions? For example fibonacci series or factorial.. 回答1: Here are a few articles that I found using google.com ;) Recursion in T–SQL Using recursion in stored procedures A Recursive User-Defined Function (SQL Server 2000) 回答2: Search for "common table expressions." See also this link Update Adding example from the above-referenced link: ;WITH Fibonacci(n, f, f1) AS ( -- This is the anchor part -- Initialize level to 1 and

SQL Server Equivalent of Oracle 'CONNECT BY PRIOR', and 'ORDER SIBLINGS BY'

那年仲夏 提交于 2019-12-28 12:06:20
问题 I've got this Oracle code structure I'm trying to convert to SQL Server 2008 ( Note: I have used generic names, enclosed column names and table names within square brackets '[]', and done some formatting to make the code more readable) : SELECT [col#1], [col#2], [col#3], ..., [col#n], [LEVEL] FROM (SELECT [col#1], [col#2], [col#3], ..., [col#n] FROM [TABLE_1] WHERE ... ) CONNECT BY PRIOR [col#1] = [col#2] START WITH [col#2] IS NULL ORDER SIBLINGS BY [col#3] What is the SQL Server equivalent

Multiple CTE in single query

隐身守侯 提交于 2019-12-27 17:30:29
问题 Is it possible to combine multiple CTEs in single query with arel ? I am looking for way to get result like this: WITH 'cte1' AS ( ... ), WITH RECURSIVE 'cte2' AS ( ... ), WITH 'cte3' AS ( ... ) SELECT ... FROM 'cte3' WHERE ... As you can see, I have one recursive CTE and two non recursive. 回答1: Use the key word WITH once at the top. If any of your Common Table Expressions (CTE) are recursive (rCTE) you have to add the keyword RECURSIVE at the top once also, even if not all CTEs are recursive

select all products and join main category through sub-categories (unknown level)

丶灬走出姿态 提交于 2019-12-25 08:13:50
问题 I have 2 tables Categories id - name - parent 1 - Category A - 0 2 - Category B - 0 3 - Category C - 0 4 - Category D - 0 5 - Subcategory Of 1 - 1 6 - Subcategory Of 5 - 5 7 - Subcategory Of 5 - 5 Product id - name - category - description 1 - Name - 5 - Description How to select all products and join main category through sub-categories? Product categories can has only 1 or 2 or 3 or 4 levels (Unknown level). I use "WITH RECURSIVE" in categories table but can't find the way to combine

select all products and join main category through sub-categories (unknown level)

廉价感情. 提交于 2019-12-25 08:10:04
问题 I have 2 tables Categories id - name - parent 1 - Category A - 0 2 - Category B - 0 3 - Category C - 0 4 - Category D - 0 5 - Subcategory Of 1 - 1 6 - Subcategory Of 5 - 5 7 - Subcategory Of 5 - 5 Product id - name - category - description 1 - Name - 5 - Description How to select all products and join main category through sub-categories? Product categories can has only 1 or 2 or 3 or 4 levels (Unknown level). I use "WITH RECURSIVE" in categories table but can't find the way to combine

Transform a parent/child table to a fixed column dimentional table

三世轮回 提交于 2019-12-25 07:59:51
问题 I've a relational table (id, parentId, name) which I'd like to convert to a flattened dimentional table (id, Level1, Level2, Level3, Level4) I'm ok fixing the depth at 4 deep. I've made progress with a recursive CTE and pivot, but the result set isn't right I get Id Name Level1 Level2 0 Root NULL NULL 1 NULL L1 NULL but I need Id Name Level1 Level2 0 Root NULL NULL 1 Root L1 NULL here's what I have to date with rcte as ( select h.id ,h.parent_id ,h.name ,1 as HierarchyLevel FROM RelTable h

Returning all children with a recursive select

↘锁芯ラ 提交于 2019-12-25 07:46:52
问题 Good day everyone! I've got a graph. First, I know how to build simple recursive selections. I read some info on msdn . In this image you can see that (for example) the top node of the graph, which is numbered 0, influences node number 1 (etc (2->4), (3->4), (4->5), (5->6), (1->5)) TASK: for every node show nodes which it influences. For example, number 1 influences 5 and 6. The result SQL must return something like this: who_acts| on_whom_influence 0 | 1 0 | 5 0 | 6 1 | 5 1 | 6 2 | 4 2 | 5 2