Share via

error not part of aggregate function complex criteria with Ands an Ors in where clause

Anonymous
2011-08-31T12:34:38+00:00

With the below SQL I am getting an error Tried to Execute a query that does not include specified expression 'Category3' as part of an aggregate function

Not sure how to fix???

SELECT "PlanHouseCirc" AS Type, VehiclePlanData.Category3,

VehiclePlanData.Activity, VehiclePlanData.PlanQuarter, (DateDiff("ww",

[Forms]![FormCirculationCalendar]![txtStartDate],

[plandate]-Weekday([plandate],2)+1)+1) AS WeekStart,

Format(Sum([PlanHouseCirc]/1000), "0.0") AS DivPlanHouseCirc 

FROM VehiclePlanData WHERE ([VehiclePlanData].[Mega] =

[Forms]![FormCirculationCalendar]![cboSegment] OR

[Forms]![FormCirculationCalendar]![cboSegment] is Null) AND

(([VehiclePlanData].[Category3] =

[Forms]![FormCirculationCalendar]![cboCategory3] OR

[Forms]![FormCirculationCalendar]![cboCategory3] = "All"))  AND

((VehiclePlanData.PlanQuarter)=([Forms]![FormCirculationCalendar]![cboQuarte

r]))

AND(([plandate]-Weekday([plandate],2)+1)>=CVDate([Forms]![FormCirculationCal

endar]![txtStartDate])) and

(((VehiclePlanData.Objective)=[Forms]![FormCirculationCalendar]![cboObjectiv

e])) OR

(((VehiclePlanData.Program)=[Forms]![FormCirculationCalendar]![cboProgram]))

OR

(((VehiclePlanData.Campaign)=[Forms]![FormCirculationCalendar]![cboCampaign]

));

sorry for big font???

thanks so much for helping - what am I missing????

thanks,

bkel

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-08-31T13:42:38+00:00

You can not use an aggregate function (Count, Sum, etc) along with other data fields unless you also have a Group By clause for the other data fields.  Even if you remove the Category3 field, you will get the same error for the Activity, PlanQuarter and calulated date field.

If the data fields all have the same values in all the reccords used in the Sum, then just add the Froup By clause at the end of the query:

   GROUP BY Category3, Activity, PlanQuarter, DateDiff("ww", Forms!FormCirculationCalendar!txtStartDate, plandate - Weekday(plandate,2)+1)+1

If that's not what you are trying to do, please explain what the query is supposed to accomplish.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2011-08-31T13:21:37+00:00

You are missing the GROUP BY clause.  Since you are using the sum function, you  need to group by the other fields.

In an aggregate query, any column that does not use one of the aggregate functions (Max, Min, Count, Avg, First, Last, ...) must use the GROUP BY for the column.  One exception, if the column is ONLY used in the where clause you don't need to specify group by or any of the aggregate functions for that column.

SELECT "PlanHouseCirc" AS Type

, VehiclePlanData.Category3

, VehiclePlanData.Activity

, VehiclePlanData.PlanQuarter

, (DateDiff("ww",[Forms]![FormCirculationCalendar]![txtStartDate],[plandate]-Weekday([plandate],2)+1)+1) AS WeekStart

,Format(Sum([PlanHouseCirc]/1000), "0.0") AS DivPlanHouseCirc

FROM VehiclePlanData

WHERE ([VehiclePlanData].[Mega] =[Forms]![FormCirculationCalendar]![cboSegment] OR

[Forms]![FormCirculationCalendar]![cboSegment] is Null)

AND

(([VehiclePlanData].[Category3] =[Forms]![FormCirculationCalendar]![cboCategory3] OR

[Forms]![FormCirculationCalendar]![cboCategory3] = "All")) 

AND

((VehiclePlanData.PlanQuarter)=([Forms]![FormCirculationCalendar]![cboQuarter]))

AND(([plandate]-Weekday([plandate],2)+1)>=CVDate([Forms]![FormCirculationCalendar]![txtStartDate])) and

(((VehiclePlanData.Objective)=[Forms]![FormCirculationCalendar]![cboObjectiv

e]))

OR

(((VehiclePlanData.Program)=[Forms]![FormCirculationCalendar]![cboProgram]))

OR

(((VehiclePlanData.Campaign)=[Forms]![FormCirculationCalendar]![cboCampaign]))

GROUP BY "PlanHouseCirc", VehiclePlanData.Category3, VehiclePlanData.Activity, VehiclePlanData.PlanQuarter, (DateDiff("ww",[Forms]![FormCirculationCalendar]![txtStartDate],[plandate]-Weekday([plandate],2)+1)+1)

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful