SQL: Joins that limit results from one of the tables

November 16, 2010 No Comments

I’ve been grappling with this one for a while now, and I finally came up with a solution, so I figured I better post this someplace because when it comes time for me to do it again, I need to remember how!

Sometimes you want to get a list of records, and join them with another table, but with one-to-many relationships, this will result in records from the first table showing more than once. Here’s an example:

We have two tables, Customer and Address. Each customer can have multiple addresses. I want to get a list of all our customers and list their corresponding address next to them. For customers with multiple addresses, I just want to show one of their addresses. In fact, in my address table, I have a bit indicating a default address. Let’s just show their default address.

If we query a simple join:

select * from Customer C left join Address A on C.customer_ID = A.customer_ID

This works, but customers with multiple addresses are returned over and over, once for each of their addresses.

The answer is to include a top statement in a subquery in the ON clause.

select * from Customer C left join Address A ON C.customer_ID=A.customer_ID AND A.address_ID in (select top 1 AA.address_ID from Address AA where AA.customer_ID=C.customer_ID order by DefaultAddress desc)

If address records are optional, you can also add clauses that allow it to be null too, but this is a simplified version.

* Featured, * News, TSQL Programming

Leave a Reply

(required)

(required)


Are you backing up your data?

Most people know they should back up their data, but not everyone does. If you’re one of those people,...

5 Easy Choices For a Better Website.

Are you planning to design or redesign your website soon? If so, then here are a few tips that...

How to enable site maps in Waypoint

Sitemaps are simple XML files that Google and other search engines use to find the content in your website....

5 reasons you should learn how to use HTML

HTML is the backbone of everything online. Everyone should know how to read it and write it! OK, well...

How to Link Facebook to Twitter

Posting to Facebook, LinkedIn, Twitter and other social media sites is a great way to interact with your customers....

Visual Studio Environment Tips

This week I had to send my iMac in for repair. Normally I program using Windows Server 2008 running...

Fixing IIS7 Maximum Upload Size

If you have your Waypoint site hosted with Twin Harbor and you need to increase the maximum file upload...

What is an SSL Certificate?

You may have heard about SSL Certificates, also called secure certificates, or SSL Certs for short. But what are...

OSX Lion Removes Functionality

In my previous post I thought I had found a way to maintain some of the functionality that we...

Assign Applications to “Spaces” in OSX Lion

I was a happy user of Spaces in OSX 10.6 Snow Leopard. You could assign certain applications to certain...