Alan392
Alan392

Reputation: 685

Query in Access 2010

With this query

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

I get the following result

enter image description here

If you want to add the global sum (sum) and a key, what needs to change?

enter image description here

Upvotes: 0

Views: 28

Answers (1)

Darren Bartrup-Cook
Darren Bartrup-Cook

Reputation: 19767

Although I haven't tested you should be able to just add another field:

This will add all hours for all years, months and customers.

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

To keep the GlobalTotal to March 2016 use:

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

Note - I've renamed hours to lhours to avoid a circular reference. You should also consider renaming year and month as these are key words and may cause problems.
Also, year and month would be better as numbers rather than text.

Upvotes: 1

Related Questions