我知道有类似的问题,但我还没有找到一个讨论这个......
示例销售表。需要退回未销售产品类型的个人姓名' A'在16日 - 2月16日
Name Date Product Type
John 21-Feb-16 A
John 21-Feb-16 B
Joe 21-Feb-16 D
Joe 21-Feb-16 B
Jane 21-Feb-16 A
Jane 21-Feb-16 D
期望的回报:
Name
Joe
需要的不仅仅是......
select
distinct Name
from Sales
where Name = 'Joe'
答案 0 :(得分:0)
刚刚阅读
中的Not notselect *
from Sales
where Name not in
(select name
from Sales
where Date='21-Feb-16'
and product_type='A')
答案 1 :(得分:0)
如果您有SalesPerson
这样的表格;使用not exists()
:
select Name
from SalesPerson sp
where not exists (
select 1
from Sales s
where s.SalesPersonId = sp.SalesPersonId
and s.ProductType = 'A'
and s.date = '20160221'
)
否则:
select distinct Name
from Sales s
where not exists (
select 1
from Sales i
where s.Name = i.Name
and i.ProductType = 'A'
and i.date = '20160221'
)
答案 2 :(得分:0)
您可以使用EXCEPT
运算符。
SELECT DISTINCT Name
FROM Sales
EXCEPT
SELECT Name
FROM Sales
WHERE ProductType = 'A' AND Date = '21-Feb-16'
答案 3 :(得分:0)
您可以使用NOT in
和以下子选择。实际上,您检索所有卖家,不包括卖出至少一种A类产品的卖家
select distinct
Name
from
Sales
where
Name not in (select distinct Name
from Sales
where Product_type = 'A' and date '21-feb-2016')
答案 4 :(得分:-2)
如何使用:
select
Name
from Sales
where Name = 'Joe'
group by Name