Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

Query Help - MSSQL - Averages

Hello, Experts,

I have a query written that does an average from a column via a 52-week period. The manipulation here is dividing a count of a column called RO_NO by 52 to give me the total number of records then dividing by the number of weeks. However, I ran into something a bit weird with the results.

When I run the data I get a result on one record for the total of a number of RO_NO's in the giving time frame to be 16. So when I do the formula to divide 16/52 instead of 0.3076923077 I get 0.

Why is this happening? And how can I fix it?

COUNT(RO_NO) / 52 as [Avg Per Year]

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaiah Melendez
Isaiah Melendez

ASKER

That was it! I am going to award you with the points. Can you answer me this question?

The rounding is very long. To shorten it to lets say XX.XX would the ROUND function be appropriate here even though the count function is being used? Of so, how would it be displayed?
This was the solution to my question, thanks, Scott P.!
ROUND(COUNT(RO_NO) / 52.0, 2) as [Avg Per Year]

Open in new window


This rounds. Duh! I should have tried that first before asking. :)
And to limit the number of decimals in the display to the rounded number, CAST it with that number of decimals:

CAST(ROUND(COUNT(RO_NO) / 52.0, 2) AS decimal(9, 2)) AS [Avg Per Year]