带有总和和附加条件的msAccess查询

时间:2018-10-04 17:58:25

标签: ms-access

我需要获取特定月份狗的所有“费用”值。当我将此代码与Access一起使用时,它说不支持联接操作。有没有更好的方法可以在MS Access中完成此操作?我需要所有的狗名都回来,即使它们没有特定月份的相关费用

Select Dog.DogName, Dog.DogOwner, Sum(Costs.CostAmount)
From 
(Dog Left join Costs on Dog.DogName = Costs.DogName and Costs.CostMonth = 10)
Group by Dog.DogName, Dog.OwnerName

2 个答案:

答案 0 :(得分:1)

尝试一下:

Select 
    Dog.DogName, Dog.DogOwner, Sum(Costs.CostAmount) As TotalAmount
From 
    Dog 
Left join 
    Costs 
    On 
       (Dog.DogName = Costs.DogName) 
Where
    Costs.CostMonth <= Month(Date())
    Or
    Costs.CostMonth Is Null
Group by 
    Dog.DogName, Dog.OwnerName

答案 1 :(得分:0)

SELECT  Dogs.DogName
        , Dogs.OwnerName
        , (
            SELECT  SUM(Costs.CostAmountAmount) 
            FROM    Costs 
            WHERE   Dogs.DogName = Costs.DogName AND 
                    Costs.CostMonth =NumMonth
          )
FROM Dogs;