Access 2010中的查询

时间:2016-05-13 15:20:40

标签: ms-access-2010 access

使用此查询

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"));

我得到以下结果

enter image description here

如果您想添加全球总和(总和),需要更改哪些内容?

enter image description here

1 个答案:

答案 0 :(得分:1)

虽然我没有经过测试,但您应该可以添加另一个字段:

这将为所有年,月和客户增加所有小时数。

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会更好,而不是文字。