gpt4 book ai didi

sql - CTE 内的 CTE

转载 作者:行者123 更新时间:2023-12-01 19:03:51 26 4
gpt4 key购买 nike

是否可以在 CTE 内编写 CTE?

我希望它遵循这个逻辑,但解释器不喜欢这段代码。

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:

;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

关于sql - CTE 内的 CTE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com