回答

收藏

CTE中的CTE

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

是否可以在CTE中编写CTE?
2 ?# k$ _- t9 t( |$ W我希望它遵循这个逻辑,但解释器不喜欢它。0 e; I( ]: |7 I0 e. l
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               
7 e2 \' N, s  A' q0 Y    解决方案:                                                               
. N% _1 B; ]1 C" t( B6 W6 g                                                                您不能在SQL Server像这样嵌套CTE,但是,可以使用以下方法CTE:
9 [4 E4 N6 d' y( M9 e;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
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则