[英]SQL - Select distinct record in one column based on criteria in another
I understand there are similar questions but I haven't found one that discusses this yet... 我了解也有类似的问题,但尚未找到讨论该问题的问题...
Example Sales table. 示例销售表。 Need to return Name of individual who did not sell product type 'A' on 21-Feb-16 需要返回16年2月21日未销售产品类型“ A”的个人名称
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
Desired return: 所需退货:
Name
Joe
Need something more than just... 需要的不只是...
select
distinct Name
from Sales
where Name = 'Joe'
just read about Not in 刚刚读到关于不在
select *
from Sales
where Name not in
(select name
from Sales
where Date='21-Feb-16'
and product_type='A')
If you have a table like SalesPerson
; 如果您有一个类似于SalesPerson
的表; using not exists()
: 使用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'
)
otherwise: 除此以外:
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'
)
You can use the NOT in
and a subselect like the following. 您可以在以下位置使用NOT in
和子选择。 In practical way you retrieve all the sellers excluding the sellers who sold at least one product of type A 实际上,您可以检索所有卖家,但不包括已售出至少一种A型产品的卖家
select distinct
Name
from
Sales
where
Name not in (select distinct Name
from Sales
where Product_type = 'A' and date '21-feb-2016')
How about using: 如何使用:
select
Name
from Sales
where Name = 'Joe'
group by Name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.