2016-01-22 61 views
1

我有以下的MS Access查詢:的MS Access:查詢加上「如果」條件

SELECT a.* 
FROM Version_Change AS a 
RIGHT JOIN 
    (SELECT VERSION, 
      min(ChangeDate) AS minimumdate, 
      ObjectID 
    FROM Version_Change 
    GROUP BY ObjectID, 
      VERSION) AS b ON(a.VERSION = b.VERSION) 
AND (a.ChangeDate = b.minimumdate) 
AND (a.ObjectID = b.ObjectID); 

問題是列「CHANGEDATE」有時可以blank.So我想我的查詢以顯示「ChangeDate」爲空的字段。我如何將這個條件集成到我的查詢中?

回答

1

如果更改日期,對於一個版本和OBJECTID,都是NULL然後min(ChangeDate)將爲空也,和連接將不會成功,因爲a.ChangeDate = NULL將被評估爲NULL這是不正確的。

可以更改ON條件是這樣的:

ON (a.version = b.version) 
    AND 
    (
    (a.ChangeDate = b.minimumdate) 
    OR 
    (a.ChangeDate IS NULL AND b.minimumdate IS NULL) 
    ) 
    AND (a.ObjectID = b.ObjectID); 
0

您可以使用老把戲與的Nz

AND (Nz(a.ChangeDate,b.minimumdate) = b.minimumdate)