recursive-query

How CTE really works?

北城以北 提交于 2019-12-04 02:16:45
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 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF OBJECT_ID('Products2','U') IS NOT NULL DROP

Recursive select via LINQ? [duplicate]

纵然是瞬间 提交于 2019-12-04 01:42:24
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: linq to sql recursive query I got stuck with having to build a Recursive select via LINQ for the self referencing table. I use this class: public class DivisionHierarchy { public Division Division { get; set; } public IEnumerable<DivisionHierarchy> Divisions { get; set; } } and I created this function but somehow it is infinite. public IEnumerable<DivisionHierarchy> GetDivisionHierarchy(IEnumerable<Division>

Aggregating connected sets of nodes / edges

半腔热情 提交于 2019-12-03 16:23:56
I have a connected set of edges with unique nodes. They are connected using a parent node. Consider the following example code and illustration: CREATE TABLE network ( node integer PRIMARY KEY, parent integer REFERENCES network(node), length numeric NOT NULL ); CREATE INDEX ON network (parent); INSERT INTO network (node, parent, length) VALUES (1, NULL, 1.3), (2, 1, 1.2), (3, 2, 0.9), (4, 3, 1.4), (5, 4, 1.6), (6, 2, 1.5), (7, NULL, 1.0); Visually, two groups of edges can be identified. How can the two groups be identified using PostgreSQL 9.1, and length summed? The expected result is shown:

How can I paginate with Spring Security, Hibernate and row level ACL

a 夏天 提交于 2019-12-03 15:12:58
I'm reading about Spring Security and wonder whether it's possible to use Spring ACL together with hibernate and pagination. The resulting SQL is surely scary but possible to be auto-generated. It's even possible to use hierarchical ACL if the database supports recursive query evaluation . Using a post filter is no solution since it breaks pagination and is an unnecessary overhead compared to ACL filtering inside the database. So I actually have the pieces to build a solution. I want to know whether somebody has already done it. Links: Similar question from 2012 without response link list

Recursive SQL statement (PostgreSQL 9.1.4)

断了今生、忘了曾经 提交于 2019-12-03 11:59:47
PostgreSQL 9.1 Business situation Every month, there is a new batch of accounts given to a specific process. Every batch can be described by month, number of accounts and total balance of accounts. The goal of the process is to recover some of the balance back from customers. Each batch is than tracked separately on a monthly basis (amount recovered on each month since batch was transferred to the process). Goal My goal is to predict what amount will be recovered in the future. Data definition create table vintage_data ( granularity date, /* Month when account entered process*/ distance_in

PostgreSQL WITH RECURSIVE performance

北城余情 提交于 2019-12-03 11:40:43
I have a simple question. Somehow I was unable to find a definitive answer. How much is WITH RECURSIVE syntax optimized in PostgreSQL? By that I mean: is it merely a syntactic sugar for a series of non recursive queries, OR is it more of a single statement that despite its complicated semantics has been optimized as a whole. A follow-up question - just about how much is it possible to optimize this kind of syntax? Of course some concrete data on the matter is most welcome. My experience is that it is indeed very well optimized. Check out the execution plan for your query generated by EXPLAIN

Oracle SQL Analytic query - recursive spreadsheet-like running total

不羁岁月 提交于 2019-12-03 04:04:33
问题 I have the following data, composed of the A value, ordered by MM (month). The B column is computed as GREATEST(current value of A + previous value of B, 0) in a spreadsheet-like fashion. How can I compute B using a SQL Query? I tried using Analytic Functions, but I was unable to succeed. I know there is the Model Clause; I found a similar example, but I don't know where to begin. I am using Oracle 10g, therefore I cannot use recursive queries. Here is my test data: MM | A | B -----------+---

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

左心房为你撑大大i 提交于 2019-12-03 01:57:49
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 REFERENCES tree (from_node), mode character varying(5), -- redundant, but illustrative length numeric NOT NULL,

Total children values based on parent

时光毁灭记忆、已成空白 提交于 2019-12-02 23:47:13
问题 I have 2 tables: table1 , table2 Parent Child Point Parent Total a b 100 a 0(default) (result = 1050) b c 200 b 0 (result = 950) c d 250 c 0 (result = 750) d e 500 The result in table2 should be sum of the children points based on parent in table1 . a---b---c---d---e I tried many times but cant figure out. UPDATE table2 set Total=??? 回答1: Use a recursive CTE: WITH RECURSIVE cte AS ( SELECT parent, child, point AS total FROM tbl1 UNION ALL SELECT c.parent, t.child, c.total + t.point FROM cte c

The maximum recursion 100 has been exhausted before statement completion error showing in SQL Query

白昼怎懂夜的黑 提交于 2019-12-02 19:19:20
问题 "The maximum recursion 100 has been exhausted before statement completion" error showing in SQL Query WITH DepartmentCTE AS ( SELECT ID, DepartmentName, RootID, RecursionLevel = 1, ParentRoot = CAST('None' AS NVARCHAR(max)), LastParentCatID = RootID, DisplayOrder FROM Department UNION ALL SELECT cte.ID, cte.DepartmentName, cte.RootID, cte.RecursionLevel + 1, ParentRoot = CASE WHEN cte.RecursionLevel = 1 THEN '' ELSE cte.ParentRoot + '>' END + c.DepartmentName, LastParentCatID = c.RootID, cte