本文介紹了CTE 中的 CTE的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否可以在 CTE 中編寫 CTE?
Is it possible to write a CTE within a CTE?
我希望它遵循這個邏輯,但解釋器不喜歡這段代碼.
I want it to follow this logic, but the interpreter doesn't like this code.
with outertest as(
with test as (
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 Server 中嵌套這樣的 CTE,但您可以通過以下方式使用多個 CTE:
You can't nest CTEs like that in SQL Server but you can use multiple CTEs the following way:
;with test as
(
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
),
outertest as
(
select
ApplicationNumber
,count(*) as RetailerAppearance
from test
group by ApplicationNumber
having count(*) = 4
)
select count(*)
from outertest
這篇關于CTE 中的 CTE的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!