common-table-expression

Trying to refactor the recursive query in an Oracle CTE?

孤街浪徒 提交于 2020-01-15 03:54:12
问题 I have been working on a CTE which works pretty good for MS-SQL. I am translating it to Oracle and it works. However it is very slow and when I look at the explain plan I see "Merge Join Cartesian" when I run the query. I am running against Oracle 12.2. If I remove the "OR" (...) part of the query the speed is dramatically improved. However the "Merge Join Cartesian" still shows up in the explain plan. I have determined where the problem is in the query. The "and" (....) "or" (...) in the

Postgres' CTE vs Subquery Performance difference. Why?

放肆的年华 提交于 2020-01-15 03:08:47
问题 I have two equivalent queries which extracts the average distance between buildings (table a) and the nearest highway (highways in table v) in a specific district (ace) and city (pro_com). This is the CTE version WITH subq AS ( SELECT a.n, a.geom as g1, unnest(ARRAY(SELECT v.geom as g2 FROM atlas_sezioni2 as v where v.code = '12230' and a.pro_com = v.pro_com and a.code <> v.code ORDER BY a.geom <-> v.geom LIMIT 15)) as g2 FROM atlas_sezioni2 a where a.pro_com = 15146 and a.ace = 1 and a.code

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,

CTE very slow when Joined

拜拜、爱过 提交于 2020-01-10 18:43:09
问题 I have posted something similar before, but I am approaching this from a different direction now so I opened a new question. I hope this is OK. I have been working with a CTE that creates a sum of charges based on a Parent Charge. The SQL and details can be seen here: CTE Index recommendations on multiple keyed table I don't think I am missing anything on the CTE, but I am getting a problem when I use it with a big table of data (3.5 million rows). The table tblChargeShare contains some other

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.

Ordering hierarchy from recursive query results in SQL 2005

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-09 23:49:11
问题 I've got a 'Task' table with the following columns (the TaskOrder is for ordering the children within the scope of the parent, not the entire table): TaskId ParentTaskId TaskName TaskOrder I've got this CTE query to return all the rows: with tasks (TaskId, ParentTaskId, [Name]) as ( select parentTasks.TaskId, parentTasks.ParentTaskId, parentTasks.[Name] from Task parentTasks where ParentTaskId is null union all select childTasks.TaskId, childTasks.ParentTaskId, childTasks.[Name] from Task

Combine two SELECT queries in PostgreSQL

和自甴很熟 提交于 2020-01-09 19:49:19
问题 I would like to combine two select queries with UNION . How can I use the result from the first SELECT in the second SELECT ? (SELECT carto_id_key FROM table1 WHERE tag_id = 16) UNION (SELECT * FROM table2 WHERE carto_id_key = <the carto_id result from above> ) 回答1: Use a CTE to reuse the result from a subquery in more than one SELECT . You need PostgreSQL 8.4+ for that: WITH x AS (SELECT carto_id_key FROM table1 WHERE tag_id = 16) SELECT carto_id_key FROM x UNION ALL SELECT t2.some_other_id

Efficient way to string split using CTE

六月ゝ 毕业季﹏ 提交于 2020-01-09 10:25:19
问题 I have a table that looks like ID Layout 1 hello,world,welcome,to,tsql 2 welcome,to,stackoverflow The desired output should be Id Splitdata 1 hello 1 world 1 welcome 1 to 1 tsql 2 welcome 2 to 2 stackoverflow I have done this by the below query Declare @t TABLE( ID INT IDENTITY PRIMARY KEY, Layout VARCHAR(MAX) ) INSERT INTO @t(Layout) SELECT 'hello,world,welcome,to,tsql' union all SELECT 'welcome,to,stackoverflow' --SELECT * FROM @t ;With cte AS( select F1.id ,O.splitdata from ( select *,

Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)

旧街凉风 提交于 2020-01-08 08:50:19
问题 Hi For many days I have been working on this problem in MySQL, however I can not figure it out. Do any of you have suggestions? Basically, I have a category table with domains like: id , name (name of category), and parent (id of parent of the category). Example Data: 1 Fruit 0 2 Apple 1 3 pear 1 4 FujiApple 2 5 AusApple 2 6 SydneyAPPLE 5 .... There are many levels, possibly more than 3 levels. I want to create an sql query that groups the datas according to he hierarchy: parent > child >

How to join CTE query with another table in SQL Server 2008

丶灬走出姿态 提交于 2020-01-07 04:25:10
问题 I have created a CTE query, and I manage to join the CTE when the data in CTE is match with another table. For example, this is how my CTE query result looks like: ID NAME REG INV CUS BR ----------------------------------------------- 1 A0001 R0001 I0001 C0001 B0001 2 A0002 R0002 I0002 C0002 B0002 3 A0003 R0003 I0003 C0003 B0003 4 A0004 R0004 I0004 C0004 B0004 And this is the table I manage to join it to: ID NAME CUS --------------------- 1 TEST1 C0001 2 TEST2 C0002 3 TEST3 C0003 4 TEST4