Hooray! It’s CTE month. Some of you may be happy but you others may by like ugh. Common table expressions can be tough and confusing but WE’VE GOT YOU COVERED! This month we will be throwing lots of valuable CTE information and resources at you including….. Instructional videos Tips of the trade And a FREE download! Check in daily to …

It’s CTE Month at Stedman Solutions Read more »

Ok cool Stedman Solutions is having CTE (common table expressions) month in June. What does that mean for you my reader? Well let me share all the goodies I have to offer this month! Next month I will be releasing……. Over 15 CTE videos with topics ranging from a memory table to recursive CTE’s Common table expressions hot tips A …

What Does CTE Month Mean for You? Read more »

Russian Nesting Dolls If you have ever seen the Matryoshka dolls known as the Russian nesting dolls or babushka dolls they are very interesting. They start with a large wooden doll that when opened contains a slightly smaller doll, then inside of that one an even smaller doll. These dolls often have five to eight dolls nested inside of each …

Nested CTEs Read more »

Have you ever needed to generate a list of dates for the last 30 days.  There are lots of different ways to do this with TSQL, but one way is with a recursive CTE. You can then left join against it with another query to generate a list of all  dates in a result set, along with the dates for …

Recursive CTE to Calculate Dates for the Last 30 Days Read more »

The question came up as to how do I parse a query string using TSQL.  So here you go, using a common table expression, and basing this on a similar function to what I put together yesterday for split. which produces this output. A quick and easy way to parse a query string  in TSQL using a CTE. Receive a …

Using a CTE in a Function to Split Up a Query String Read more »

After my CTE presentation a while back I was asked many questions, and received several great suggestions from people.   One question was how does the performance compare between a recursive CTE to generate a hierarchical tree path listing and a query using self JOINs and UNION ALL to generate similar results.  To test this I created a simple table …

CTE Hierarchy compared to the alternative Read more »

Years ago while working on my CTE presentation for a SQL Saturday I added a blog post called “Using a CTE to Split a String Into Rows“, and since that posting I have used it many times. But as things go in development eventually there is a need to do something more. Doing some complex string building to create files …

Using a CTE to Split a String Into Rows with Line Numbers Read more »

The following question came up when working on my CTE presentation for SQL Saturday. Does a query that JOINs a CTE to itself execute the CTE query once or twice? For instance:  ;WITH deptCTE(id, department, parent)    AS (SELECT id,department,parent FROM   Departments)SELECT q1.department,q2.department  FROM deptCTE q1 INNER JOIN deptCTE q2 ON q1.id = q2.parent WHERE q1.parent IS NULL;  The following execution plan is produced showing that the Departments table is hit twice with a table scan each time with the same cost. …

CTE Query Performance Read more »