Link to home
Start Free TrialLog in
Avatar of abarefoot
abarefoot

asked on

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.
User generated imageUser generated image
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
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>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

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)
Avatar of abarefoot
abarefoot

ASKER

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.
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
>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.
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
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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.
Didn't mean OR but AND.
Mike's should work but I'm having issues with the NOT IN part.  It's giving me an error on "NOT IN"
try with Not Exist instead of Not In
Mikes script did the trick.  Thanks
Thanks Mike this is what I was needing.
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