我有两张桌子: TblA TargetDate和TblB DepartmentName
使用由四个字段组成的复合键连接两个表: RecNr,DiarieNr,ReportMonth,ReportYear
TblA
RecNr, DiarieNr, ReportMonth, ReportYear, TargetDate, DepartmentName
1 1000 7 2013 01/08/2013 DeptA
2 1000 7 2013 01/10/2013 DeptA
3 1000 7 2013 01/08/2013 DeptC
1 3000 7 2013 01/07/2013 DeptB
2 3000 7 2013 01/09/2013 DeptB
3 3000 7 2013 01/01/2014 DeptA
TblB
RecNr, DiarieNr, ReportMonth, ReportYear, Completed
1 1000 7 2013 2013
2 1000 7 2013 2013
3 1000 7 2013 2013
1 3000 7 2013 2013
2 3000 7 2013 2012
3 3000 7 2013 2013
现在 - 我需要为每个部门创建2013年完成的条目的中位数摘要,如下所示:
Department Name TargetDate
DeptA 01/10/2013
DeptB 01/07/2013
DeptC 01/08/2013
我最好也只是猜测这样做就像下面的查询,但是当我在Access中运行sql时,我被提示提供一个DepartmentName。所以我猜它在主查询中没有被正确引用。
SELECT DISTINCT A.DepartmentName,
(SELECT TOP 1 TargetDate
FROM (
SELECT TOP 50 PERCENT TargetDate
FROM TblA A1
WHERE DepartmentName=A2.DepartmentName
ORDER BY A1.DepartmentName
) AS A2
WHERE A.DepartmentName=A2.DepartmentName
ORDER BY TargetDate
)
FROM TblB M
INNER JOIN TblA A ON M.ReportMonth=A.ReportMonth
AND M.ReportYear=A.ReportYear
AND M.DiarieNr=A.DiarieNr
AND M.RecNr=A.RecNr
WHERE A.DepartmentName<>'' AND YEAR(TblB.Completed)=2013
答案 0 :(得分:0)
如果要使用表的alias
,则正确的语法是:
来自TblB As M
您错过了AS
最好使用Ms Access查询设计器,它会自动生成正确的语法。