SQL Script help

abarefoot
abarefoot used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>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

Mike EghtebasDatabase and Application Developer

Commented:
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)

Author

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.
Ensure you’re charging the right price for your IT

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

Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
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
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>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.

Author

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
Database and Application Developer
Commented:
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

Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>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.

Author

Commented:
Didn't mean OR but AND.

Author

Commented:
Mike's should work but I'm having issues with the NOT IN part.  It's giving me an error on "NOT IN"
Mike EghtebasDatabase and Application Developer

Commented:
try with Not Exist instead of Not In

Author

Commented:
Mikes script did the trick.  Thanks

Author

Commented:
Thanks Mike this is what I was needing.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial