Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MSSQL divide 2 sub queries / aggregate_functions

Posted on 2011-02-22
4
Medium Priority
?
1,313 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
[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
4 Comments
 
LVL 59

Accepted Solution

by:
HainKurt earned 1000 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:Ephraim Wangoya
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

722 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