Solved

MSSQL divide 2 sub queries / aggregate_functions

Posted on 2011-02-22
4
1,269 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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.
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…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now