Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Expression for Average of the Largest 40 in a series

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-08-11 : 08:39:37
Eric writes "Please help me with an SQL equation that will look at a large series of numbers (hundreds) and return the average of the largest 40 numbers in that series.

Thanks for the help! "

robvolk
Most Valuable Yak

15732 Posts

Posted - 2006-08-11 : 08:43:21
Hmmmm. Kinda smells like homework, but I'll bite:

SELECT Avg(num) FROM (SELECT TOP 40 num FROM myTable ORDER BY num DESC) a

Depending on how you want to handle duplicate values, you may need to use:

SELECT Avg(num) FROM (SELECT TOP 40 WITH TIES num FROM myTable ORDER BY num DESC) a

Or, if you want the average of the distinct values:

SELECT Avg(num) FROM (SELECT DISTINCT TOP 40 num FROM myTable ORDER BY num DESC) a
Go to Top of Page

druer
Constraint Violating Yak Guru

314 Posts

Posted - 2006-08-11 : 08:43:56
select avg(select top 40 field_value from table order by field_value DESC) as Avg_For_Top_40

Hope it helps,
Dalton

Blessings aren't so much a matter of "if they come" but "are you noticing them."
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2006-08-11 : 11:40:14

Kapow!

For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -