• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 29
  • Last Modified:

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.
0
Julian M.
Asked:
Julian M.
  • 4
  • 2
2 Solutions
 
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
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial 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
 
Julian M.Author Commented:
Thank you!
0
 
SujithData ArchitectCommented:
Glad to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now