APPLY Syntax

  • I got the following example of some code to use an OUTER APPLY to get some data I need. I was able to get it to work in my environment, but there's one piece of the syntax I don't understand.

    select Table1Key, MAX(LastModifiedDate)

    from Table1

    outer apply (

    select Table1.LastModifiedDate

    union all

    select LastModifiedDate

    from RTable01

    where RTable01.Table1Key = Table1.Table1Key

    union all

    select LastModifiedDate

    from RTable02

    where RTable02.Table1Key = Table1.Table1Key

    union all

    select LastModifiedDate

    from RTable30

    where RTable30.Table1Key = Table1.Table1Key

    )

    ---------------------------------------------------------

    AllTables(LastModifiedDate)

    ----------------------------------------------------------

    group by Table1.Table1Key

    What is the AllTables() statement at then end of the OUTER APPLY() doing? I can't find any syntax references to it, and the whole thing doesn't work without it, so I feel like I should find out what it's doing!

  • rray 44280 (7/11/2013)


    I got the following example of some code to use an OUTER APPLY to get some data I need. I was able to get it to work in my environment, but there's one piece of the syntax I don't understand.

    select Table1Key, MAX(LastModifiedDate)

    from Table1

    outer apply (

    select Table1.LastModifiedDate

    union all

    select LastModifiedDate

    from RTable01

    where RTable01.Table1Key = Table1.Table1Key

    union all

    select LastModifiedDate

    from RTable02

    where RTable02.Table1Key = Table1.Table1Key

    union all

    select LastModifiedDate

    from RTable30

    where RTable30.Table1Key = Table1.Table1Key

    )

    ---------------------------------------------------------

    AllTables(LastModifiedDate)

    ----------------------------------------------------------

    group by Table1.Table1Key

    What is the AllTables() statement at then end of the OUTER APPLY() doing? I can't find any syntax references to it, and the whole thing doesn't work without it, so I feel like I should find out what it's doing!

    That is giving the subquery an alias of AllTables and assigning the 1 column the name of LastModifiedDate.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

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

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