下面的代码将具有相同项目名称和不同日期的多个记录分组。
问题是日期字段中的某些日期为空。我需要一种方法让这段代码包含最大日期或空值。
我尝试在Dates下添加Or Is Null但是那只会让我回到我有多个记录的项目的问题
原始SQL
SELECT t1.Project_Name, t1.IssueNmb, t1.Date
FROM Milestones_Detail_Report_Table AS t1
WHERE (((t1.Date)=(SELECT Max(t2.Date)
FROM Table_Name t2
WHERE t2.Project_Name=t1.Project_Name
GROUP BY t2.Project_Name)))
带OR的SQL
SELECT t1.Project_Name, t1.IssueNmb, t1.Date
FROM Milestones_Detail_Report_Table AS t1
WHERE (((t1.Date)=(SELECT Max(t2.Date)
FROM Table_Name t2
WHERE t2.Project_Name=t1.Project_Name
GROUP BY t2.Project_Name))) OR (((t1.Date) Is Null));
表:TableName
Project_Name | IssueNmb | Date
------------------------
Car | 1 | 1/1/15
Car | 1 | 2/1/15 <--This record populates
House | 1 | <--This record does NOT populate
答案 0 :(得分:1)
您可能需要此where
子句:
WHERE (t1.Date IS NULL) OR
(t1.Date = (SELECT iif(COUNT(*) <> COUNT(t2.Date), NULL, Max(t2.Date))
FROM Table_Name t2
WHERE t2.Project_Name = t1.Project_Name
)
)
这会将NULL
视为最长日期,因此如果给定项目的t2.Date
的{{1}}值为NULL
,则子查询将不会匹配。
请注意,子查询中不需要group by
。