是否可以在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