CTE in select Statement

  • Hi

    I have a situation like below.

    With CTE

    (

    Select * from a1

    inner join a2

    )

    Select * from cte.

    What i want here that i will need to place another CTE in place of a2 table.

    is it possible?

    Let me know if you need more information.

    any help would be appreciated...

    Thanks..

  • Something like

    With CTE as

    (

    Select a2.* from a1

    inner join a2

    on a1.col=a2.col

    ),

    CTE2 as

    (

    Select a4.* from a3

    inner join a4

    on a3.col=a4.col),

    CTE3 as

    (

    Select a5.* from CTE1

    inner join a5

    on CTE1.col=a5.col

    )

    Select cte3.* from cte

    inner join CTE3

    on cte.col=cte3.col

    As you can see, you can even reference one cte within another cte.

    All separate CTEs would be treated as subqueries, just easier to write... 😉

    It's also possible to use recursive CTEs. For details, please see BOL (BooksOnLine).



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thank you vey much for your time...

    I will modify my code as you mentioned format.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply