Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

SQL Server Reporting Services (SSRS) – When to use OUTER APPLY instead of LEFT OUTER JOIN for complex SQL Statements

Scenario:

You create a report. You have one parameter called ‘CITY’ which contains all valid cities in the state of Florida. This parameter is used to report on all citizens that live in a particular city.

You have two tables.
1) name_table that contains all citizens in Florida.
2) address_table that contains the citizen’s current and previous addresses.

Your report will show the following:
a) Name
b) CURRENT address, city, state, and zipcode

You want to retrieve each citizen’s current address, so you write your SQL like this (Assumming that the address with the greatest entered_date  column is the citizen’s current address)

SELECT lastname, firstname, ADDR.address, ADDR.city, ADDR.state, ADDR.zipcode, ADDR.entered_date
   FROM  name_table a
   LEFT OUTER JOIN (SELECT top 1 address, city, state, zipcode
                               FROM address_table
                               WHERE name_guid  = a.name_guid
                               order by entered_date desc) ADDR
   WHERE ADDR.city IN (@City)

Note: Instead of the typical LEFT OUTER JOIN statement, you need to use a sub select to get the top 1 address (current address) of each citizen based on the entered_date ordered DESC.

Problem?

You can not reference a.name_guid within the sub select of the LEFT OUTER JOIN.
The error you will receive is

“The multi-part identifier “a.name_guid” could not be bound”

Solution?

Use OUTER APPLY instead of LEFT OUTER JOIN

SELECT lastname, firstname, ADDR.address, ADDR.city, ADDR.state, ADDR.zipcode, ADDR.entered_date
   FROM  name_table a
   OUTER APPLY (SELECT top 1 address, city, state, zipcode
                         FROM address_table
                         WHERE name_guid  = a.name_guid
                         order by entered_date desc) ADDR
   WHERE ADDR.city IN (@City)

Leave a Reply