Often times in an application, I have wanted to output a simple list of items that have a one-to-many relationship with other items in the database. One scenario that I come across very often involves attorneys and offices; many attorneys will work out of more than one office, but in an attorney directory, they only want to be listed with their primary office. This is an easy query IF (and only if) "primary office" is explicitly defined using some sort of "is_primary" flag or "sort" indicator, but often times, this filtering is not available.
As such, if you were just to create the SQL JOIN between attorneys and offices, you would get a lot of duplicate names (one for each office relationship), but this is way too much data. Yesterday, I was dealing with a very similar situation and was convinced that I could do all of this in a single query, rather than use query of queries or output shenanigans. What I came up with works, but only with small sets of data; it is not a very efficient method and should be avoided when dealing with large data sets.
To explore this idea, let's create the SQL tables we are going to be used. For this demo, I am going to be using a Contact table and Phone table. The contact table holds people's names. The phone table holds associated phone numbers:
Launch code in new window » Download code as text file »
As you can see from this SQL code, we have 4 contacts. Three of them have phone numbers and only two of them have a phone number flagged as "preferred". This means that the "preferred" phone number will not always be easy to access, such as with Maria Bello, who has three numbers but no preference.
Before we get into anything tricky, let's look at a standard JOIN that would bring back both contacts and phone numbers:
Launch code in new window » Download code as text file »
As you can see, we just join the two tables based on the associated contact_id. Running the above SQL code, we get the following CFDump output:
| | | | ||
| | ![]() | | ||
| | | |
In this query, we are getting back all contact-phone number associations. For a simple list, however, we don't want that; we only want one contact with a max of one phone number.
If we always had a preferred phone number or had some sort value that was always defined, getting this data would not be a problem - we would just add the sort=1 or is_preferred=1 logic to the LEFT OUTER JOIN. However, as you can see, that can't be done in our example. To get around this, our LEFT OUTER JOIN logic has to get a bit crazy. As part of our join, we want to only get the phone number that is most preferable (which does not mean is_preferred).
Before we look at the overall query, let's think about how we would get the most preferred number for any given contact. Well, we want to prioritize the phone numbers that are flagged as is_preferred. Of course, if that is not defined, or that is always zero, then we need to make an arbitrary decision and say that the first phone number created (as dictated by the auto-incrementing ID) is the most preferred.
Taking that logic, if we wanted to get the most preferred number for contact (ID:1), we would run SQL like this:
Launch code in new window » Download code as text file »
This will return one (or zero) phone records with the most "preferred" number for contact:1. Now, we want to take this logic, and integrate it into our SQL LEFT OUTER JOIN logic:
Launch code in new window » Download code as text file »
Now, our LEFT OUTER JOIN logic not only joins based on the contact ID, it also requires that the ID of the phone number in the join be the ID of the most preferred phone number for that contact. Running this code, you see that we get a much better and more useful query result set:
| | | | ||
| | ![]() | | ||
| | | |
This query is easy to work with, in terms of results, but like I said, this is only good for small sets of data. Anytime you make a sub query be part of you JOIN logic, whether it's an INNER or OUTER JOIN, you've got problems; that's a lot of look-ups to perform and the query is going to be slow. But, like I said, if you just need a simple list with one record per group in a one-to-many query, this is an option that I discovered yesterday.
Download Code Snippet ZIP File
Comments (6) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
CFQueryParam Data Conversion Caveat (Thanks Tony Petruzzi)
ColdFusion CFQueryParam Binding vs. SQL Execution
That UNION ALL trick is rather convenient, isn't it?
How about this one:
Instead of the line-by-line join that yours used, this one tries to grab the best phone number for every contact all at once. The COALESCE() sets up two choices:
1. The first MIN() looks for the first preferred number. It does this just in case someone accidentally ends up with more than one preferred number.
2. The second MIN looks for the first number, whether or not it is preferred.
The "r" subquery then has the best number for each contact, and we join again to [phone] to get the details for the number.
Posted by Rick O on Dec 19, 2007 at 9:39 AM
@Rick,
I formatted your SQL statement so I could read it. Very clever stuff. I like it cause you are using the subquery to create an intermediary table that is not row-specific, so you lack the overhead that my ON-clause subquery has. Very slick.
Also, I am pretty sure I learned that UNION ALL trick from you a long time ago. If I didn't say thanks then, THANK YOU now - it has made testing SQL stuff insanely easy :)
Rick, why are you so clever?!?
Posted by Ben Nadel on Dec 19, 2007 at 9:59 AM
check this 1 out:
Posted by Peter Swanson on Dec 19, 2007 at 7:09 PM
@Peter,
I formatted your code snippet so that I could better read it (I hope that you don't mind).
In yours, I am not sure that you can get the phone Ext if is not preferred. Your ON clause will exclude the record for non-preferred items. But then, your select clause only makes up for the phone_number, not the Ext field.... I think.
Posted by Ben Nadel on Dec 20, 2007 at 4:27 PM
Yes, you're right. The extension would require another subquery just like the phone number.
Oh well, thought I'd give it a shot.
P.S. Thanks for formatting.
Peter
Posted by Peter Swanson on Dec 20, 2007 at 5:00 PM
@Peter,
No worries. The more people we have look at / taking shots at a solution, the better then end result will be. Thanks for taking the time to give it a go.
Posted by Ben Nadel on Dec 20, 2007 at 5:03 PM