?
Solved

query help - aggregate function

Posted on 2007-03-28
4
Medium Priority
?
180 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

752 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