common-table-expression

Unexpected results from CTE

偶尔善良 提交于 2019-12-21 21:00:03
问题 I've created a complex process that uses several CTEs (mainly for recursive hierarchical work). On small sample sets of data everything goes as expected but when I apply the code to large sets of data I received unexpected (and wrong) results. I think I've narrowed it done to the CTEs. The recursive CTEs are "fed" data processed in several earlier CTEs, and that seems to be the problem. I set up a sample data set as follows: Four rows with unique data Each row receives a random row number

How to find circular dependent table in sql server

血红的双手。 提交于 2019-12-21 17:32:32
问题 Currently im working on finding the dpendency order of tables in a database. And im stuck up with peoblem of circular depedency of some tables in database. since some tables are circularly depended im not getting the entire order..... Is there any way to find circular dependent tables in any database in sql server, Other than the database diagrams?? 回答1: You don't really need to buy a tool to find these references. SELECT OBJECT_SCHEMA_NAME(fk1.parent_object_id) + '.' + OBJECT_NAME(fk1.parent

TSQL - Recursive CTE inefficient - Need an alternative

余生颓废 提交于 2019-12-21 15:07:30
问题 Here is a table with sample data: DECLARE @TestTable TABLE ( ItemID INT, A INT, B INT, Month INT) INSERT INTO @TestTable VALUES (1234, 5, 9, 1) INSERT INTO @TestTable VALUES (1234, 6, 9, 2) INSERT INTO @TestTable VALUES (4321, 5, 11, 1) INSERT INTO @TestTable VALUES (4321, 12, 11, 2) INSERT INTO @TestTable VALUES (1324, 14, 6, 1) INSERT INTO @TestTable VALUES (1324, 5, 6, 2) INSERT INTO @TestTable VALUES (1234, 1, 9, 3) INSERT INTO @TestTable VALUES (1324, 9, 6, 3) Something to note is that

Aggregating connected sets of nodes / edges

断了今生、忘了曾经 提交于 2019-12-21 05:05:08
问题 I have a connected set of edges with unique nodes. They are connected using a parent node. Consider the following example code and illustration: CREATE TABLE network ( node integer PRIMARY KEY, parent integer REFERENCES network(node), length numeric NOT NULL ); CREATE INDEX ON network (parent); INSERT INTO network (node, parent, length) VALUES (1, NULL, 1.3), (2, 1, 1.2), (3, 2, 0.9), (4, 3, 1.4), (5, 4, 1.6), (6, 2, 1.5), (7, NULL, 1.0); Visually, two groups of edges can be identified. How

Bizarre performance issue: Common Table Expressions in inline User-Defined Function

亡梦爱人 提交于 2019-12-20 10:34:53
问题 Here's a brain-twister for the SQL guys - can anyone think of a reason why the first of these functions performs fine, and the second one runs dog-slow? Function A - Typically finishes in ~5 ms CREATE FUNCTION dbo.GoodFunction ( @IDs UniqueIntTable READONLY ) RETURNS TABLE AS RETURN SELECT p.ID, p.Node, p.Name, p.Level FROM ( SELECT DISTINCT a.Ancestor AS Node FROM Hierarchy h CROSS APPLY dbo.GetAncestors(h.Node.GetAncestor(1)) a WHERE h.ID IN (SELECT Value FROM @IDs) ) np INNER JOIN

Is it possible to have multiple secondary use of a returning value in a CTE statement in Postgres?

有些话、适合烂在心里 提交于 2019-12-20 06:28:06
问题 I want to insert my foreign keys in multiple tables after a insert into the main table in one CTE. I can't find the solution so it may well be impossible... see this example: CREATE TABLE test_main (main_id serial NOT NULL, main_name character varying(64) default null); CREATE TABLE test_sub_one (sub_one_id serial NOT NULL,sub_one_main_id integer NOT NULL,sub_one_name character varying(64) default null); CREATE TABLE test_sub_two (sub_two_id serial NOT NULL,sub_two_main_id integer NOT NULL

Convert recursive function to view

冷暖自知 提交于 2019-12-19 17:46:55
问题 I'm trying to convert a function in Postgres into a select query which I intend to use as a view. The reason is that I'd like to access it from a client via a select query with a where clause instead of using a parameter as with the function. The table represents a tree (and adjacency list) and is defined as follow: CREATE TABLE tree ( id serial primary key, parent_id int references tree(id) ); INSERT INTO tree (id, parent_id) VALUES (1,null) , (2,1), (3,2), (4,3), (5,3) , (6,5), (7,6), (8,4)

Convert recursive function to view

与世无争的帅哥 提交于 2019-12-19 17:46:22
问题 I'm trying to convert a function in Postgres into a select query which I intend to use as a view. The reason is that I'd like to access it from a client via a select query with a where clause instead of using a parameter as with the function. The table represents a tree (and adjacency list) and is defined as follow: CREATE TABLE tree ( id serial primary key, parent_id int references tree(id) ); INSERT INTO tree (id, parent_id) VALUES (1,null) , (2,1), (3,2), (4,3), (5,3) , (6,5), (7,6), (8,4)

avg sale of quarter with previous quarter avg sale

这一生的挚爱 提交于 2019-12-19 11:42:41
问题 I have a table one in which there are various attribute like region product,year,qtr,month,sale. I have to calculate the avg_qtr sale of each product having same region and show their previous avg_qtr sale.I have read about lag but here it is not possible to use as it is not fixed after how many rows it will be repeated. My table structure is like this Region Product Year Qtr Month Sales NORTH P1 2015 1 JAN 1000 NORTH P1 2015 1 FEB 2000 NORTH P1 2015 1 MAR 3000 NORTH P1 2015 2 APR 4000 NORTH

avg sale of quarter with previous quarter avg sale

微笑、不失礼 提交于 2019-12-19 11:42:11
问题 I have a table one in which there are various attribute like region product,year,qtr,month,sale. I have to calculate the avg_qtr sale of each product having same region and show their previous avg_qtr sale.I have read about lag but here it is not possible to use as it is not fixed after how many rows it will be repeated. My table structure is like this Region Product Year Qtr Month Sales NORTH P1 2015 1 JAN 1000 NORTH P1 2015 1 FEB 2000 NORTH P1 2015 1 MAR 3000 NORTH P1 2015 2 APR 4000 NORTH