如何将小数位数减少到只有2位?

问题描述 投票:0回答:1

我在 BigQuery 中有此查询

SELECT Id, 
       AVG(VeryActiveMinutes) AS avg_very_active, 
       SUM(VeryActiveMinutes) AS total_very_active,
       SUM(SedentaryMinutes) AS total_sedentary
FROM `fit-mantra-410702.fitbit_data.dailyactivity`
GROUP BY Id
ORDER BY avg_very_active DESC

avg_very_active 返回的值例如 87.333333333333329。

我尝试在 AVG() 内外使用 CAST() 来减少到只有 2 位小数,但没有成功。我该如何解决这个问题?

sql google-bigquery decimal
1个回答
0
投票

使用

ROUND(...,2)
将聚合结果返回到小数点后 2 位。

SELECT Id, 
  ROUND(AVG(VeryActiveMinutes),2) AS avg_very_active, 
  SUM(VeryActiveMinutes) AS total_very_active,
  SUM(SedentaryMinutes) AS total_sedentary
FROM `fit-mantra-410702.fitbit_data.dailyactivity`
GROUP BY Id
ORDER BY avg_very_active DESC
© www.soinside.com 2019 - 2024. All rights reserved.