recursive-cte

Using recursive CTE with Ecto

南笙酒味 提交于 2019-12-04 19:13:41
问题 How would I go about using the result of a recursive CTE in a query I plan to run with Ecto? For example let's say I have a table, nodes, structured as so: -- nodes table example -- id parent_id 1 NULL 2 1 3 1 4 1 5 2 6 2 7 3 8 5 and I also have another table nodes_users structured as so: -- nodes_users table example -- node_id user_id 1 1 2 2 3 3 5 4 Now, I want to grab all the users with a node at or above a specific node, for the sake of an example let's choose the node w/ the id 8. I

Using recursive CTE with Ecto

一笑奈何 提交于 2019-12-03 12:40:26
How would I go about using the result of a recursive CTE in a query I plan to run with Ecto? For example let's say I have a table, nodes, structured as so: -- nodes table example -- id parent_id 1 NULL 2 1 3 1 4 1 5 2 6 2 7 3 8 5 and I also have another table nodes_users structured as so: -- nodes_users table example -- node_id user_id 1 1 2 2 3 3 5 4 Now, I want to grab all the users with a node at or above a specific node, for the sake of an example let's choose the node w/ the id 8. I could use the following recursive postgresql query to do so: WITH RECURSIVE nodes_tree AS ( SELECT * FROM

Issue with recursive CTE in PostgreSQL

谁说胖子不能爱 提交于 2019-12-01 21:59:11
问题 This query generates the numbers from 1 to 4. with recursive z(q) as ( select 1 union all select q + 1 from z where q < 4 ) select * from z; But, if I modify it to this, with x as ( select 1 y ), recursive z(q) as ( select y from x union all select q + 1 from z where q < 4 ) select * from z; It gives ERROR: syntax error at or near "z" What did i do wrong here? 回答1: I think this is because RECURSIVE is modifier of WITH statement, not a property of common table expression z , so you can use it

Does PostgreSQL have a pseudo-column like “LEVEL” in Oracle?

佐手、 提交于 2019-12-01 15:47:13
Does PostgreSQL have a pseudo-column like "LEVEL" in Oracle? If not, then how can we create a column similar to "LEVEL"? Erwin Brandstetter Postgres does not have hierarchical queries . No CONNECT BY , therefore also no LEVEL . The additional module tablefunc provides the function connectby() doing almost the same. See: What is the equivalent PostgreSQL syntax to Oracle's CONNECT BY ... START WITH? Or you can do similar things with a standard recursive CTE and a level column that's incremented with every recursion. This query in Oracle: SELECT employee_id, last_name, manager_id, LEVEL FROM

To find infinite recursive loop in CTE

泪湿孤枕 提交于 2019-12-01 06:35:54
I'm not a SQL expert, but if anybody can help me. I use a recursive CTE to get the values as below. Child1 --> Parent 1 Parent1 --> Parent 2 Parent2 --> NULL If data population has gone wrong, then I'll have something like below, because of which CTE may go to infinite recursive loop and gives max recursive error. Since the data is huge, I cannot check this bad data manually. Please let me know if there is a way to find it out. Child1 --> Parent 1 Parent1 --> Child1 or Child1 --> Parent 1 Parent1 --> Parent2 Parent2 --> Child1 You haven't specified the dialect or your column names, so it is

Get all parents for a child

北城以北 提交于 2019-11-30 06:06:19
I want to retrieve the parentid of an id, if that parentid has a parent again retrieve it, and so on. Kind of hierarchy table. id----parentid 1-----1 5-----1 47894--5 47897--47894 am new to sql server and tried, some queries like: with name_tree as ( select id, parentid from Users where id = 47897 -- this is the starting point you want in your recursion union all select c.id, c.parentid from users c join name_tree p on p.id = c.parentid -- this is the recursion ) select * from name_tree; It is giving me only one row. and also I want to insert these records into a temporary table variable. How

Get all parents for a child

强颜欢笑 提交于 2019-11-29 05:50:02
问题 I want to retrieve the parentid of an id, if that parentid has a parent again retrieve it, and so on. Kind of hierarchy table. id----parentid 1-----1 5-----1 47894--5 47897--47894 am new to sql server and tried, some queries like: with name_tree as ( select id, parentid from Users where id = 47897 -- this is the starting point you want in your recursion union all select c.id, c.parentid from users c join name_tree p on p.id = c.parentid -- this is the recursion ) select * from name_tree; It

PostgreSQL pass data from recursive CTE onto function

孤街浪徒 提交于 2019-11-28 00:29:22
I have the following problem: I am trying to discover all possible paths from source node ( node_s ) to target node ( node_t ). The format of the original table with graph edges is simple: | node_x | node_y | strength | , where "node_x" -> "node_y" is a direct edge with strength of the edge being "weight". The idea is if at any point during the exploration of the paths we discover that a node among its children has target node_t , we record this path and stop exploring paths from this node, otherwise continue exploration. The simple solution was to use PostgreSQL's recursive CTE which

To find infinite recursive loop in CTE

穿精又带淫゛_ 提交于 2019-11-27 22:37:58
问题 I'm not a SQL expert, but if anybody can help me. I use a recursive CTE to get the values as below. Child1 --> Parent 1 Parent1 --> Parent 2 Parent2 --> NULL If data population has gone wrong, then I'll have something like below, because of which CTE may go to infinite recursive loop and gives max recursive error. Since the data is huge, I cannot check this bad data manually. Please let me know if there is a way to find it out. Child1 --> Parent 1 Parent1 --> Child1 or Child1 --> Parent 1

Select statement to return parent and infinite children

别等时光非礼了梦想. 提交于 2019-11-27 09:21:48
Give the table structure, as something like: ID ParentID Name 1 NULL A root 2 NULL Another root 3 1 Child of 1 4 3 Grandchild of 1 5 4 Great grandchild of 1 6 1 Child of 1 7 NULL Another root 8 7 Child of 6 I am looking for an elegant (if possible) solution for a single Sql statement/function that would return all data in the table when given an ID = 1 So my result would look something like: ID ParentID Name 1 NULL A root 3 1 Child of 1 4 3 Grandchild of 1 5 4 Great grandchild of 1 6 1 Child of 1 I've seen similar questions on SO though for the most part they only seem to be looking at a given