2 MINUTE READ | March 14, 2017
SQL Join on One Column Contained in Another
SQL joins typically look to combine two tables into a set of columns that exist in both. Usually, the values match exactly, making the join straightforward. We can also use ILIKE to make the join case-insensitive and BETWEEEN if we have the date in one table and start date and end date in another.
Those don’t always get the job done on their own. Sometimes we need to be able to join one table to another if the value in the first table is contained in the value in the second. That’s probably clear as mud, so let me walk you through a practical example.
I typically use this technique when working with combined budgets. We typically don’t get our budgets down to the vendor level, we get one budget for a set of vendors, as seen below.
Our cost data, however, is broken out by vendor.
So how can we easily pull the cost data for each vendor in a given budget? Let’s backtrack for a second. How would we pull all budgets that contain the vendor ‘mediamath’?
We would leverage the wildcard symbol. Placing a wildcard at the start and end of the string finds all budget line items where the vendor’s column contains ‘mediamath’.
The difference for the join is that we have to apply the wildcard to the start and end of the vendor column in the cost data. We can do this by simply concatenating the wildcard to the start and end of the column name. This creates a dynamic string as opposed to the static ‘%mediamath%’ in the previous example.
Sign up for weekly articles & resources.
Now we see our budget line items with all the cost right there, just as we wanted. It’s a pretty simple trick once you look at it, but extremely effective when you need it.
Posted by Preston Smith
1 MINUTE READ | September 28, 2021
4 MINUTES READ | August 30, 2021
5 MINUTES READ | November 19, 2020
4 MINUTES READ | August 31, 2020
1 MINUTE READ | April 29, 2020
8 MINUTES READ | April 28, 2020
1 MINUTE READ | April 23, 2020
2 MINUTES READ | February 4, 2020
4 MINUTES READ | December 2, 2019
2 MINUTES READ | September 4, 2019
1 MINUTE READ | August 28, 2019
5 MINUTES READ | August 22, 2019