我正在为学校做一个小项目是sql。我正在使用MS Access作为我的数据库引擎。
我正在尝试计算和更新项目阶段,其中阶段的实际结束日期是该阶段中任务的最后一个实际结束日期。 此查询返回一个提示,要求我输入“meet.PhaseId”的值。
UPDATE ProjectPhases
SET RealEndDate = (SELECT MAX(asgn.RealEndDate) AS RealEndDate
FROM Assignments AS asgn
INNER JOIN
(
Meetings AS meet
INNER JOIN ProjectPhases
ON meet.PhaseId = ProjectPhases.PhaseId
)
ON asgn.MeetingId = meet.MeetingId
WHERE meet.PhaseId = ProjectPhases.PhaseId
vAND asgn.RealEndDate IS NOT NULL)
WHERE meet.PhaseId = ProjectPhases.PhaseId;
答案 0 :(得分:2)
考虑在单独的查询中保存聚合查询,然后在更新查询中使用域聚合DLookUp()
。在MS Access SQL中(与其他方言不同),更新查询必须维护可更新条件(或非read-only),这将排除聚合查询的使用。
SELECT查询 (没有ProjectPhases加入)
SELECT meet.PhaseId, MAX(asgn.RealEndDate) AS MaxRealEndDate
FROM Assignments AS asgn
INNER JOIN Meetings AS meet
ON asgn.MeetingId = meet.MeetingId
WHERE asgn.RealEndDate IS NOT NULL
GROUP BY meet.PhaseId
更新查询
UPDATE ProjectPhases p
SET p.RealEndDate = DLookUp("MaxRealEndDate", "myaggQuery", "PhaseId=" & p.PhaseId)
或者,您可以考虑使用嵌套域聚合来避免保存的聚合查询:
UPDATE ProjectPhases p
SET p.RealEndDate = DMax("RealEndDate", "Assignments", "MeetingId=" &
DLookUp("MeetingId", "Meetings", "PhaseId=" & p.PhaseId) &
" AND RealEndDate IS NOT NULL")
答案 1 :(得分:0)
您不从子查询中选择 meet.PhaseId ,因此不存在 PhaseId 以进行过滤。
使用GUI设计器逐步构建查询通常更容易。