0

I have the following data:

Supplier Name     Supplier City     Supplier Country      Spend
Supplier A        New York          US                    500,000
Supplier A        Dublin            Ireland               300,000
Supplier B        London            UK                    400,000
Supplier C        Austin            US                    300,500
Supplier C        London            UK                    200,000
Supplier D        Barcelona         Spain                 500,000
Supplier D        Paris             France                650,000

I am looking to get the data only for the regional Europe suppliers and not the one that also operate in the US. So from the above data I only need Supplier B and D since A and C also operate in the US (New York and Austin respectively).

I am trying something like:

select
supplier_name,
  supplier_city, supplier_country, spend
from
supplier_detail
where
supplier_name IN (select distinct supplier_name, supplier_city, supplier_country from supplier_detail where supplier_country<>'US')

The problem is that this still returns Supplier A (for Newyork) and Supplier C (for Austin).

Can anyone help?

Thanks. A

1
  • Aside from the good answer provided by Gosfly, you may want to normalize your data. What happens if "Supplier A" changes their legal name. Are you going to go through each record and update? Similarly for possible City which would have a country as well. Just something to consider for future. Commented Nov 18, 2019 at 14:36

4 Answers 4

1

I would use aggregation here:

SELECT sd1.supplier_name, sd1.supplier_city, sd1.supplier_country, sd1.spend
FROM supplier_detail sd1
INNER JOIN
(
    SELECT supplier_name
    FROM supplier_detail
    GROUP BY supplier_name
    HAVING COUNT(CASE WHEN supplier_country = 'US' THEN 1 END) = 0
) sd2
    ON sd1.supplier_name = sd2.supplier_name

screen capture from demo below

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

With not exists:

select supplier_name, supplier_city, supplier_country, spend
from supplier_detail s
where not exists (
  select 1 from supplier_detail
  where supplier_name = s.supplier_name and supplier_country = 'US'
)

assuming that all the other countries are from Europe.
Or:

select supplier_name, supplier_city, supplier_country, spend
from supplier_detail 
where supplier_name not in (
  select supplier_name from supplier_detail
  where supplier_country = 'US'
) 

1 Comment

@AbhishekDahiya then don't forget to accept th eanswer.
0

You can use not exists:

select sd.*
from supplier_detail sd
where not exists (select sd2.*
                  from supplier_detail sd2
                  where sd2.supplier_name = sd.supplier_name and
                        sd2.supplier_country not in ('Ireland', 'UK', 'Spain', 'France', . . .)
                 );

You can also use:

where not exists (select sd2.*
                  from supplier_detail sd2
                  where sd2.supplier_name = sd.supplier_name and
                        sd2.supplier_country not in ('US')
                 );

I'm not sure if it is easier to list out the European countries or to list out the rest of the world.

Comments

0

You should inverse your condition like this :

SELECT
  supplier_name,
  supplier_city, 
  supplier_country, 
  spend
FROM
  supplier_detail
WHERE
  supplier_name NOT IN (
    SELECT DISTINCT supplier_name 
    FROM supplier_detail 
    WHERE supplier_country = 'US')

See example HERE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.