CTE within a CTE

后端 未结 1 1024
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 02:28

Is it possible to write a CTE within a CTE?

I want it to follow this logic, but the interpreter doesn\'t like this code.

with outertest as(

    with tes         


        
1条回答
  •  失恋的感觉
    2021-01-31 03:17

    You can't nest CTEs like that in SQL Server but you can use multiple CTEs the following way:

    ;with test as 
    (
        select 
            SRnum, 
            gamenumber, 
            StartOfDistribution, 
            ApplicationNumber   
        from #main
        where startofdistribution = '2011-06-14 00:00:00.000'
        and SRnum = '313'
        --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
    

    0 讨论(0)
提交回复
热议问题