common-table-expression

Cleaner SQL CTE than the current one

﹥>﹥吖頭↗ 提交于 2021-02-11 18:23:30
问题 Currently I use the following CTE to grab a category and any category below it: WITH RECURSIVE tree AS ( SELECT * FROM ( SELECT categoryId, categoryName, categoryParentId, categoryDescr, categoryType, categoryDC, categoryLedgerId, 1 as categoryDepth FROM tbl_categories WHERE categoryId = '.$categoryId.' UNION SELECT categoryId, categoryName, categoryParentId, categoryDescr, categoryType, categoryDC, categoryLedgerId, 1 as categoryDepth FROM tbl_categories_custom WHERE categoryId = '.

Getting depth-first traversal insted of breadth first in T-SQL

限于喜欢 提交于 2021-02-11 16:55:55
问题 I have the following T-SQL function: https://gist.github.com/cwattengard/11365802 This returns data in a breadth-first traversal. Is there a simple way to make this function return its data in a depth-first traversal? I have a treeview-component that excpects this (legacy system). I already have a similar stored procedure that returns the tree in a depth-first traversal, but it's using cursors and is really slow. (6-7 seconds as opposed to this function that takes less than a second on the

How to write an UPDATE Query for multiple tables in Common Table Expressions (CTE's) format?

守給你的承諾、 提交于 2021-02-11 16:52:42
问题 Ho do I rewrite the following UPDATE statement into a Common Table Expressions (CTE's) format? DB::statement('UPDATE taggables, threads SET taggables.created_at = threads.created_at, taggables.updated_at = threads.updated_at WHERE taggables.thread_id = threads.id'); 回答1: I don't really see the point for using a CTE here. Since it seems like your purpose is to update taggables from threads , I would suggest simply using Postgres' update/join syntax, which goes like: update taggables ta set

How to write an UPDATE Query for multiple tables in Common Table Expressions (CTE's) format?

℡╲_俬逩灬. 提交于 2021-02-11 16:52:18
问题 Ho do I rewrite the following UPDATE statement into a Common Table Expressions (CTE's) format? DB::statement('UPDATE taggables, threads SET taggables.created_at = threads.created_at, taggables.updated_at = threads.updated_at WHERE taggables.thread_id = threads.id'); 回答1: I don't really see the point for using a CTE here. Since it seems like your purpose is to update taggables from threads , I would suggest simply using Postgres' update/join syntax, which goes like: update taggables ta set

SQL Server get path with recursive CTE

纵饮孤独 提交于 2021-02-10 06:59:16
问题 I want to get the path for each department with this format 1.1, 1.2 and so on. This is my department table : id name parentId -------------------- 1 Dep 1 0 2 Dep 2 1 3 Dep 3 0 4 Dep 4 1 5 Dep 5 4 6 Dep 6 2 This is my recursive CTE that give me the parents and children in a flat table starting from a root department. WITH recursiveCte (parentId, id, name, Level) AS ( -- Anchor member definition SELECT d.parentId, d.id, d.name, 0 AS Level FROM Department AS d WHERE parentId = 0 UNION ALL --

postgreSQL Fibonacci Sequence - Query has no destination for result data

泄露秘密 提交于 2021-02-08 06:23:33
问题 So I wrote a Fibonacci sequence function like this: CREATE OR REPLACE FUNCTION fibonacci (lastN INTEGER) RETURNS int AS $$ BEGIN WITH RECURSIVE t(a, b) AS ( VALUES(0,1) UNION ALL SELECT GREATEST(a, b), a + b AS a from t WHERE b < $1 ) SELECT a FROM t; END; $$ LANGUAGE plpgsql; But when I called: SELECT * FROM fibonacci(20); the console shows: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function

postgreSQL Fibonacci Sequence - Query has no destination for result data

蹲街弑〆低调 提交于 2021-02-08 06:23:07
问题 So I wrote a Fibonacci sequence function like this: CREATE OR REPLACE FUNCTION fibonacci (lastN INTEGER) RETURNS int AS $$ BEGIN WITH RECURSIVE t(a, b) AS ( VALUES(0,1) UNION ALL SELECT GREATEST(a, b), a + b AS a from t WHERE b < $1 ) SELECT a FROM t; END; $$ LANGUAGE plpgsql; But when I called: SELECT * FROM fibonacci(20); the console shows: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function

Oracle CONNECT BY recursive child to parent query, include ultimate parent that self references

假装没事ソ 提交于 2021-02-07 19:59:18
问题 In the following example id parent_id A A B A C B select id, parent_id from table start with id = 'A' connect by nocycle parent_id = prior id I get A A B A C B In my database I have millions of rows in the table and deep and wide hierarchies and I'm not interested in all children. I can derive the children I'm interested in. So I want to turn the query on its head and supply START WITH with the children ids. I then want to output the parent recursively until I get to the top. In my case the

Oracle CONNECT BY recursive child to parent query, include ultimate parent that self references

六眼飞鱼酱① 提交于 2021-02-07 19:58:38
问题 In the following example id parent_id A A B A C B select id, parent_id from table start with id = 'A' connect by nocycle parent_id = prior id I get A A B A C B In my database I have millions of rows in the table and deep and wide hierarchies and I'm not interested in all children. I can derive the children I'm interested in. So I want to turn the query on its head and supply START WITH with the children ids. I then want to output the parent recursively until I get to the top. In my case the

SQL UPDATE and CASE statement does not work

情到浓时终转凉″ 提交于 2021-01-29 15:09:07
问题 I have the following table "Sales" in MS SQL Server, where [FieldX] is not updated properly: (..[My comment] is of course not an actual column in the table) The reason why the last 3 rows should be = 3, is because I am updating the fields with the following rules: with cte_previous_rows AS ( SELECT Staff_Id, LAG(FieldX) OVER (partition by Staff_Id ORDER by [date]) as Prev_Row FROM Sales ) UPDATE Sales SET FieldX = (CASE WHEN Staff_id_sales < 1500 AND ClosedSale = 0 THEN 0 WHEN Staff_id_sales