2017-05-31 44 views
0

所以我发现这2篇文章,但他们并没有完全回答我的问题...查找特定的值,并显示来自不同领域的相应值在SQL

Find max value and show corresponding value from different field in SQL server

Find max value and show corresponding value from different field in MS Access

我有一个这样的表...

ID  Type  Date 
1  Initial  1/5/15 
1  Periodic  3/5/15 
2  Initial  2/5/15 
3  Initial  1/10/15 
3  Periodic  3/6/15 
4   
5  Initial  3/8/15 

我需要得到所有的“周期”或NULL和相应的日期的ID号码。所以,我想获得查询结果,看起来像这样...

ID  Type Date 
1 Periodic 3/5/15 
3 Periodic 3/6/15 
4 

我已经试过

select id, type, date1 
from Table1 as t 
where type in (select type 
       from Table1 as t2 
       where ((t2.type) Is Null) or "" or ("periodic")); 

但是,这并不工作...从我读过有关NULL你不能比较空值... Why in SQL NULL can't match with NULL?

所以,我想

SELECT id, type, date1 
FROM Table1 AS t 
WHERE type in (select type 
       from Table1 as t2 
       where ((t.Type)<>"Initial")); 

但是这不会给我4的ID ...

有什么建议吗?

+1

根据你的用户名,我猜测正确的标签是ms-access。 –

回答

0

除非我失去了一些东西,你只是想:

select id, type, date1 
from Table1 as t 
where (t.type Is Null) or (t.type = "") or (t.type = "periodic"); 

or适用于布尔表达式,而不是被比较值。

+0

感谢您的回复!雅,但如果我添加到原始数据集“4 Periodic 3/6/15”,我同时得到“4 Periodic 3/6/15”和“4”......所以如果两者都有,我需要去获得“3/6/15/4”...这就是为什么我试图去匹配他们...... – AccessProgrammer

相关问题