35

I'm working on a site that requires me to display a graph of the average number per day of a user input. I have a SQL query already that returns this info to me:

SELECT sum(number)/count(number) as average, date FROM stats WHERE * GROUP BY date

This gives me the result I am looking for, but the result is given with three decimals precision. I want to round of this number. I could do it in PHP or my template engine, of course, but I was curious if there was a way to do this all in the database.

Is there a way to cast an output as an integer (in MySQL)?

3
  • 4
    Out of curiosity, any reason you're not using AVG function? Commented Jan 17, 2012 at 14:15
  • @jimmy_keen had no idea that existed! Is there a reason to use it? (better, faster, stronger?) Commented Jan 17, 2012 at 14:19
  • 2
    @MrGlass - More concise and avoids possible divide by zero problems in other RDBMSs (AFAIK MySQL returns NULL on a divide by zero anyway) Commented Jan 17, 2012 at 14:22

6 Answers 6

55
SELECT 
  CAST(sum(number)/count(number) as UNSIGNED) as average, 
  date 
FROM stats 
WHERE * 
GROUP BY date
Sign up to request clarification or add additional context in comments.

3 Comments

-1. If you use "INT" as type you will get an error. The available types for CAST/CONVERT can be seen in the documentation dev.mysql.com/doc/refman/5.0/en/…
Thats matters? The approach is answering the Q.
@alex on MySQL 5.5 (latest version on some repositories) it will result in a 1064 error. It might work on some newer versions, although it doesn't seem to be documented
29

The valid types for a CAST in MySQL are as follows

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

So you could use

SELECT CAST(sum(number)/count(number) AS UNSIGNED) as average...

Or SIGNED if the SUM part can ever add up to a negative number.

Comments

8

Use the DIV operator.

mysql> SELECT 5 DIV 2;
    -> 2

Integer division. Similar to FLOOR(), but is safe with BIGINT values. Incorrect results may occur for noninteger operands that exceed BIGINT range.

Comments

7

how about using MySQL FORMAT Function?

mysql> SELECT FORMAT(12345.123456, 4);
+-------------------------+
| FORMAT(12345.123456, 4) |
+-------------------------+
| 12,345.1235             |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT FORMAT(12345.123456, 0);
+-------------------------+
| FORMAT(12345.123456, 0) |
+-------------------------+
| 12,345                  |
+-------------------------+
1 row in set (0.00 sec)

Comments

1
SELECT convert(int, sum(number)/count(number)) as average,
  date
FROM stats
WHERE * GROUP BY date

or

SELECT 
  CAST(sum(number)/count(number) as INT) as average, 
  date 
FROM stats 
WHERE * 
GROUP BY date

Comments

0
  1. User mysql function round()
  2. Example round(23.33) will return 23 in msyql.

Your example query will be updated to

SELECT round(sum(number)/count(number)) as average, date FROM stats WHERE * GROUP BY date

Comments

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.