回答

收藏

CTE中的CTE

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

是否可以在CTE中编写CTE?" f. Q# G3 D2 R9 k
我希望它遵循这个逻辑,但解释器不喜欢它。
  q, S$ |$ s# B2 j0 R! ?2 cwith 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               
  C' o6 p# m  F- o    解决方案:                                                               
) r( u& `/ w( r( [7 o) i9 U; T                                                                您不能在SQL Server像这样嵌套CTE,但是,可以使用以下方法CTE:( E( S2 [; m# e8 [: f' Q% n
;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
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则