简体   繁体   English

如何从我的 SQL 计算中返回一个保留 2 位小数的值?

[英]How do I return a value to 2 decimal places from my SQL calculation?

I have the follow sql statement and i want to return the value in "percent_change" to 2 decimal places我有以下 sql 语句,我想将“percent_change”中的值返回到小数点后两位

SELECT
  station_name,
  routes,
  ridership_2013,
  ridership_2014,
  ridership_2014 - ridership_2013 AS change_2013_raw,
  ((ridership_2014 - ridership_2013)/ ridership_2013)*100 AS Percent_Change,
FROM
  bigquery-public-data.new_york_subway.subway_ridership_2013_present
WHERE
  ridership_2013 <>0 OR ridership_2014 <>0

I do not know when to add my roundup statement.我不知道什么时候添加我的综述声明。

Should be something like this:应该是这样的:

SELECT
  station_name,
  routes,
  ridership_2013,
  ridership_2014,
  ridership_2014 - ridership_2013 AS change_2013_raw,
  ROUND(((ridership_2014 - ridership_2013)/ ridership_2013)*100, 2) AS Percent_Change,
FROM
  bigquery-public-data.new_york_subway.subway_ridership_2013_present
WHERE
  ridership_2013 <>0 OR ridership_2014 <>0

If you want to round the value in "percent_change", than you need to include your whole calculation in the ROUND() function.如果您想舍入“percent_change”中的值,则需要将整个计算包含在 ROUND() function 中。

For your calculation this will look as follows:对于您的计算,这将如下所示:

ROUND(((ridership_2014 - ridership_2013)/ ridership_2013) * 100, 2) AS Percent_Change

So in your query that looks like this:所以在你的查询中看起来像这样:

SELECT station_name
, routes
, ridership_2013
, ridership_2014
, ridership_2014 - ridership_2013 AS change_2013_raw
, ROUND(((ridership_2014 - ridership_2013)/ ridership_2013) * 100, 2) AS Percent_Change
FROM bigquery-public-data.new_york_subway.subway_ridership_2013_present 
WHERE ridership_2013 <> 0 
OR ridership_2014 <> 0

For more information on the ROUND() function, see https://www.w3schools.com/sql/func_sqlserver_round.asp有关 ROUND() function 的更多信息,请参阅https://www.w3schools.com/sql/func_sqlserver_round.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM