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
Microsoft SQL ServerSSRS

Avatar of undefined
Last Comment
Scott Pletcher
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

Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

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

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

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

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

ASKER

Didn't mean OR but AND.
Avatar of abarefoot
abarefoot

ASKER

Mike's should work but I'm having issues with the NOT IN part.  It's giving me an error on "NOT IN"
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

try with Not Exist instead of Not In
Avatar of abarefoot
abarefoot

ASKER

Mikes script did the trick.  Thanks
Avatar of abarefoot
abarefoot

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
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo