Solved

MSSQL divide 2 sub queries / aggregate_functions

Posted on 2011-02-22
4
1,280 Views
Last Modified: 2012-06-27
Hi I'm trying to divide & average out ratingsum / filenamecount.

When I add  
(SELECT COALESCE(ratingsum,1) / COALESCE(filenamecount,1)) as divnumber
I receive an sql server error stating that either columns doesn't exist

SELECT filename, rating,
                          (SELECT     SUM(rating) AS Expr1
                            FROM         mytable AS GV
                            WHERE      GV.filename = a.filename) AS ratingsum,
                          (SELECT     COUNT(*) AS Expr2
                            FROM          mytable AS GV
                            WHERE       GV.filename = a.filename) AS filenamecount,
                   (SELECT COALESCE(ratingsum,1) / COALESCE(filenamecount,1)) as divnumber
FROM         mytable AS a

Open in new window

0
Comment
Question by:m2ew
4 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 250 total points
ID: 34954165
try this:

select t.*, (SELECT COALESCE(ratingsum,1) / COALESCE(filenamecount,1)) as divnumber
from (
SELECT filename, rating,
                          (SELECT     SUM(rating) AS Expr1
                            FROM         mytable AS GV
                            WHERE      GV.filename = a.filename) AS ratingsum,
                          (SELECT     COUNT(*) AS Expr2
                            FROM          mytable AS GV
                            WHERE       GV.filename = a.filename) AS filenamecount
FROM         mytable AS a
) x
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34954171
There is no FROM <tablename> part for that sub-query. So field are not identified
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34954215
select *, (SELECT COALESCE(ratingsum,1) / COALESCE(filenamecount,1)) as divnumber
(
SELECT filename, rating,
                          (SELECT     SUM(rating) AS Expr1
                            FROM         mytable AS GV
                            WHERE      GV.filename = a.filename) AS ratingsum,
                          (SELECT     COUNT(*) AS Expr2
                            FROM          mytable AS GV
                            WHERE       GV.filename = a.filename) AS filenamecount
                   
FROM         mytable AS a )
0
 

Author Closing Comment

by:m2ew
ID: 34954401
Switched Select t.* to Select x.* for correct alias
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Log size 3 20
SQL Quer 4 22
reccommendations for a free msft sql query manager? 4 29
interpreting data from function COLUMNS_UPDATED 2 18
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 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