我有以下查询:
SELECT t1.Material, t1.Amount, t2.amtLast, t3.amtLast
FROM FAUF t1
LEFT JOIN
(SELECT FAUF.Material AS mat, FAUF.Amount AS amtLast
FROM FAUF
WHERE (year=2014)) t2
ON (t1.Material = t2.mat)
LEFT JOIN
(SELECT FAUF.Material AS mat, FAUF.Amount AS amtLast
FROM FAUF
WHERE (year=2013)) t3
ON (t1.Material = t3.mat)
WHERE (t1.year=2015);
MS Access给我一个错误,即(t1.Material = t2.mat)中缺少表达式。如果我排除了最后一次JOIN查询的工作原理,我缺少什么?
答案 0 :(得分:0)
MS Access需要额外的括号才能进行连接。试试这个:
SELECT t1.Material, t1.Amount, t2.amtLast, t3.amtLast
FROM (FAUF t1 LEFT JOIN
(SELECT FAUF.Material AS mat, FAUF.Amount AS amtLast
FROM FAUF
WHERE (year=2014)
) t2
)
ON (t1.Material = t2.mat) LEFT JOIN
(SELECT FAUF.Material AS mat, FAUF.Amount AS amtLast
FROM FAUF
WHERE (year=2013)
) t3
ON (t1.Material = t3.mat)
WHERE (t1.year = 2015);
但是,您可以使用条件聚合执行此操作,这比join
方法更简单:
select f.Material,
sum(iif(f.year = 2015, amount, 0)) as amt_2015,
sum(iif(f.year = 2014, amount, 0)) as amt_2014,
sum(iif(f.year = 2013, amount, 0)) as amt_2013
from fauf f
group by f.Material;