Solved

query help - aggregate function

Posted on 2007-03-28
4
170 Views
Last Modified: 2010-03-19
Hi:

I have a table:
EmployeeID   Score
-------------   --------
1                  100
2                     40
3                     40

I want to find out the average score of all employees and the percentage of employees whose score is above average IN ONE QUERY. That is, in this case, the query should return 60 and 0.333. Anyway I can do this? Thanks.
0
Comment
Question by:sdc248
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18811352
what about this

select  avg_score, ( select cast( count(*) as decimal (10,2)) from yourtable where score >= avg_score) / count_records
from (
select avg(score)  avg_score, count(*) count_records from yourtable
from yourtable
) as l
0
 

Author Comment

by:sdc248
ID: 18811535
Thanks angelIII. I think that'll do it. However, in reality the base table is in fact a result of a complicated sub-query, I don't feel like repeating it in the query. Is there any other way to do it?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18811579
with sql server 2005, you can use the CTE (common table expression).
with SQL server 2000 or lower, you could only store the results of the query into a table variable (or temp table), and continue processing from there.
0
 

Author Comment

by:sdc248
ID: 18812008
I use the CTE and it works. Thank you very muich.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question