回答

收藏

CTE中的CTE

技术问答 技术问答 576 人阅读 | 0 人回复 | 2023-09-14

是否可以在CTE中编写CTE?. _8 i, R$ B' M5 _- i* _
我希望它遵循这个逻辑,但解释器不喜欢它。: p/ w0 p5 v2 j$ U
with outertest as(    with test as (        select             SRnum,            gamenumber,            StartOfDistribution,            ApplicationNumber           from #main        where startofdistribution = '2011-06-14 00:00:00.        and SRnum =   --order by SRnum,gamenumber,StartOfDistribution,ApplicationNumber    )    select        ApplicationNumber count(*) as RetailerAppearance    from test    group by ApplicationNumber    having count(*) = 4) select count(*) from outertest                9 ^$ H$ L% X' I! u
    解决方案:                                                               
* D+ [( T1 S6 N9 s, p. @                                                                您不能在SQL Server像这样嵌套CTE,但是,可以使用以下方法CTE:0 l; ?! H5 Y' S; t4 C4 d; u+ |
;with test as  select         SRnum,        gamenumber,        StartOfDistribution,        ApplicationNumber       from #main    where startofdistribution = '2011-06-14 00:00:00.000'    and SRnum =  --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
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则