recursive-query

Oracle SQL Analytic query - recursive spreadsheet-like running total

时光总嘲笑我的痴心妄想 提交于 2019-12-02 16:24:49
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 -----------+--------+------ 2012-01-01 | 800 | 800 2012-02-01 | 1900 | 2700 2012-03-01 | 1750 | 4450 2012-04-01 |

Find Parent Recursively using Query

倾然丶 夕夏残阳落幕 提交于 2019-12-02 14:18:34
I am using postgresql. I have the table as like below parent_id child_id ---------------------- 101 102 103 104 104 105 105 106 I want to write a sql query which will give the final parent of input. i.e suppose i pass 106 as input then , its output will be 103. (106 --> 105 --> 104 --> 103) Sean Here's a complete example. First the DDL : test=> CREATE TABLE node ( test(> id SERIAL, test(> label TEXT NOT NULL, -- name of the node test(> parent_id INT, test(> PRIMARY KEY(id) test(> ); NOTICE: CREATE TABLE will create implicit sequence "node_id_seq" for serial column "node.id" NOTICE: CREATE

Join a table to itself

允我心安 提交于 2019-12-02 12:59:25
问题 this is one on my database tables template. Id int PK Title nvarchar(10) unique ParentId int This is my question.Is there a problem if i create a relation between "Id" and "ParentId" columns? (I mean create a relation between a table to itself) I need some advices about problems that may occur during insert or updater or delete operations at developing step.thanks 回答1: You can perfectly join the table with it self. You should be aware, however, that your design allows you to have multiple

Total children values based on parent

非 Y 不嫁゛ 提交于 2019-12-02 09:11:06
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=??? 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 JOIN tbl1 t ON t.parent = c.child ) SELECT * FROM cte It hurts my brain... The below should work for you,

Recursive SQL to split CSV to table rows

本小妞迷上赌 提交于 2019-12-02 08:07:44
问题 After working on a different question here on SO, I stumbled across recursive CTEs which would on the surface seem a fairly easy way to solve the "Split a csv to table rows" problem. I put this example together DECLARE @InputString varchar(255) = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z' SELECT @InputString = @InputString + ',' ; with MyCTE(x,y) as ( SELECT x = SUBSTRING(@InputString,0,PATINDEX('%,%',@InputString)), y = SUBSTRING(@InputString,PATINDEX('%,%',@InputString)+1,LEN(

Recursive CTE concatenate fields with parents from arbitrary point

為{幸葍}努か 提交于 2019-12-02 07:09:03
问题 How can I concatenate a list of parent items in a RECURSIVE CTE with PostgreSQL (version 10.2)? For example, I have: CREATE TABLE test ( id SERIAL UNIQUE, parent integer references test(id), text text NOT NULL ); with: INSERT INTO test(parent, text) VALUES (NULL, 'first'), (1, 'second'), (2, 'third'), (3, 'fourth'), (NULL, 'top'), (5, 'middle'), (6, 'bottom'); How do I get a tree with a particular item and all it's parents concatenated (or in an array) given it's id? So far I have the

Recursive SQL to split CSV to table rows

╄→гoц情女王★ 提交于 2019-12-02 06:41:29
After working on a different question here on SO, I stumbled across recursive CTEs which would on the surface seem a fairly easy way to solve the "Split a csv to table rows" problem. I put this example together DECLARE @InputString varchar(255) = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z' SELECT @InputString = @InputString + ',' ; with MyCTE(x,y) as ( SELECT x = SUBSTRING(@InputString,0,PATINDEX('%,%',@InputString)), y = SUBSTRING(@InputString,PATINDEX('%,%',@InputString)+1,LEN(@InputString)) UNION ALL SELECT x = SUBSTRING(y,0,PATINDEX('%,%',y)), y = SUBSTRING(y,PATINDEX('%,%',y)+1

Recursive CTE - Get descendants (many-to-many relationship)

為{幸葍}努か 提交于 2019-12-02 06:12:14
What I have: Given a tree (or more like a directed graph) that describes how a system is composed by its generic parts. For now let this system be e.g. the human body and the nodes its body parts. So for instance 3 could be the liver that has a left and a right lobe ( 6 and 9 ), in both of which there are veins ( 8 ) (that can also be found at any unspecified place of the liver, hence 8 -> 3 ) but also in the tongue ( 5 ). The lung ( 7 ) - which is in the chest ( 4 ) - also has a right lobe, and so on... (Well, of course there is no lung in the liver and also a 6 -> 7 would be reasonable so

Recursive CTE concatenate fields with parents from arbitrary point

我们两清 提交于 2019-12-02 05:46:52
How can I concatenate a list of parent items in a RECURSIVE CTE with PostgreSQL (version 10.2)? For example, I have: CREATE TABLE test ( id SERIAL UNIQUE, parent integer references test(id), text text NOT NULL ); with: INSERT INTO test(parent, text) VALUES (NULL, 'first'), (1, 'second'), (2, 'third'), (3, 'fourth'), (NULL, 'top'), (5, 'middle'), (6, 'bottom'); How do I get a tree with a particular item and all it's parents concatenated (or in an array) given it's id? So far I have the following query to see what is being returned, but I can't seem to add the right WHERE clause to return the

SQL query to get the list of supervisor hierarchy. employee --> supervisor --> supervisor

∥☆過路亽.° 提交于 2019-12-02 05:16:01
I have two tables Employee and Department this image shows the manager of every employee. I want to write a SQL query that gives me a list of all the supervisor (Manager, Manager of Manager..). I just want a single column that displays a list of supervisor when given a particular employee. E.g. If I give employee id = 202 then I should receive 200,130 |supervisor | +-----------+ | 200 | | 130 | I have this query WITH emp_dept as( SELECT employee_id,manager_id FROM employee,department WHERE employee.dept_id= department.dept_id ) WITH recursive p as ( select e1.employee_id, e1.manager_id from