0% found this document useful (0 votes)
3 views

SQL - CTE Within a CTE - Stack Overflow

The document discusses the possibility of nesting Common Table Expressions (CTEs) within each other in SQL Server, concluding that this is not allowed. Instead, it suggests using multiple CTEs sequentially. The document includes example SQL code demonstrating the correct usage of CTEs.

Uploaded by

rajandayma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL - CTE Within a CTE - Stack Overflow

The document discusses the possibility of nesting Common Table Expressions (CTEs) within each other in SQL Server, concluding that this is not allowed. Instead, it suggests using multiple CTEs sequentially. The document includes example SQL code demonstrating the correct usage of CTEs.

Uploaded by

rajandayma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5/6/2019 sql - CTE within a CTE - Stack Overflow

CTE within a CTE Ask Question

Is it possible to write a CTE within a CTE?

I want it to follow this logic, but the interpreter doesn't like


31 this code.

with outertest as(

with test as (
10 select
SRnum,
gamenumber,
StartOfDistribution,
ApplicationNumber
from #main
where startofdistribution = '2011-06-14 00:00:00.000'
and SRnum = '313'
--order by SRnum, gamenumber, StartOfDistribution, ApplicationNumber
)
select
ApplicationNumber
,count(*) as RetailerAppearance
from test
group by ApplicationNumber
having count(*) = 4

) select count(*) from outertest

sql sql-server tsql

asked Sep 10 '13 at 20:47


Join Stack Overflow to learn, sharesion_corn
knowledge, and build your career.
2,053 7 28 47

Email Sign Up OR SIGN IN WITH Google Facebook


1 Answer
https://stackoverflow.com/questions/18728474/cte-within-a-cte 1/3
5/6/2019 sql - CTE within a CTE - Stack Overflow

You can't nest CTEs like that in SQL Server but you can
use multiple CTEs the following way:
Home 70
;with test as
PUBLIC (
select
Stack Overflow SRnum,
gamenumber,
Tags StartOfDistribution,
ApplicationNumber
Users from #main
where startofdistribution = '2011-06-14 00:00:00.000'
Jobs and SRnum = '313'
--order by SRnum, gamenumber, StartOfDistribution, App
),
outertest as
Teams (
Q&A for work select
ApplicationNumber
,count(*) as RetailerAppearance
Learn More from test
group by ApplicationNumber
having count(*) = 4
)
select count(*)
from outertest

edited Sep 11 '13 at 11:21

answered Sep 10 '13 at 20:50


Taryn ♦
196k 47 299 359

Seems like you can only run either one of the two cte queries.
JoinAny
Stack
wayOverflow to learn,
of running both? share
– Naufal Janknowledge, and build your career.
30 '18 at 17:53

@Naufal I'm not sure I understand your question. This chains


Email Sign
the CTEs Up so
together ORthey
SIGNrun
IN at Google
the same time.
WITH – Taryn ♦ Facebook
Jan 30 '18 at 17:55
https://stackoverflow.com/questions/18728474/cte-within-a-cte 2/3
5/6/2019 sql - CTE within a CTE - Stack Overflow

What I mean is running the two nested CTEs together at the


same time e.g. with cte1 as (query1), cte2 as (query2) select *
from cte1, select * from cte2. But I know that's not the case,
CTEs are not designed to perform the aforementioned
operation. I managed to find another way around this and it
was Tableau. – Naufal Aug 23 '18 at 15:52

Join Stack Overflow to learn, share knowledge, and build your career.

Email Sign Up OR SIGN IN WITH Google Facebook

https://stackoverflow.com/questions/18728474/cte-within-a-cte 3/3

You might also like