SQL Script help

I have two tables (Customer and Territory).  I'm needing a select statement that will find all customers that don't have a territory of 1.05 and 1.09 so I can add them to my territory table later.  For some reason I'm having trouble with this.
2015-09-22_12-12-36.jpg2015-09-22_12-12-52.jpg
I was going to do a temp table or CTE that stored all my customers that had the territories that I need then show all the customers that don't match from the customer table but for some reason this didn't work.

Thanks for your help
LVL 1
abarefootAsked:
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:
>find all customers that don't have a territory of 1.05 and 1.09
Total air code.  Give this a whirl, which uses a subquery to count the 1.05 and 1.09's, then a main query to return all where the count is less than 2.
SELECT c.c_id, c.c_name,  t.its_got_it
FROM Customer c 
   JOIN (SELECT c_id, SUM(CASE WHEN Territory = 1.05 OR Territory = 1.09 THEN 1 ELSE 0 END) as its_got_it FROM Territory GROUP BY c_id)  t ON c.c_id = t.c_id 
WHERE t.its_got_it < 2

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
Select c_id, c_name from Customer c
inner join CustTerritory ct
on  ct.c_id = c.c_id
Where c.Territory Not In(1.05,  1.09)
0
abarefootAuthor Commented:
Jim - Not sure how this accomlises what I'm wanting.

Mike - This returns every territory other then 1.05 and 1.09 even if they are already a member of 1.05 and 1.09.  So if they are a member of 1.01 and 1.05 they still show up because they are a member of 1.01.  I need that customer to not show up.  Also territory is not in the customer table

I need this to return customers that are not members of 1.05 and 1.09 at all.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Scott PletcherSenior DBACommented:
SELECT t.c_id, MAX(t.c_name) AS c_name
FROM Territory t
GROUP BY t.c_id
HAVING SUM(CASE WHEN t.territory IN ('1.05', '1.09') THEN 1 ELSE END) = 0
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>return customers that are not members of 1.05 and 1.09 at all.
So .. does this mean not 1.05 OR 1.09, or not both 1.05 AND 1.09?

In the above sample data
Since none of them have an 1.09, and AND would return all c_id's.
2 and 4 have a 1.05, so an OR would return all except those two.
0
abarefootAuthor Commented:
The results I'm looking for are below.  Since none of those have 1.09 or 1.05

c_name               c_id
Bob                       1
Jill                          3
Paul                      7
Jim                        6
joe                        5
0
Mike EghtebasDatabase and Application DeveloperCommented:
Select c.c_id, c.c_name from Customer c
inner join CustTerritory ct
on  ct.c_id = c.c_id Where c.c_id Not In
(Select c2.c_id from Customer c2
inner join CustTerritory ct2
on  ct2.c_id = c2.c_id
Where c2.Territory In(1.05,  1.09))

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
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>The results I'm looking for are below.  Since none of those have 1.09 or 1.05
Ok, that would be 1.09 OR 1.05, which is different than how the original question was asked.

Pretty sure my code works, so explain 'Not sure how this accomlises what I'm wanting.'.
Scott's code will work too.
0
abarefootAuthor Commented:
Didn't mean OR but AND.
0
abarefootAuthor Commented:
Mike's should work but I'm having issues with the NOT IN part.  It's giving me an error on "NOT IN"
0
Mike EghtebasDatabase and Application DeveloperCommented:
try with Not Exist instead of Not In
0
abarefootAuthor Commented:
Mikes script did the trick.  Thanks
0
abarefootAuthor Commented:
Thanks Mike this is what I was needing.
0
Scott PletcherSenior DBACommented:
Did you try mine?

If you need additional detail from the table(s), you can join to that result:

SELECT t.*
FROM (
    SELECT t.c_id
    FROM Territory t
    GROUP BY t.c_id
    HAVING SUM(CASE WHEN t.territory IN ('1.05', '1.09') THEN 1 ELSE END) = 0
) AS t_matches
INNER JOIN dbo.territory t ON t.c_id = t_matches.c_id
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

From novice to tech pro — start learning today.