Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SUM And COUNT in same Query?

Author  Topic 

monaya
Yak Posting Veteran

58 Posts

Posted - 2009-05-26 : 19:17:05
I'm just trying to gather an average rating.

I want the sum of all x columns and then I want to divide by the count of x columns for the average of x column, but how do i get the SUM and the count in the same table? Thanks

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-05-26 : 19:42:12
Why not just use the AVG() function?

SELECT SUM(x)/COUNT(x) FROM YourTable will return the same results as
SELECT AVG(x) FROM YourTable

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-05-26 : 19:47:45
BTW if you want precision with your result then do something like

SELECT AVG(x*1.0) FROM YourTable

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

monaya
Yak Posting Veteran

58 Posts

Posted - 2009-05-30 : 02:11:13
Thanks, Sorry for the late reply I didnt get an email about this until today even though I was subscribed to the topic.

I'll try that tomorrow!
Go to Top of Page
   

- Advertisement -