Join table to only the top record

I am performing a left join, table 'Individual' only has one record per 'person', it joins a list of addresses, but there may be more than one address per person. I want to join only to the first address  if there is a case in which there is more than one so not to get more than one result per person.

This is my current code:

SELECT *
FROM [TrackerSostrin].[dbo].[Individual]
LEFT JOIN dbo.BDotAddressesHome ON [TrackerSostrin].[dbo].[Individual].[IndivID] = [TrackerSostrin].[dbo].[BDotAddressesHome].[IndivID]

Open in new window


So Individual would need to left join BDotAddressesHome  but only the top record.
LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Try this:
SELECT  *
FROM    [TrackerSostrin].[dbo].[Individual]
CROSS APPLY (SELECT TOP 1 * FROM dbo.BDotAddressesHome
             WHERE [TrackerSostrin].[dbo].[Individual].[IndivID] = [TrackerSostrin].[dbo].[BDotAddressesHome].[IndivID]) t

Open in new window

You can even add an ORDER BY if you want to get an oldest or newest record and have a date or key field that would reflect it, as follows:
SELECT  *
FROM    [TrackerSostrin].[dbo].[Individual]
CROSS APPLY (SELECT TOP 1 * FROM dbo.BDotAddressesHome
             WHERE [TrackerSostrin].[dbo].[Individual].[IndivID] = [TrackerSostrin].[dbo].[BDotAddressesHome].[IndivID]
             ORDER BY SomeDateOrKeyField DESC) t

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
That doesn't work because it seems it does an inner join and some individuals don't have an address, this is why I was trying a 'left join'
dsackerContract ERP Admin/ConsultantCommented:
Makes sense. Simply replace CROSS APPLY with OUTER APPLY, which is the equivalent of a LEFT JOIN. :)
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

AleksAuthor Commented:
Thanks Ill try it tonight.
AleksAuthor Commented:
Worked !  thank you !
PortletPaulEE Topic AdvisorCommented:
In future I suggest you include the topic MS SQL Server.

An alternative to using an APPLY operator (which is specific to MS SQL Server) is to use ROW_NUMBER() which is available in several rdbms
SELECT
      *
FROM [TrackerSostrin].[dbo].[Individual] i
      LEFT JOIN (
            SELECT
                  *
                , ROW_NUMBER() OVER (PARTITION BY IndivID 
                                     ORDER BY SomeDateOrKeyField DESC) AS rn
            FROM dbo.BDotAddressesHome
      ) a ON i.[IndivID] = a.[IndivID]
                  AND a.rn = 1

Open in new window

AleksAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.