I have been looking at CROSS / OUTER APPLY
with a colleague and we're struggling to find real life examples of where to use them.
I've spent quite a lot of time looking at When should I use Cross Apply over Inner Join? and googling but the main (only) example seems pretty bizarre (using the rowcount from a table to determine how many rows to select from another table).
I thought this scenario may benefit from OUTER APPLY
:
Contacts Table (contains 1 record for each contact) Communication Entries Table (can contain n phone, fax, email fro each contact)
But using subqueries, common table expressions, OUTER JOIN
with RANK()
and OUTER APPLY
all seem to perform equally. I'm guessing this means the scenario isn't applicable to APPLY
.
Please share some real life examples and help explain the feature!
One real life example would be if you had a scheduler and wanted to see what the most recent log entry was for each scheduled task.
There are various situations where you cannot avoid
CROSS APPLY
orOUTER APPLY
.Consider you have two tables.
MASTER TABLE
DETAILS TABLE
There are many situation where we need to replace
INNER JOIN
withCROSS APPLY
.1. If we want to join 2 tables on
TOP n
results withINNER JOIN
functionalityConsider if we need to select
Id
andName
fromMaster
and last two dates for eachId
fromDetails table
.The above query generates the following result.
See, it generated results for last two dates with last two date's
Id
and then joined these records only in outer query onId
, which is wrong. To accomplish this, we need to useCROSS APPLY
.and forms he following result.
Here is the working. The query inside
CROSS APPLY
can reference the outer table, whereINNER JOIN
cannot do this(throws compile error). When finding the last two dates, joining is done insideCROSS APPLY
ie,WHERE M.ID=D.ID
.2. When we need
INNER JOIN
functionality using functions.CROSS APPLY
can be used as a replacement withINNER JOIN
when we need to get result fromMaster
table and afunction
.And here is the function
which generated the following result
1. If we want to join 2 tables on
TOP n
results withLEFT JOIN
functionalityConsider if we need to select Id and Name from
Master
and last two dates for each Id fromDetails
table.which forms the following result
This will bring wrong results ie, it will bring only latest two dates data from
Details
table irrespective ofId
even though we join withId
. So the proper solution is usingOUTER APPLY
.which forms the following desired result
2. When we need
LEFT JOIN
functionality usingfunctions
.OUTER APPLY
can be used as a replacement withLEFT JOIN
when we need to get result fromMaster
table and afunction
.And the function goes here.
which generated the following result
CROSS APPLY
orOUTER APPLY
can be used to retainNULL
values when unpivoting, which are interchangeable.Consider you have the below table
When you use
UNPIVOT
to bringFROMDATE
ANDTODATE
to one column, it will eliminateNULL
values by default.which generates the below result. Note that we have missed the record of
Id
number3
In such cases a
CROSS APPLY
orOUTER APPLY
will be usefulwhich forms the following result and retains
Id
where its value is3
To answer the point above knock up an example:
And now run the two queries with a execution plan.
You can see that the outer apply query is more efficient. (Couldn't attach the plan as I'm a new user... Doh.)
Some uses for
APPLY
are...1) Top N per group queries (can be more efficient for some cardinalities)
2) Calling a Table Valued Function for each row in the outer query
3) Reusing a column alias
4) Unpivoting more than one group of columns
Assumes 1NF violating table structure....
Example using 2008+
VALUES
syntax.In 2005
UNION ALL
can be used instead.