Questions tagged [sql-cte]

In Microsoft SQL Server 2005 the common table expression (CTE) is a temporary named result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement.

The common table expression (CTE), Introduced in Microsoft , is a temporary named result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement.

A CTE can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. It is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.

A CTE can be used to:

  • Create a recursive query.
  • Substitute for a view when the general use of a view is not required.
  • Enable grouping by a column that is derived from a scalar subselect, or a function that is either not deterministic or has external access.
  • Reference the resulting table multiple times in the same statement.

Resources

Using Common Table Expressions

10 questions
8
votes
1 answer

CTE (Common Table Expression) in EFCore

I have a table in my DB that stores the hierarchy of locations. It has 3 columns (Id, Name, ParentId). I need to load some row base on a condition and all their parents up to the root. If it was in ADO I would use the following statement. with…
1
vote
1 answer

How to combine uniqueidentifier IDs in T-SQL CTE query into string

I want to build uniqueidentifier IDs with comma separated as a string in a T-SQL CTE query: WITH departmentcte(ID, Name, ParentID, LEVEL, FullPath) AS ( SELECT ID, Name, ParentID, 0 AS LEVEL, CAST(Name AS VARCHAR(1024)) AS…
NoWar
  • 36,338
  • 80
  • 323
  • 498
1
vote
1 answer

SQL - Creating View with multiple CTEs

I have a View to create, which has 2 CTES. This is what I have done below. The first part is fine, but after "AND" things do not work. Your input is appreciated CREATE VIEW dbo.VW.SPAg AS With today as (SELECT * FROM dbo.Work_Days WHERE [Date] =…
Speedio
  • 45
  • 1
  • 5
0
votes
0 answers

Ragged Hierarchy using Multiple Nested Joins

I have a header table: BOM_ID, BOM_Name BOM001, PartA BOM002, PartB BOM003, PartC BOM004, PartD And a Line table BOM_ID, Item_Name BOM001, PartB BOM001, PartC BOM002, PartD BOM003, PartE BOM003, PartF BOM004, PartG I'm looking to get the…
0
votes
0 answers

Use CTE with dynamic SQL in stored procedures

I have a stored procedure that uses a CTE, however the table to be consulted is variable (I have several tables with the same structure), so I try use dynamic SQL. But it returns an error ("incorrect syntax near 'set'"), because I'm not referencing…
0
votes
0 answers

SQL parent child list all fullNames slower than list all FullIDS

Can someone explain this behavior? I have a temp table variable with 15000 rows: declare @sampleitems table ( id nvarchar(255), parentid nvarchar(255), …
Stelios
  • 330
  • 5
  • 21
0
votes
1 answer

Accees table attribute in Snowflake CTE

use database DQ_MART; use schema WORKING; WITH ASCENDER_EMPLOYEE AS ( **SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_ASCENDER_WORKER_TIMESHEET** ), WORKDAY_EMPLOYEE AS ( **SELECT DISTINCT EMPLOYEE_ID FROM…
Vijay
  • 49
  • 1
  • 9
0
votes
1 answer

SQL Server CTE hierarchy data type conversion

I have created a SQL Server hierarchy which is working fine. However I have problem in the hierarchy level data type handling which requires decimal addition. I tried that this query below works in adding decimals and working perfectly. SELECT …
Jemru
  • 2,091
  • 16
  • 39
  • 52
0
votes
1 answer

Running total SQL Server query

So far, I have the following SQL Server 2005 query: WITH D AS ( SELECT CONVERT(VARCHAR, '2020.11.01', 102) AS d_y_m, CAST('2020-11-01' AS DATETIME) AS dt UNION ALL SELECT CONVERT(VARCHAR, DATEADD(dd, 1, z.dt), 102) AS d_y_m, DATEADD(dd, 1,…
milo2011
  • 339
  • 1
  • 9
  • 25
0
votes
1 answer

Reading multiple records related to every record in the result set at a time

I have 3 tables: Message table having MsgID, MsgText, MsgDate columns. MsgId is PK. MessageSender table having MsgId, SenderId, SenderName. MsgId is FK. MessageTo table having MsgId, ToId, ToName. MsgId is FK. Every Message will have one Sender.…
Learner
  • 4,661
  • 9
  • 56
  • 102