Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aggregate Functions error

Status
Not open for further replies.

gazza11

Technical User
Jan 12, 2003
46
0
0
AU
I have 2 tables:
Manhours with fields: ManID, VehicleID, ManHours and RPTDate
Vehicles with fields: VehicleID and Vehicle

I am trying to create a query that sums the ManHours for each vehicle based on a given date range. The following query works:

SELECT Sum(manhours.Manhours) AS SumYearHours
FROM manhours
HAVING (((DatePart("yyyy",[rptdate]))=2002) AND ((DatePart("m",[rptdate])) Between 7 And 12) AND ((manhours.VehicleID)=8)) OR (((DatePart("yyyy",[rptdate]))=2003) AND ((DatePart("m",[rptdate])) Between 1 And 6) AND ((manhours.VehicleID)=8));

but it only gives me the hours for vehicleID 8.

I have tried to GROUP BY VehicleID (I then remove the expression from VehicleID) but I keep getting the message "You tried to execute a query that does not include the expression 'DatePart("yyyy",[rptdate]))=2002) AND ((DatePart("m",[rptdate])) Between 7 And 12) AND ((manhours.VehicleID)=8)) OR (((DatePart("yyyy",[rptdate]))=2003) AND ((DatePart("m",[rptdate])) Between 1 And 6' as part of an aggregate function.

What am I doing wrong, everything I read states that you can only use group by where you use aggregate functions.

 

SELECT Sum(manhours.Manhours) AS SumYearHours
FROM manhours
Where (DatePart("yyyy",[rptdate])=2002 AND DatePart("m",[rptdate]) Between 7 And 12) OR (DatePart("yyyy",[rptdate])=2003 AND DatePart("m",[rptdate]) Between 1 And 6) Group By manhours.vehicleid;

let me know if it works, i'm off to bed...
 
Worked a treat - it looked to me like there were too many brackets in my query - but that is what the SQL designer created after I used the wizard. Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top