recursive-query

Multiple CTE in single query

只谈情不闲聊 提交于 2019-11-26 22:13:10
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. 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: WITH RECURSIVE cte1 AS (...) -- can still be non-recursive , cte2 AS (SELECT ... UNION ALL SELECT ...) --

SQL Server recursive query

假如想象 提交于 2019-11-26 21:02:40
I am new to SQL Server development. Most of my experience has been done with Oracle. suppose I have the following table that contains Appointments objects CREATE TABLE [dbo].[Appointments]( [AppointmentID] [int] IDENTITY(1,1) NOT NULL, ....... [AppointmentDate] [datetime] NOT NULL, [PersonID] [int] NOT NULL, [PrevAppointmentID] [int] NULL, CONSTRAINT [PK_Appointments] PRIMARY KEY CLUSTERED ([AppointmentID] ASC) An appointment can be postponed so, when this happens, a new row is created on the table with the PrevAppointmentID field containing the ID of the original Appointment. I would like to

Create nested json object using php mysql

不问归期 提交于 2019-11-26 18:27:25
问题 I have two tables, table 1 has 2 fields (question_pk, question_name) and table 2 has 4 fields(ans_pk, options, question_fk and right_answer). I want to create json like the following structure { "type": "quiz", "name": "Brand Colors", "description": "Can you identify these brands by the background color?", "questions": [ { "name": "Can you identify this color?", "description": "#ea4c89", "answers": [ { "name": "Dribbble", "description": "dribbble.png", "weight": 1 }, { "name": "Amazon",

@ Symbol - a solution for Recursive SELECT query in Mysql?

半城伤御伤魂 提交于 2019-11-26 16:48:19
问题 there are a lot of questions about Recursive SELECT query in Mysql, but most of answers is that "There NO solution for Recursive SELECT query in Mysql". Actually there is a certain solution & I want to know it clearly, so this question is the following of the previous question that can be found at (how-to-do-the-recursive-select-query-in-mysql) Suppose you have this table: col1 - col2 - col3 1 - a - 5 5 - d - 3 3 - k - 7 6 - o - 2 2 - 0 - 8 & you want to find all the links that connect to

Is it possible to make a recursive SQL query?

荒凉一梦 提交于 2019-11-26 12:50:49
I have a table similar to this: CREATE TABLE example ( id integer primary key, name char(200), parentid integer, value integer); I can use the parentid field to arrange data into a tree structure. Now here's the bit I can't work out. Given a parentid, is it possible to write an SQL statement to add up all the value fields under that parentid and recurse down the branch of the tree ? UPDATE: I'm using posgreSQL so the fancy MS-SQL features are not available to me. In any case, I'd like this to be treated as a generic SQL question. BTW, I'm very impressed to have 6 answers within 15 minutes of

How does a Recursive CTE run, line by line?

社会主义新天地 提交于 2019-11-26 12:17:06
问题 I think I\'ve got the format of Recursive CTEs down well enough to write one, but still find myself frustrated to no end that I cannot manually process one (pretend to be the SQL engine myself and reach the result set with pen and paper). I\'ve found this, which is close to what I\'m looking for, but not detailed enough. I have no problem tracing through a C++ recursive function and understanding how it runs -- but for SQL I don\'t understand why or how the engine knows to stop. Does the

SQL Server recursive query

孤街醉人 提交于 2019-11-26 07:49:01
问题 I am new to SQL Server development. Most of my experience has been done with Oracle. suppose I have the following table that contains Appointments objects CREATE TABLE [dbo].[Appointments]( [AppointmentID] [int] IDENTITY(1,1) NOT NULL, ....... [AppointmentDate] [datetime] NOT NULL, [PersonID] [int] NOT NULL, [PrevAppointmentID] [int] NULL, CONSTRAINT [PK_Appointments] PRIMARY KEY CLUSTERED ([AppointmentID] ASC) An appointment can be postponed so, when this happens, a new row is created on the

Is it possible to make a recursive SQL query?

不打扰是莪最后的温柔 提交于 2019-11-26 03:07:40
问题 I have a table similar to this: CREATE TABLE example ( id integer primary key, name char(200), parentid integer, value integer); I can use the parentid field to arrange data into a tree structure. Now here\'s the bit I can\'t work out. Given a parentid, is it possible to write an SQL statement to add up all the value fields under that parentid and recurse down the branch of the tree ? UPDATE: I\'m using posgreSQL so the fancy MS-SQL features are not available to me. In any case, I\'d like

How to create a MySQL hierarchical recursive query

不羁的心 提交于 2019-11-25 23:55:35
问题 I have a MySQL table which is as follows: id | name | parent_id 19 | category1 | 0 20 | category2 | 19 21 | category3 | 20 22 | category4 | 21 ...... Now, I want to have a single MySQL query to which I simply supply the id [for instance say \'id = 19\'] then I should get all its child ids [i.e. result should have ids \'20,21,22\'].... Also, the hierarchy of the children is not known it can vary.... Also, I already have the solution using the for loop..... Let me know how to achieve the same

How to do the Recursive SELECT query in MySQL?

萝らか妹 提交于 2019-11-25 22:06:06
问题 I got a following table: col1 | col2 | col3 -----+------+------- 1 | a | 5 5 | d | 3 3 | k | 7 6 | o | 2 2 | 0 | 8 If a user searches for \"1\", the program will look at the col1 that has \"1\" then it will get a value in col3 \"5\", then the program will continue to search for \"5\" in col1 and it will get \"3\" in col3 , and so on. So it will print out: 1 | a | 5 5 | d | 3 3 | k | 7 If a user search for \"6\", it will print out: 6 | o | 2 2 | 0 | 8 How to build a SELECT query to do that?