recursive-query

Recursive Teradata Query

China☆狼群 提交于 2019-12-13 05:49:28
问题 I'm trying to query the below table into a consolidated and sorted list, such as: Beginning list: GROUP_ID MY_RANK EMP_NAME 1 1 Dan 1 2 Bob 1 4 Chris 1 3 Steve 1 5 Cal 2 1 Britt 2 2 Babs 2 3 Beth 3 1 Vlad 3 3 Eric 3 2 Mike Query Result: 1 Dan, Bob, Steve, Chris, Cal 2 Britt, Babs, Beth 3 Vlad, Mike, Eric It needs to use a recursive query because the list is much longer. Also, I have to sort by my_rank to get them in sequential order. Thanks in advance. I've tried about 10 examples found on

CTE RECURSIVE optimization, how to?

百般思念 提交于 2019-12-13 04:19:01
问题 I need to optimize the performance of a commom WITH RECURSIVE query... We can limit the depth of the tree and decompose in many updates, and can also change representation (use array)... I try some options but perhaps there are a "classic optimization solution" that I'm not realizing. All details There are a t_up table, to be updated, with a composit primary key (pk1,pk2), one attribute attr and an array of references to primary keys... And a unnested representation t_scan , with the

Recursive query in Oracle to find children and sibling

柔情痞子 提交于 2019-12-13 03:40:02
问题 I'm struggling a hierarchical SQL query. I want to have another 2 columns of the disp_order of its children and sibling. Children - Should hold all disp_order of their child and their grand children and so far. Sibling - Should hold the disp_order of the row having the same parent. +------------+-----+-------------+--------+ | disp_order | lvl | description | parent | +------------+-----+-------------+--------+ | 0 | 1 | A | | | 1 | 2 | B | 0 | | 2 | 3 | C | 1 | | 3 | 4 | D | 2 | | 4 | 5 | E

Aggregate periods using recursive queries

社会主义新天地 提交于 2019-12-13 03:29:56
问题 I need to merge overlapping periods (defined by FROM and TO variables) of sequential events (with identifier NUM) for each group (ID) with a "lookahead buffer", meaning that if the next period starts within the buffer zone, they should be merged. For instance; in the following example the second event (NUM = 2) starts at time 13, which is within the buffer zone (10 + 5 = 15). The tricky part here compared to other similar problems I've found is that although the buffer period has a fixed

Migrate query with START WITH and CONNECT BY PRIOR from oracle to postgresql

China☆狼群 提交于 2019-12-13 02:56:41
问题 I am migrating a process from oracle to postgresql, and I am in another problem with the conversion of them. I have been researching how to migrate an oracle query, which has "START WITH" and "CONNECT BY PRIOR", I have documented with respect to this, and I think the easiest way to do it is with "WITH RECURSIVE" Make the migration of the query, but I'm not sure about the results they throw since the bd oracle and postgres are different, and it is not possible to homologate the bd. This is the

Oracle SQL creating different levels of data from a single table

谁说我不能喝 提交于 2019-12-13 01:00:32
问题 I'm trying to create a new column in a SELECT statement that picks out top level lines from withing the same table. SAMPLE DATA: ITEM_VALUE DESCRIPTION LEVEL_NO ITEM_ABOVE 100 Ford 3 CAR 200 Honda Own 3 CAR 210 Honda 3rd Party 3 CAR 1000 Ford 4 100 2000 Honda T Own 4 200 801 Ford 1 4 1000 802 Ford 2 4 1000 803 Ford 3 4 1000 804 Ford 4 4 1000 805 Ford 5 4 1000 806 Ford 6 4 1000 807 Ford 7 4 1000 808 Ford 8 4 1000 814 Ford 4 1000 809 Honda 4 2000 2100 Honda T 3rd Party 4 210 EXPECTED OUTPUT:

Trying to obtain the accurate information (CTE - recursive)

对着背影说爱祢 提交于 2019-12-12 18:15:11
问题 I have different tables and the goal is to obtain the approval workflow for every customer, displaying that information in this way: > CLIENT | APPROVER1 | APPROVER2 | APPROVER3 | APPROVER4 First of all, i have a table called entities (12, 'Math Andrew', 308, 'CHAIN1-MathAndrew') (13, 'John Connor', 308, 'CHAIN2-JohnConnor') (18, 'ZATCH', 309, null), (19, 'MAX', 309, null), (20, 'Ger',310, null), (21, 'Mar',310, null), (22, 'Maxwell',311, null), (23, 'Ryan',312, null), (24, 'Juy',313, null),

How can I sum up data in tree-like structure in SQL from children to parent?

寵の児 提交于 2019-12-12 18:13:27
问题 I have a query for selecting amounts per department in a tree-like structure. I want to display the sum amount of the children on their respective parent. Is it possible to archive this in a query without using a cursor? Below is a resultset of the data to sum up. A full sample can also be found on sqlfiddle. Results : | DEPARTMENT_ID | PARENT_DEP_ID | DEPARTMENT | AMOUNT | |---------------|---------------|----------------|-----------------| | 1 | 0 | 1 | 0 | | 7 | 1 | 11 | 0 | | 34 | 7 | 111

Generate range of dates using CTE Oracle

孤街醉人 提交于 2019-12-12 15:08:30
问题 I want to generate a range of days between two different dates using recursive WITH clause in Oracle. WITH CTE_Dates (cte_date) AS ( SELECT CAST(TO_DATE('10-02-2017', 'DD-MM-YYYY') AS DATE) cte_date FROM dual UNION ALL SELECT CAST( (cte_date + 1) AS DATE) cte_date FROM CTE_Dates WHERE TRUNC(cte_date) + 1 <= TO_DATE('20-02-2017', 'DD-MM-YYYY') ) SELECT * FROM CTE_Dates The returned results are completely other than expected: 10-02-2017 09-02-2017 08-02-2017 07-02-2017 06-02-2017 ... (unlimited

Preserve the order of distinct inside string_agg

一世执手 提交于 2019-12-12 13:19:34
问题 My SQL function: with recursive locpais as ( select l.id, l.nome, l.tipo tid, lp.pai from loc l left join locpai lp on lp.loc = l.id where l.id = 12554 union select l.id, l.nome, l.tipo tid, lp.pai from loc l left join locpai lp on lp.loc = l.id join locpais p on (l.id = p.pai) ) select * from locpais gives me 12554 | PARNA Pico da Neblina | 9 | 1564 12554 | PARNA Pico da Neblina | 9 | 1547 1547 | São Gabriel da Cachoeira | 8 | 1400 1564 | Santa Isabel do Rio Negro | 8 | 1400 1400 | RIO NEGRO