JOIN or UNION?

Hello! I need some help with an SQL query (MSSQL).

I need to return a set of contact IDs from a "Contact" table. However, I also have an "Account" table, which holds information about account holders. Each record from the Account table has a linked Contact record. I only want to return "Contact" records where the linked "Account" record is set to active. This part is fine, I can do this using the following query.

SELECT DISTINCT c.core_UserAccountName AS [ContactID]
FROM contact c
JOIN account a ON a.core_UserAccountName = c.core_UserAccountName
WHERE
	a.statecode = 0 and a.statuscode = 1 --active

Open in new window


The part where I'm getting stuck is this... Some Contact records are sub-user accounts. They are not directly linked to the Account table but rather through an additional table called "core_PortalAccountHolder". I would like to include these Contact records in my result set.

core_PortalAccountHolder.core_ContactId is linked to Contact.ContactId and core_PortalAccountHolder.core_AccountId is linked to Account.AccountId.

Any help would be appreciated.
Julian M.Asked:
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.

SujithData ArchitectCommented:
SELECT c.core_UserAccountName AS [ContactID]
FROM contact c
JOIN account a ON a.core_UserAccountName = c.core_UserAccountName
WHERE
	a.statecode = 0 and a.statuscode = 1 
UNION ALL
SELECT c.core_UserAccountName AS [ContactID]
FROM contact c2
JOIN core_PortalAccountHolder cp on c2.ContactId = cp.core_ContactId 
JOIN account a2 ON cp.core_AccountId = a2.AccountId
WHERE
	a2.statecode = 0 and a2.statuscode = 1 
)
select  distinct [ContactID]
from    data

Open in new window

0
Julian M.Author Commented:
Hi @Sujith. Thanks for that! I'm getting the following error on this one:

The multi-part identifier "c.core_UserAccountName" could not be bound.
0
SujithData ArchitectCommented:
looks like I didnt paste the whole thing

with data as (
SELECT c.core_UserAccountName AS [ContactID]
FROM contact c
JOIN account a ON a.core_UserAccountName = c.core_UserAccountName
WHERE
	a.statecode = 0 and a.statuscode = 1 
UNION ALL
SELECT c2.core_UserAccountName AS [ContactID]
FROM contact c2
JOIN core_PortalAccountHolder cp on c2.ContactId = cp.core_ContactId 
JOIN account a2 ON cp.core_AccountId = a2.AccountId
WHERE
	a2.statecode = 0 and a2.statuscode = 1 
)
select  distinct [ContactID]
from    data

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

SujithData ArchitectCommented:
If you are to do it using JOIN you may try something like this -
with data as (
SELECT c.core_UserAccountName AS [ContactID], NVL(a.statecode,a2.statecode) as statecode, NVL(a.statuscode, a2.statuscode) as statuscode
FROM contact c
LEFT JOIN account a ON a.core_UserAccountName = c.core_UserAccountName
LEFT JOIN core_PortalAccountHolder cp on c.ContactId = cp.core_ContactId 
LEFT JOIN account a2 ON cp.core_AccountId = a2.AccountId 
)
select  distinct ContactID
from    data
where   statecode = 0 and statuscode = 1

Open in new window

0

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
Julian M.Author Commented:
Thank you!
0
SujithData ArchitectCommented:
Glad to help
0
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.