231

I have this simple T-SQL query, it emits a bunch of columns from a table and also joins information from other related tables.

My data model is simple. I have a scheduled event, with participants. I need to know how many participants participate in each event.

My solution to this is to add a CTE that groups scheduled events and counts the number of participants.

This will allow me to join in that information per scheduled event. Keeping the query simple.

I like to keep my queries simple, however, If I ever in the future need to have additonal temporary results accessible during my simple query, what do I do?

I would really like it, if I could have multiple CTEs but I can't, right? What are my options here?

I've ruled out views and doing things at the application data layer. I prefer to isolated my SQL queries.

2 Answers 2

426

You can have multiple CTEs in one query, as well as reuse a CTE:

WITH    cte1 AS
        (
        SELECT  1 AS id
        ),
        cte2 AS
        (
        SELECT  2 AS id
        )
SELECT  *
FROM    cte1
UNION ALL
SELECT  *
FROM    cte2
UNION ALL
SELECT  *
FROM    cte1

Note, however, that SQL Server may reevaluate the CTE each time it is accessed, so if you are using values like RAND(), NEWID() etc., they may change between the CTE calls.

6
  • 4
    It was that simple. the MSDN documentation was a bit fuzzy around the issue, I couldn't find anything conclusive. Thank you very much! Jan 26, 2010 at 16:27
  • 3
    It's documented in WITH common_table_expression (Transact-SQL). You can see this are in the syntax section (take special note of the [ ,...n ] in [ WITH <common_table_expression> [ ,...n ] ]. Example C, "Using multiple CTE definitions in a single query," calls this out explicitly. Sadly, this example is not provided in the documentation for SQL 2008 and older (i.e., the example wasn't provided when the OP posted the question).
    – Brian
    Jul 10, 2014 at 15:01
  • I get double the amount of records on this :/ Apr 8, 2019 at 20:18
  • @TomStickel try only using the half of the query, before the last UNION ALL
    – Quassnoi
    Apr 9, 2019 at 2:03
  • 1
    @TomStickel my guess is the 2nd union is there only to illustrate Quassnoi's point that you can reuse a CTE. May 29, 2020 at 3:26
124

You certainly are able to have multiple CTEs in a single query expression. You just need to separate them with a comma. Here is an example. In the example below, there are two CTEs. One is named CategoryAndNumberOfProducts and the second is named ProductsOverTenDollars.

WITH CategoryAndNumberOfProducts (CategoryID, CategoryName, NumberOfProducts) AS
(
   SELECT
      CategoryID,
      CategoryName,
      (SELECT COUNT(1) FROM Products p
       WHERE p.CategoryID = c.CategoryID) as NumberOfProducts
   FROM Categories c
),

ProductsOverTenDollars (ProductID, CategoryID, ProductName, UnitPrice) AS
(
   SELECT
      ProductID,
      CategoryID,
      ProductName,
      UnitPrice
   FROM Products p
   WHERE UnitPrice > 10.0
)

SELECT c.CategoryName, c.NumberOfProducts,
      p.ProductName, p.UnitPrice
FROM ProductsOverTenDollars p
   INNER JOIN CategoryAndNumberOfProducts c ON
      p.CategoryID = c.CategoryID
ORDER BY ProductName
2
  • 8
    @JohnLeidegren: posting a correct answer within 2 minutes of the first correct answer merits an upvote, which I've given, at least. Mar 20, 2012 at 15:52
  • what if one of them is a recursive cte? is there any particular order that needs to be followed?
    – NAGA
    Jan 25, 2021 at 2:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.