选择一个值相同但另一个值不同的值

问题描述 投票:1回答:1

使用Advantage SQL,如果我有列a,这是学习者的标识符,则列b是他们正在研究的特定目标,列c是适用的结束日期,我只想看到身份和最新仅当属于该身份的所有目标都有结束日期时,该身份的结束日期。我试过了:

select ident
into #tmp
from pclscvq
where fundend is not null
group by ident

上面的问题是,它带来了一些但不是全部具有结束日期的标识符。

我已经将结果附加到一个查询中,显示了三列。这个例子是我不想看的,但是我使用了上面提供的代码。基于这个结果我想要的只是看到身份(列a),其中fundend(列c),具有针对所有相关目标输入的日期(vq_ref-column b)

有任何想法吗?

sql select unique advantage-database-server
1个回答
1
投票

通过使用where not exists,您可以在任何目标中排除所有具有空结束日期的标识

select distinct ident
from pclscvq t1
where not exists (select 1 
                  from pclscvq t2 
                  where t2.ident = t1.ident 
                  and t2.fundend  is null)

如果您的DBMS不支持此功能,您可以自行加入:

select distinct ident
from  pclscvq t1
left join pclscvq t2
on t1.ident = t2.ident
and t2.fundend is null
where t1.ident is not null
and t1.fundend is not null

或使用not in(最后的手段)

select distinct ident
from  pclscvq t1
where ident not in (select ident from pclscvq where fundend is null)
© www.soinside.com 2019 - 2024. All rights reserved.