对于这个问题,假设我有两个表:“交易”和“类别”。
交易:
类别:
我正在尝试编写一个查询,该查询将汇总按Category_ID分组的一个月(例如,十二月)发生的所有TransactionValues,并显示所有类别的结果,即使特定表上没有事务也是如此类别。
SELECT Categories.Category_ID, Sum(TransactionValue)
FROM Categories LEFT JOIN Transactions ON Categories.Category_ID = Transactions.Category_ID_FK
WHERE Month(TransactionDate) = '12'
这样,我只能从“交易”表上具有至少一项交易的类别中获得结果。
谢谢大家!
答案 0 :(得分:0)
您可以在MS Access中使用子查询:
SELECT c.Category_ID, Sum(t.TransactionValue)
FROM Categories as c LEFT JOIN
(SELECT t.*
FROM Transactions as t
WHERE Month(t.TransactionDate) = 12
) as t
ON c.Category_ID = t.Category_ID_FK
GROUP BY c.Category_ID;
典型的SQL语法将在ON
子句中包含条件:
SELECT c.Category_ID, Sum(t.TransactionValue)
FROM Categories as c LEFT JOIN
Transactions as t
ON c.Category_ID = t.Category_ID_FK AND Month(t.TransactionDate) = 12
GROUP BY c.Category_ID;
我认为MS Access不支持此语法。