SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Cannot perform an aggregate function on an express
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

tadhg88
Yak Posting Veteran

Ireland
62 Posts

Posted - 06/14/2006 :  09:56:35  Show Profile
Hi i am tryin to get the max of collection of calculated averages but when i try to use the simple option of using the max outside the avg function i get this error "Server: Msg 130, Level 15, State 1, Line 2
Cannot perform an aggregate function on an expression containing an aggregate or a subquery." so im just wondering if anyone has any idea how to avoid this problem
this is my code

Select 'Team Averages ' =
max(avg(case Code when 'A' then 4.0 when 'B' then 3.0 when 'C' then 2.0 when 'D' then 1.0 else 0.0 end))
From ScoutingReport sr
Where sr.Type = 1
And sr.ScoutId = 11619
group by matchid

and this is my output at the moment there are 3 teams and the averages are
-----------------------------
Team Averages
1.812500
2.937500
1.187500
-----------------------------
thanks in advance Tim

madhivanan
Premature Yak Congratulator

India
22713 Posts

Posted - 06/14/2006 :  10:03:11  Show Profile  Send madhivanan a Yahoo! Message

Select max(average) as 'Team Averages ' from
(
	Select avg(case Code when 'A' then 4.0 when 'B' then 3.0 when 'C' then 2.0 when 'D' then 1.0 else 0.0 end) average
		From ScoutingReport sr
	Where sr.Type = 1
		And sr.ScoutId = 11619
	group by matchid
) T


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tadhg88
Yak Posting Veteran

Ireland
62 Posts

Posted - 06/14/2006 :  10:08:52  Show Profile
thanks a lot thats prefect but can i just ask what the T at the end represents and why it doesnt work when i remove it

Edited by - tadhg88 on 06/14/2006 10:09:15
Go to Top of Page

RyanRandall
Flowing Fount of Yak Knowledge

United Kingdom
1074 Posts

Posted - 06/14/2006 :  10:16:08  Show Profile
The T is a derived table alias. The first bit of this article explains it...

http://www.sqlteam.com/item.asp?ItemID=6692

Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22713 Posts

Posted - 06/14/2006 :  10:20:12  Show Profile  Send madhivanan a Yahoo! Message
Read about Derived tables in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tadhg88
Yak Posting Veteran

Ireland
62 Posts

Posted - 06/14/2006 :  10:25:57  Show Profile
thanks alot folks
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000