recursive-query

How to get Running Total based on start date and end date in MYSQL

拈花ヽ惹草 提交于 2020-05-15 21:46:11
问题 I have sample data like the below . Start_Dt End_Dt Dur_of_months amount 2020-01-01 2020-04-01 4 800 where I have Start date and End Date along Duration of months. By dividing amount (800) by 4 = 200. I want to get running total minus (200) for each month . output : mon_dt amount Jan 2020 800 Feb 2020 600 Mar 2020 400 Apr 2020 200 I have some code to increment the months between start date and End date SELECT ID, DATE_FORMAT(Startdate + INTERVAL n.n MONTH, '%M %Y') AS Dates FROM dates JOIN (

Is branch pruning possible for recursive cte query

 ̄綄美尐妖づ 提交于 2020-04-30 06:38:26
问题 This is inspired by question Retrieve a list of lists in one SQL statement - I have come up with a solution, but I have doubts on its efficiency. To restate the problem: we have 2 Tables: Person and Parent Person contains basic data about each person Parent is a join table relating person with its parents each Person can have multiple parents we want to receive each person data with list of all their ancestors - each ancestor in its own row if there are no ancestors, we have only one row for

Recursive SQL in Oracle to allocate data only once per row

生来就可爱ヽ(ⅴ<●) 提交于 2020-04-17 20:08:11
问题 I have the below data in Oracle ITEM_CNT ID 0 1 1 1 2 1 3 1 0 2 2 2 3 2 0 3 1 3 2 3 3 3 4 3 and I want the data in the below format. Note since ITEM_CNT 0 is taken by ID 1 hence ID 2 should take the next available number which is 2.Similarly ITEM_CNT 1 is taken by ID 3 and so on. Note if an item_cnt is already taken by an ID it cannot be used by another ID. Also always choose the least ITEM_CNT available. ITEM_CNT ID 0 1 2 2 1 3 Also, note I tried the below but for each additional row I have

The maximum recursion 100 has been exhausted

余生长醉 提交于 2020-04-17 19:09:56
问题 When I run this query I am getting error when I increase the date to be above months with calender_cte as ( select convert(date, '2019-01-01') as startdate, convert(date, '2019-12-31') as enddate union all select dateadd(day, 1, startdate), enddate from calender_cte cc where startdate < enddate ) SELECT DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf', DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to' --ISNULL(DATEPART(wk, Inter.StartDate), 0) as 'WeekNumber' FROM

The maximum recursion 100 has been exhausted

只愿长相守 提交于 2020-04-17 19:03:34
问题 When I run this query I am getting error when I increase the date to be above months with calender_cte as ( select convert(date, '2019-01-01') as startdate, convert(date, '2019-12-31') as enddate union all select dateadd(day, 1, startdate), enddate from calender_cte cc where startdate < enddate ) SELECT DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf', DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to' --ISNULL(DATEPART(wk, Inter.StartDate), 0) as 'WeekNumber' FROM

The maximum recursion 100 has been exhausted

走远了吗. 提交于 2020-04-17 19:03:29
问题 When I run this query I am getting error when I increase the date to be above months with calender_cte as ( select convert(date, '2019-01-01') as startdate, convert(date, '2019-12-31') as enddate union all select dateadd(day, 1, startdate), enddate from calender_cte cc where startdate < enddate ) SELECT DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf', DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to' --ISNULL(DATEPART(wk, Inter.StartDate), 0) as 'WeekNumber' FROM

How to select using WITH RECURSIVE clause [closed]

我是研究僧i 提交于 2020-03-17 03:53:07
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . I have googled and read throug some articles like this postgreSQL manual page or this blog page and tried making queries myself with a moderate success (part of them hangs, while others works good and fast), but so far I can not completely understand how this magic works. Can

Write element's ancestry to Postgres table from adjacency list

六月ゝ 毕业季﹏ 提交于 2020-02-24 11:38:29
问题 I want to write a 1 to n-hierarchy that's stored as an adjacency list to a table that lists each of an element's ancestors. I'm using a Postgres database (Postgres 10, but the machine on which the code is to be deployed runs Postgres 9.x). Sample input table (adjacency list): INSERT INTO public.test (id, name, parent_id) VALUES (1, 't', 1), (11, 't1', 1), (12, 't2', 1), (13, 't3', 1), (111, 't11', 11), (112, 't12', 11), (121, 't21', 12), (14, 't4', 1), (141, 't41', 14), (142, 't42', 14) As a

Recursive CTE Concept Confusion

倖福魔咒の 提交于 2020-02-16 05:18:40
问题 I am trying to understand the concepts of using CTE in my SQL code. I have gone through a number of online posts explaining the concept but I cannot grasp how it iterates to present the hierarchical data. One of the widely used examples to explain the R-CTE is the Employee and ManagerID Example as below: USE AdventureWorks GO WITH Emp_CTE AS ( SELECT EmployeeID, ContactID, LoginID, ManagerID, Title, BirthDate FROM HumanResources.Employee WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID,

Recursive CTE Concept Confusion

别说谁变了你拦得住时间么 提交于 2020-02-16 05:18:29
问题 I am trying to understand the concepts of using CTE in my SQL code. I have gone through a number of online posts explaining the concept but I cannot grasp how it iterates to present the hierarchical data. One of the widely used examples to explain the R-CTE is the Employee and ManagerID Example as below: USE AdventureWorks GO WITH Emp_CTE AS ( SELECT EmployeeID, ContactID, LoginID, ManagerID, Title, BirthDate FROM HumanResources.Employee WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID,