recursive-query

Recursive SELECT query to return rates of arbitrary depth?

烈酒焚心 提交于 2019-12-11 08:14:01
问题 This is my first time attempting a recursive SQL query to traverse N parent-child relationships upward, and I don't know where to start. Any help would be appreciated. Scenario is that I have two tables - rate and rate_plan . Rates belong to a rate plan which is applied to a user. CREATE TERM rate_plan ( id integer PRIMARY KEY NOT NULL DEFAULT nextval('rate_plan_id'), descr varchar(64) NOT NULL, parent_rate_plan_id integer NOT NULL REFERENCES rate_plan(id) ); CREATE TABLE rate ( id integer

Recursive SQL statement (Postgresql) - simplified version

一笑奈何 提交于 2019-12-11 07:53:50
问题 This is simplified question for more complicated one posted here: Recursive SQL statement (PostgreSQL 9.1.4) Simplified question Given you have upper triangular matrix stored in 3 columns (RowIndex, ColumnIndex, MatrixValue): ColumnIndex 1 2 3 4 5 1 2 2 3 3 4 2 4 4 5 6 X 3 3 2 2 X X 4 2 1 X X X 5 1 X X X X X values are to be calculated using the following algorithm: M[i,j] = (M[i-1,j]+M[i,j-1])/2 (i= rows, j = columns, M=matrix) Example: M[3,4] = (M[2,4]+M[3,3])/2 M[3,5] = (m[2,5]+M[3,4])/2

CakePHP combining Recursive and Group

坚强是说给别人听的谎言 提交于 2019-12-11 07:26:26
问题 I have recursive set to Two on my find query, this is great and returns all the results i want, but i need to group by Day on the results that are Teo levels deep in the recursive query. This is what i have: Project->Track->Lapse . But i need to group by Day on the Track results, how can i do this? $project = $this->Project->find('all', array( 'conditions' => array( 'Project.id' => $id, 'Project.user_id' => $this->Auth->user('id') ), 'recursive' => 2 ) ); 回答1: IMHO, Cake's ORM tend to be very

mySQL transitive closure table

淺唱寂寞╮ 提交于 2019-12-11 07:26:12
问题 I have some code I've been using in SQL Server to generate a closure table from another table that has just the direct parent/child relationships, I can run very simple queries against this to determine lineage. Now I am needing to do all this in mySQL, but I am having trouble with the recursive querying to generate the closure table... My original SQL server query is WHILE @@ROWCOUNT>0 INSERT INTO [ClosureTable] ([Ancestor], [Descendent]) SELECT distinct [Parent],[tc].[Descendent] FROM

PostgreSQL - Select all the hierarchy of a table with levels

隐身守侯 提交于 2019-12-11 04:46:09
问题 I have a problem in this moment. I have a table called places with this structure: id parent_id name I want to do a selection to have all the hierarchy of this table. There's a little example of data: (1, null, '123 Barclay St') (2, 1, 'Floor 1') (3, 1, 'Floor 2') (4, 1, 'Floor 3') (5, 2, 'Hall 1') (6, 2, 'Room 1') (7, 2, 'Room 2') (8, 3, 'Room 3') (9, null, '10 Thames St') Obviously the order in the table is not this one. So I want to get this result with my SELECT (with the 9 rows): 123

How to do an upper recursive self-join in SQL Server?

佐手、 提交于 2019-12-11 03:15:26
问题 How can I do a recursive self-join in SQL Server ? I have a table like this: TableID | ParentID 1 | NULL 2 | 1 3 | 1 4 | 3 5 | NULL 6 | 4 7 | 6 I want to get the following results based on given TableID to get all the ParentsID related to the TableID , let's say I want to get all the parents for the TableID = 6 : TableID 6 4 3 1 I'm stuck on this and i don't know how to get the result in SQL Query ... Hope to tell me the SQL Query to get the previous data 回答1: It should be ; WITH MyQuery

Delete recursive children

徘徊边缘 提交于 2019-12-10 20:10:42
问题 I have the following sql that gets me all the children and grandchildren of a root forumpost. with recursive all_posts (id, parentid, root_id) as ( select t1.id, t1.parent_forum_post_id as parentid, t1.id as root_id from forumposts t1 union all select c1.id, c1.parent_forum_post_id as parentid, p.root_id from forumposts c1 join all_posts p on p.id = c1.parent_forum_post_id ) select fp.id from forumposts fp inner join all_posts ap on fp.id=ap.id where root_id=1349 group by fp.id Thing is I

SQL Server Hierarchical Sum of column

我是研究僧i 提交于 2019-12-10 17:28:45
问题 I have my database design as per the diagram. Category table is self referencing parent child relationship Budget will have all the categories and amount define for each category Expense table will have entries for categories for which the amount has been spend (consider Total column from this table). I want to write select statement that will retrieve dataset with columns given below : ID CategoryID CategoryName TotalAmount (Sum of Amount Column of all children hierarchy From BudgetTable )

How to use a recursive query as a subquery?

别等时光非礼了梦想. 提交于 2019-12-10 15:33:56
问题 I need to write a query that calls a recursive query many times. I was not able to figure out how to do. I guess I can do this by using a cursor, preparing the sql statement at run time and then use EXEC(mySQLstatement) to run it at every cursor FETCH NEXT. Anyway this is not the good approach. This is the problem (of course here it is simplified and I leave only the necessary columns to express myself): I have a tree of customers (a hierarchy) and for every customer there are some contacts

Recursive calculation to form a tree using sql

拟墨画扇 提交于 2019-12-10 13:06:56
问题 I am working on a simple problem and wanted to solve it using SQL. I am having 3 tables Category, Item & a relational table CategoryItem. I need to return count of items per category but the twist is Categories are arranged in Parent-Child relationships and the count of items in child categories should be added to the count in its parent Category. Please consider the sample data below and the expected resultset using SQL. Id Name ParentCategoryId 1 Category1 Null 2 Category1.1 1 3 Category2.1