在 Access 2010 中查询

Query in Access 2010

有了这个查询

SELECT Year, Month,  Customer, Cod_user, Int(Sum(hours)) AS hours
FROM T_Att
GROUP BY Year, Mounth, Customer, Cod_user
HAVING (((Year)="2016") AND ((Month)="03") AND ((Customer)="CA"));

我得到以下结果

如果要添加全局求和(sum)和一个key,需要改什么?

虽然我还没有测试你应该可以添加另一个字段:

这将添加所有年份、月份和客户的所有时间。

SELECT      Year, 
            Month,  
            Customer, 
            Cod_user, 
            Int(Sum(hours)) AS lhours,
            (
             SELECT SUM(hours)
             FROM T_Att
            ) AS GlobalTotal
FROM        T_Att
GROUP BY    Year, Month, Customer, Cod_user
HAVING      (((Year)="2016") AND ((Month)="03") AND ((Customer)="CA"));

要将 GlobalTotal 保持到 2016 年 3 月,请使用:

SELECT      Year, 
            Month,  
            Customer, 
            Cod_user, 
            Int(Sum(hours)) AS lhours,
            (
             SELECT SUM(hours)
             FROM T_Att
             WHERE Year = T1.Year AND Month = T1.Month
            ) AS GlobalTotal
FROM        T_Att T1
GROUP BY    Year, Month, Customer, Cod_user
HAVING      (((Year)="2016") AND ((Month)="03") AND ((Customer)="CA"));

注意 - 我已将 hours 重命名为 lhours 以避免循环引用。您还应该考虑重命名 yearmonth,因为这些是关键字,可能会导致问题。
此外,yearmonth 作为数字比文本更好。