我有以下数据:
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
我希望仅获得欧洲区域供应商的数据,而不是同样在美国运营的供应商的数据。因此,根据上述数据,我仅需要供应商B和D,因为供应商B和C也在美国运营(分别为纽约和奥斯汀)。
我正在尝试类似的事情:
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')
问题在于,这仍然返回供应商A(对于纽约)和供应商C(对于奥斯丁)。
有人可以帮忙吗?
谢谢。 A
答案 0 :(得分:1)
我将在此处使用聚合:
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
答案 1 :(得分:1)
使用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'
)
假设所有其他国家/地区都来自欧洲。
或者:
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'
)
答案 2 :(得分:0)
您可以使用agrep(
pattern = target,
x = text,
value = TRUE
)
:
[
{
"ID": "FIRST",
"Name" : "FirstName",
"ZIP" : "0815"
},
{
"ID": "SECOND",
"Name" : "SecondName",
"ZIP" : "4711"
},
{
"ID": "THIRD",
"Name" : "ThirdName",
"ZIP" : "123"
}
]
您还可以使用:
not exists
我不确定列出欧洲国家或列出世界其他国家是否更容易。
答案 3 :(得分:0)
您应该像这样逆转条件:
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')
请参见示例HERE