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

时间:2018-03-06 15:47:46

标签: sql select unique advantage-database-server

使用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)

Myscreenshot

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

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)