Tomas Walek
Tomas Walek

Reputation: 2544

Multiple With-Subqueries

Can I use multiple WITH-subqueries within one query?

;WITH x AS (
  SELECT ...
),
WITH y AS (
  SELECT ...
)
SELECT * FROM z 
INNER JOIN x ON ...
INNER JOIN y ON ...

Upvotes: 1

Views: 88

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79969

It is called common table expression, and yes, you can use multiple CTE's, use only one WITH and remove the second with like this:

WITH x AS (
  SELECT ...
),y AS (
  SELECT ...
)
SELECT * FROM z 
INNER JOIN x ON ...
INNER JOIN y ON ...

Upvotes: 3

Related Questions