recursive-query

SQL Server : BOM Recursion from the bottom up

孤街浪徒 提交于 2020-01-16 02:50:10
问题 I am attempting to sum up the BOM costs from the bottom up. I need to be able to determine at a particular level of the BOM what the costs are for that level as all costs roll up from the levels below. In the below example Job 1000 costs should be the sum of all costs for all jobs below as well as the cost for Job 1000. 1000-1 should be the sum of 1000-1 + 1000-1A, 1000-2 would only contain the costs for 1000-2 as there are no components tied to that Job, etc... (NOTE: Job Numbers are random

SQL Server : BOM Recursion from the bottom up

若如初见. 提交于 2020-01-16 02:50:08
问题 I am attempting to sum up the BOM costs from the bottom up. I need to be able to determine at a particular level of the BOM what the costs are for that level as all costs roll up from the levels below. In the below example Job 1000 costs should be the sum of all costs for all jobs below as well as the cost for Job 1000. 1000-1 should be the sum of 1000-1 + 1000-1A, 1000-2 would only contain the costs for 1000-2 as there are no components tied to that Job, etc... (NOTE: Job Numbers are random

Drop rows identified within moving time window

和自甴很熟 提交于 2020-01-16 00:36:14
问题 I have a dataset of hospitalisations ('spells') - 1 row per spell. I want to drop any spells recorded within a week after another (there could be multiple) - the rationale being is that they're likely symptomatic of the same underlying cause. Here is some play data: create table hif_user.rzb_recurse_src ( patid integer not null, eventdate integer not null, type smallint not null ); insert into hif_user.rzb_recurse_src values (1,1,1); insert into hif_user.rzb_recurse_src values (1,3,2); insert

generate JSON for the parent child rows. parent child depth is dynamic

↘锁芯ラ 提交于 2020-01-15 11:07:06
问题 I want to generate a JSON which represent parent child hierarchy. i want to minimize looping on server side to get below expected JSON structure. is it possible to get data from a sql statement, which i can use in server side (with minimum loop) to generate this JSON. i have to support Oracle, DB2, SYBASE,SQL Server etc. below is my table structure and respective sample data CREATE TABLE TABLEA ( SCEID VARCHAR(10), Group_Step VARCHAR(100) ); CREATE TABLE TABLEB ( SCEID VARCHAR(10), Group_Step

Find whether graph has a cycle

為{幸葍}努か 提交于 2020-01-15 05:48:25
问题 I want to find out whether it is possible to find cycles in Hierarchical or Chain data with SQL. E.g. I have following schema: http://sqlfiddle.com/#!3/27269 create table node ( id INTEGER ); create table edges ( id INTEGER, node_a INTEGER, node_b INTEGER ); create table graph ( id INTEGER, edge_id INTEGER); INSERT INTO node VALUES (1) , (2), (3), (4); INSERT INTO edges VALUES (1, 1, 2), (2, 2, 3) , (3, 3, 4) , (4, 4, 1); -- first graph [id = 1] with cycle (1 -> 2 -> 3 -> 4 -> 1) INSERT INTO

postgresql procedure for finding hie parent chain

白昼怎懂夜的黑 提交于 2020-01-11 13:14:11
问题 I am new to postgresql and need a little help.I have a table named products ID Product Parent_ID 1 laptop Null 2 Camera 1 3 Iphone 1 4 Mouse 2 5 Printer 2 6 Scanner 3 7 HardDisk 3 I want to create a function in postgres to get the hierarchy of the parent chain of any value i pass like if i pass 4 then my output should be id parent_id 1 Null 2 1 4 2 回答1: I would suggest to use "recursive with" clause. Kindly check below query. WITH RECURSIVE recursiveTable AS ( SELECT id, parent_id FROM table

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

删除回忆录丶 提交于 2020-01-11 11:21:52
问题 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,

Invalid operation: WITH RECURSIVE is not supported

流过昼夜 提交于 2020-01-10 05:44:06
问题 When I'm running query below I get message: [Amazon](500310) Invalid operation: WITH RECURSIVE is not supported; Can someone explain me why recursive function doesn't work? (I'm working on amazon redshift) WITH RECURSIVE r AS ( SELECT 1 AS i, 1 AS factorial UNION SELECT i+1 AS i, factorial * (i+1) as factorial FROM r WHERE i < 10 ) SELECT * FROM r; 回答1: The official Amazon Redshift documentation: Unsupported PostgreSQL Features: These PostgreSQL features are not supported in Amazon Redshift.

How to preserve order of children to appear after their parents

不打扰是莪最后的温柔 提交于 2020-01-07 03:16:08
问题 Expected order by replyid: 55, 57, 58, 59, 60, 56 -- So that the entire 1st parent reply and all its children appear BEFORE the 2nd parent reply The following SQL query returns the wrong order of results WITH RECURSIVE t(replyid, replypid, depth, path, reply, replied, reply_userid) AS ( (SELECT replyid, replypid, 0, array[replyid], reply, replied, replies.userid, u.displayname, u.email_address, (SELECT COUNT(*) FROM reply_revs WHERE replyid = replies.replyid) AS reply_revs FROM replies LEFT

How to write involved recursive subquery in sqlplus w/ multiple tables to trace nodes?

瘦欲@ 提交于 2020-01-06 06:35:24
问题 I am trying to create a report that maps loads on a circuit to their respective power source (Transformer). All of the devices are in separate tables and interconnected in the database: Loads, Switches, Lines, Wires, Transformers. For simplicity's sake, I will show an example where a load is connected to a transformer directly through switches only: Load table: LoadNumber, SectionNumber, BusNumber 100 54 3000 Switch table: SwitchNumber, FromSectionNumber, ToSectionNumber, State, BusNumber 1