common-table-expression

Sql Server CTE Parent Child recursive

别说谁变了你拦得住时间么 提交于 2019-12-31 04:00:45
问题 I have following table structure: create table Test( ParentId int, ChildId int ) insert into Test(ParentId, ChildId) select 1, NULL union select 1, 2 union select 1, 3 union select 2, NULL union select 2, 5 union select 5, 6 union select 6, 5 union select 6, 8 I'm trying to build a result set of all parent child DIRECT and INDIRECT relationships . So suppose I pass the parameter of ParentID = 2, I would like the result set to return exactly as below: ParentId ChildId ------------------- 2

SQL - Use a reference of a CTE to another CTE

五迷三道 提交于 2019-12-30 07:11:08
问题 Is it possible in SQL use a reference inside a Common Table Expression inside another C.T.E in the same query? Here there is an example: WITH CT1 AS (SELECT * FROM T), CT2 AS (SELECT * FROM CT1) SELECT * FROM CT2; I tried this in SQLite3 and it works, I just wanted to know if it's part of standard SQL. Any advices concerning this argument will be highly appreciated. Thank you very much! 回答1: Here are three important properties of CTEs: You can refer to a CTE in subsequent CTEs or in the main

CTE and FOR XML to generate nested XML

谁说胖子不能爱 提交于 2019-12-29 05:21:34
问题 I have an adjacency list in the DB and want to deliver the data in XML format to the client through a SQL SP. I'm trying to use CTE and FOR XML but I am not getting the XML nodes to nest. FYI, this will represent a site map. The Table structure: CREATE TABLE [dbo].[PageHierarchy]( [ModuleId] [int] NOT NULL, [PageId] [int] IDENTITY(1,1) NOT NULL, [ParentPageId] [int] NULL, [PageUrl] [nvarchar](100) NULL, [PageTitle] [nvarchar](50) NOT NULL, [PageOrder] [int] NULL) and the beginnings of the CTE

Visiting a directed graph as if it were an undirected one, using a recursive query

半世苍凉 提交于 2019-12-29 04:25:10
问题 I need your help about the visit of a directed graph stored in a database. Consider the following directed graph 1->2 2->1,3 3->1 A table stores those relations: create database test; \c test; create table ownership ( parent bigint, child bigint, primary key (parent, child) ); insert into ownership (parent, child) values (1, 2); insert into ownership (parent, child) values (2, 1); insert into ownership (parent, child) values (2, 3); insert into ownership (parent, child) values (3, 1); I'd

SQL 2005 CTE vs TEMP table Performance when used in joins of other tables

[亡魂溺海] 提交于 2019-12-28 13:47:11
问题 I have a complex query that I need to use in a subsequent query (actually update statement). I have tried both using a CTE and a temp table. The performance using the CTE is horrible vs the temp table approach. Its something like 15 seconds vs milliseconds. To simplify the test instead of joining the CTE/Temp table in the subsequent query I simply selected * from it. In that case they perform the same. I Have Looked at The Execution Plan for both approaches both with the joins in the

Adding an INDEX to a CTE

北城余情 提交于 2019-12-28 11:43:02
问题 Should be a pretty straight forward question. Can I add an INDEX to a Common Table Expression (CTE)? 回答1: No. A CTE is a temporary, "inline" view - you cannot add an index to such a construct. If you need an index, create a regular view with the SELECT of your CTE, and make it an indexed view (by adding a clustered index to the view). You'll need to obey a set of rules outlined here: Creating an Indexed View. 回答2: I have had the same requirement. Indexes can not be added to a CTE. However, in

Multiple CTE in single query

隐身守侯 提交于 2019-12-27 17:30:29
问题 Is it possible to combine multiple CTEs in single query with arel ? I am looking for way to get result like this: WITH 'cte1' AS ( ... ), WITH RECURSIVE 'cte2' AS ( ... ), WITH 'cte3' AS ( ... ) SELECT ... FROM 'cte3' WHERE ... As you can see, I have one recursive CTE and two non recursive. 回答1: Use the key word WITH once at the top. If any of your Common Table Expressions (CTE) are recursive (rCTE) you have to add the keyword RECURSIVE at the top once also, even if not all CTEs are recursive

CTE Recursion to get tree hierarchy

☆樱花仙子☆ 提交于 2019-12-27 10:34:23
问题 I need to get an ordered hierarchy of a tree, in a specific way. The table in question looks a bit like this (all ID fields are uniqueidentifiers, I've simplified the data for sake of example): EstimateItemID EstimateID ParentEstimateItemID ItemType -------------- ---------- -------------------- -------- 1 A NULL product 2 A 1 product 3 A 2 service 4 A NULL product 5 A 4 product 6 A 5 service 7 A 1 service 8 A 4 product Graphical view of the tree structure (* denotes 'service'): A ___/ \___ /

CTE Recursion to get tree hierarchy

倾然丶 夕夏残阳落幕 提交于 2019-12-27 10:33:30
问题 I need to get an ordered hierarchy of a tree, in a specific way. The table in question looks a bit like this (all ID fields are uniqueidentifiers, I've simplified the data for sake of example): EstimateItemID EstimateID ParentEstimateItemID ItemType -------------- ---------- -------------------- -------- 1 A NULL product 2 A 1 product 3 A 2 service 4 A NULL product 5 A 4 product 6 A 5 service 7 A 1 service 8 A 4 product Graphical view of the tree structure (* denotes 'service'): A ___/ \___ /

Bucket filling SQL Server

不羁的心 提交于 2019-12-25 08:30:23
问题 I am trying to extend the below query to allow the bucket filling from a table rather than a single value. In below example I want to replace @AmountToAllocate with a table of potential values that will be used to fill the bucket. Any suggestion highly appreciated. create table dbo.Buckets ( TotalSize int not null, Amount int not null, BucketID int not null, constraint pk_Buckets primary key (BucketID), constraint ck_Buckets_Amount check ( Amount between 0 and TotalSize) ) go insert into dbo