[SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery. - Jon Galloway

[SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Problem:

select avg(count(ip)) from pagehits where [month] = 2 group by ip

will give the following error: “Cannot perform an aggregate function on an expression containing an aggregate or a subquery.” MS SQL Server doesn't support it.

Solution - use a derived table:

select avg(ipcount) from (select count(ip) ipcount from pagehits where [month] = 2 group by ip) as sub

I'm posting this because searches on the error message didn't return good results, so if someone else has this problem (read: when I forget this again) this may save some frustration.


Published Wednesday, May 19, 2004 2:36 PM by Jon Galloway
Filed under:

Comments

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

To save some time, post your question on SQLTeam.com. You should get an answer quickly if you've provided enough information. Don't forget to subscribe to the thread so that you know when an answer has been posted.

Thursday, May 20, 2004 12:53 PM by SQLTeam

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Yep. Did a brief search there, but found it in a google group posting first. I found the answer in about 10 minutes, so I didn't post.

http://www.sqlteam.com is great - I've spent hours there over the years.

http://www.sqlservercentral.com/ and http://www.sql-server-performance.com/ are also pretty good resources.

Friday, May 21, 2004 3:44 PM by Jon Galloway

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Hi,

Had very similar problem - in my case it was sum, instead of avg and your solution obviously worked here as well, tx a lot!

Friday, June 11, 2004 12:10 PM by Muscle

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

I had been trying to get the average of two summed columns (weighted average) and this was a great solution.

Thanks!

-Mike

Monday, September 25, 2006 12:28 PM by Mike

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Thanks a bunch mate, I wasted loads of time trying to figure this one out.

Friday, December 29, 2006 1:37 PM by Trevor

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Worked here too - good work fella!

Friday, May 11, 2007 3:13 AM by Crispin

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

I am using a reporting software called Showcase Strategy and am ecountering the same message.  The software uses a different type of select statement.

I am trying to get an Average for the statement below, any ideas on how I can avoid the error?

CAST( TIMESTAMPDIFF( SQL_TSI_DAY, MIN( event_dt ), MAX( work_dt ) ) AS real )

Monday, July 02, 2007 2:29 PM by Charlie

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Just great, where can I dig it? =) Serious this saved me a lot of time and it was the first hit on google.

Thanks

Tuesday, August 21, 2007 9:22 AM by Richard

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

great! been puzzled for a while, thank you!

Tuesday, October 09, 2007 12:19 PM by Benny

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Thank you very much, you saved me!

Friday, January 25, 2008 10:46 AM by Afsaneh

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

This helped me really... Thanks..

Monday, March 17, 2008 9:02 AM by Prabhat

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Hey thanks a lot for this tip....saved my neck :)

Tuesday, May 27, 2008 3:26 AM by Jaime Cerda

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

ver y thanks  great!

Friday, June 06, 2008 8:24 AM by by ramco

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Great job, I had a similar problum (with SUM) and this got it working great!!!

Friday, August 29, 2008 9:49 AM by Smithy

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Great! I had similar problem, I was using Sum on calculated weight and volume, selecting from other subquery , Used this Worked Great for me.

-Srini

Thursday, October 16, 2008 12:21 PM by sriniy28

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

"Usually the simplest solution is the most elegant"

(...with apologies to Occam)

Thanks!

Monday, February 23, 2009 6:14 PM by raysot

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Great .......thnx a ton

Monday, October 19, 2009 1:19 AM by binomial

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

select sum(datediff(s,att_date,(select in_time from sys_para where tran_date between '2009/04/01' and '2010/03/31'))),out_time from attendance

Thursday, December 24, 2009 2:54 AM by Santos

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

I've been using this technique for awhile but went looking for another way about doing this in SQL. See code below. Is there another way someone has devised that will give the same result? This is a small part of a larger query and it does not like it when I use the nested query. Any thoughts would be appreciated. This query only returns one value.

Query:

SELECT SUM(TEST) as SamplePct

FROM(

SELECT LB.Pct*(((LB.AmtBud /(

(SELECT CASE WHEN PR.ProjectType='FP'

THEN ISNULL(SUM(ISNULL(LB.AmtBud,0.00)),0.00)

WHEN PR.ProjectType='TM'

THEN ISNULL(SUM(ISNULL(LB.BillBud,0.00)),0.00)

ELSE ISNULL(SUM(ISNULL(LB.AmtBud,0.00)),0.00)

END

FROM LB WHERE LB.WBS1=PR.WBS1)

+

(SELECT CASE WHEN PR.ProjectType='FP'

THEN ISNULL(SUM(ISNULL(EB.AmtBud,0.00)),0.00)

WHEN PR.ProjectType='TM' THEN ISNULL(SUM(ISNULL(EB.BillBud,0.00)),0.00)

ELSE ISNULL(SUM(ISNULL(EB.AmtBud,0.00)),0.00)

END

FROM EB

left outer join PR as PR1 on EB.WBS1=PR1.WBS1 AND EB.WBS2=PR1.WBS2 AND EB.WBS3=PR1.WBS3

WHERE EB.WBS1=PR.WBS1 AND EB.Account!='4020.00')))*100)/100)

as TEST

FROM LB

left outer join PR on LB.WBS1=PR.WBS1 AND LB.WBS2=PR.WBS2 AND LB.WBS3=PR.WBS3

WHERE PR.WBS1='05012-11'

UNION ALL

SELECT EB.Pct*(((

EB.AmtBud /((SELECT CASE WHEN PR.ProjectType='FP'

THEN ISNULL(SUM(ISNULL(LB.AmtBud,0.00)),0.00)

WHEN PR.ProjectType='TM'

THEN ISNULL(SUM(ISNULL(LB.BillBud,0.00)),0.00)

ELSE ISNULL(SUM(ISNULL(LB.AmtBud,0.00)),0.00)

END

FROM LB WHERE LB.WBS1=PR.WBS1)

+ (SELECT CASE WHEN PR.ProjectType='FP'

THEN ISNULL(SUM(ISNULL(EB.AmtBud,0.00)),0.00)

WHEN PR.ProjectType='TM' THEN ISNULL(SUM(ISNULL(EB.BillBud,0.00)),0.00)

ELSE ISNULL(SUM(ISNULL(EB.AmtBud,0.00)),0.00)

END

FROM EB

left outer join PR as PR1 on EB.WBS1=PR1.WBS1 AND EB.WBS2=PR1.WBS2 AND EB.WBS3=PR1.WBS3

WHERE EB.WBS1=PR.WBS1 AND EB.Account!='4020.00')))*100)/100)

as TEST

FROM EB

left outer join PR on EB.WBS1=PR.WBS1 AND EB.WBS2=PR.WBS2 AND EB.WBS3=PR.WBS3

WHERE PR.WBS1='05012-11'

)

as TESTTABLE

Thanks in advance ~CW

Thursday, October 28, 2010 4:09 PM by Cody Williams

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Helped me a lot!!!

Great work.Thank You.

Friday, December 03, 2010 1:08 PM by anonymous

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Thanks, this was really helpful.

Wednesday, December 08, 2010 2:04 PM by Brendan

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Select CashAmount=Sum(Case When P.PayModeCode=(select paymodecash from pos_mastercode) THEN P.Amount END)

pls help

Tuesday, May 17, 2011 10:05 PM by Pwint

# re: [SQL] Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

This is just some random cooment that i wanted to write to see if i could blast the the hell out of this site later. If it sticks il will come back around and stuff the hell out of it with some links. Please wait for my return and i will take advantage of this opening.

Thursday, January 10, 2013 11:01 PM by btidojwxkd@gmail.com

Leave a Comment

(required) 
(required) 
(optional)
(required)