是否可以在CTE中编写CTE?- ]: N L! Z( u6 v9 ]1 H
我希望它遵循这个逻辑,但解释器不喜欢它。 e/ N+ T' d3 g
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 j& V, X) g3 H, D i 解决方案: 7 O4 S9 O) `% v: i 您不能在SQL Server像这样嵌套CTE,但是,可以使用以下方法CTE: c; E# P' A+ C& r; G v;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