Count distinct values in Table_A.Col1 that don't exist in Table_B.Col1

Good morning!  

Working in MS SQL 2008.  

Hopefully the title is self-explanatory but, I'm trying to find the count of distinct values in Table_A.col1 that don't exist in Table_B.col1.  

TIA!
LVL 1
ttist25Asked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl..
SELECT COUNT(DISTINCT a.Col1)
FROM Table_A a
   -- LEFT means include all rows in Table_A...
  LEFT JOIN Table_B b ON a.col1 = b.col1
  -- ... where there is no matching Table_B row
WHERE b.Col1 IS NULL

Open in new window

0
Scott PletcherSenior DBACommented:
An alternate method.  This might run faster, depending on the specifics of the data in the two tables.


SELECT a.col1
FROM (
    SELECT DISTINCT col1
    FROM Table_A
) AS a
WHERE
    NOT EXISTS(
        SELECT 1
        FROM Table_B b
        WHERE
            b.col1 = a.col1
        )
--ORDER BY a.col1
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
ttist25Author Commented:
Thanks guys - that did the trick!
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

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.