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.
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
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
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)
inner join CustTerritory ct
on ct.c_id = c.c_id
Where c.Territory Not In(1.05, 1.09)
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.
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
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
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.
ASKER
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
c_name c_id
Bob 1
Jill 3
Paul 7
Jim 6
joe 5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>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.
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.
ASKER
Didn't mean OR but AND.
ASKER
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
ASKER
Mikes script did the trick. Thanks
ASKER
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
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
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.
Open in new window