Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: avg函数结果最多保留4位吗?怎么可以保留6位呢? (已解决)
To improve efficiency, please provide the following information. A clear problem description can help resolve the issue faster:
[Overview] Scenario + Problem Overview
I need to round the mean result to 3 decimal places using the round half to even rule. However, I found that the avg function result retains up to 4 decimal places, which affects my final rounding result.
[Application Framework and Development Adaptation Business Logic]
[Background] Operations performed
[Phenomenon] Business and database phenomena
[Problem] Current issue encountered
[Business Impact]
[TiDB Version]
[Attachments] Relevant logs and monitoring
In TiDB, the default precision of the AVG function is to retain 4 decimal places. If you need to retain 6 decimal places, you can use the FORMAT
function to format the result of the AVG function.
For example, if you want to calculate the average value of column b
in table t
and retain 6 decimal places, you can use the following SQL statement:
SELECT FORMAT(AVG(b), 6) FROM t;
Here, AVG(b)
calculates the average value of column b
, and the FORMAT
function formats the result to retain 6 decimal places as a string.
Hello, the avg function retains up to 4 decimal places, and the format function rounding to 6 decimal places just adds zeros. Is it impossible for the avg function to retain 6 decimal places?
Solved: First, use the round function to round the field to 6 decimal places (or a longer number of decimal places that satisfies your calculation rounding), and then use the avg function. The result will retain many decimal places.
Understood. Please provide the Chinese text you would like translated to English.
Learned something new again.
Your cousin is impressive, must have a technical background.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.