38
SELECT     AVG(variable) AS Expr1, SUM(variable) AS Expr2
FROM       ......

result for AVG is 2, but it is not true, it must be 2.95. What is the problem, any idea?

1
  • What is the type of variable? I guess that it is an integer. Right? Commented Jul 9, 2009 at 7:53

2 Answers 2

66

Try

Select
    AVG(Cast(variable as Float)),
    SUM(variable)
From
    Table
Sign up to request clarification or add additional context in comments.

2 Comments

I would suggest using decimal instead of float. See this: stackoverflow.com/questions/1056323/…
@PouyaBCD Without knowing the precise use-case you can't just suggest to use decimals instead of floats. There exist perfectly valid use-cases for floats/doubles where using decimals would not work well (for example when you need huge ranges, for example numbers from 1e-18 to 1e+18).
0

SELECT AVG(CAST(age)) AS average_age FROM Customer C JOIN Purchase P1 USING (customer_key) JOIN Product P USING (product_key) WHERE P.name = 'Smartwatch' AND YEAR(date) = 2024

2 Comments

The original query does not mention table names, let alone joins. Did you mean to include those? I also notice that the CAST operation does not specify a type, as opposed to the 2009 answer, which specifies FLOAT. Does this work?
Please learn how to use markdown to format your posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.